Skip to content

feat(config): read staging's DB from DATABASE_URL__STAGING when present - #509

Merged
oxyc merged 1 commit into
masterfrom
feat/staging-database-url
Nov 27, 2026
Merged

oxyc merged 1 commit into
masterfrom
feat/staging-database-url

Conversation

@oxyc

@oxyc oxyc commented Nov 27, 2026

Copy link
Copy Markdown
Member

Fleet rollout of https://github.com/generoi/arter/pull/172 into the template, so new sites start with it: staging keeps working after a MyKinsta restore of a live backup.

Problem

A restore copies production's files to staging, including shared/.env. Staging then has production's DB credentials, but Kinsta gave staging a database of its own, so every request (and the staging deploy's wp:update) fails with "Error establishing a database connection".

Change

On staging, DATABASE_URL__STAGING replaces DATABASE_URL entirely and goes through the same parser. Keep staging's URL in both environments' shared/.env; a restore then brings it along with everything else.

  • Staging is detected by hostname (\bstaging\b in gethostname()), not by .env, so production's .env must not set WP_ENVIRONMENT_TYPE. Check that before adding the variable.
  • Without the variable, nothing changes: production and DDEV read DATABASE_URL / DB_* as before.
  • The parsing is unchanged, so the existing DATABASE_URL rule still applies: a password containing @, : or / won't parse.

Tested by loading config/application.php with fake env values:

Env .env has Picks
staging both URLs staging
production both URLs production
staging DB_* + staging URL staging
production DB_* + staging URL DB_*
staging DB_* only DB_*

Using it on a site

Add DATABASE_URL__STAGING=mysql://<user>:<pass>@localhost/<db> (from MyKinsta → staging → Database access) to both production's and staging's shared/.env.

Open question: if Kinsta regenerates staging's DB credentials on every restore (not only when staging is recreated), the stored URL goes stale and this doesn't help. A test restore on a site answers that.

The harness ran against another repo's vendor/, because this one's lock wants PHP 8.5 and the host has 8.4; the file only needs roots/wp-config, oscarotero/env and vlucas/phpdotenv to load. Pint passes and the pre-commit hook ran.

🤖 Generated with Claude Code

A MyKinsta restore of a live backup copies production's shared/.env onto
staging, which then points at production's database credentials while
staging has a database of its own. With DATABASE_URL__STAGING kept in both
environments' .env, staging (detected by hostname) still finds its own.

Ref generoi/arter#172.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@oxyc
oxyc merged commit 70be39b into master Nov 27, 2026
1 check passed
@oxyc
oxyc deleted the feat/staging-database-url branch November 27, 2026 16:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant