feat: add automatic database migration recovery to scripts/develop (#23466)

When developers switch branches, the database may have migrations
from the other branch that don't exist in the current binary.
This causes coder server to fail at startup, leaving developers
stuck.

The develop script now detects this before starting the server:

1. Connects to postgres (starts temp embedded instance for
   built-in postgres, or uses CODER_PG_CONNECTION_URL).
2. Compares DB version against the source's latest migration.
3. If DB is ahead, searches git history for the missing down
   SQL files and applies them in a transaction.
4. If git recovery fails (ambiguous versions across branches,
   missing files), falls back to resetting the public schema.

Also adds --reset-db and --skip-db-recovery flags.
This commit is contained in:
Mathias Fredriksson
2026-03-24 22:04:56 +02:00
committed by GitHub
parent 798a6673c6
commit 78b18e72bf
4 changed files with 748 additions and 0 deletions
+14
View File
@@ -306,6 +306,20 @@ separate title.
## Troubleshooting
### Database migration mismatch after switching branches
If `./scripts/develop.sh` exits with a "database migration conflict" error,
it means the database has migrations from another branch that don't exist
on the current one. You have two options:
```shell
# Roll back the mismatched migrations (preserves your dev data):
./scripts/develop.sh --db-rollback
# Or wipe the database and start fresh:
./scripts/develop.sh --db-reset
```
### Nix on macOS: `error: creating directory`
On macOS, a [direnv bug](https://github.com/direnv/direnv/issues/1345) can cause