Phase 9, part 3: schema migrations for both databases
Migrations from schema 30 on are one list (internal/db/migrations.go), written in SQLite's SQL and translated for Postgres (db.PostgresSQL) unless one gives its own. On Postgres the whole migration is one transaction under an advisory lock, and each step is recorded in schema_migrations. TestSchemasMatch compares the two schemas (tables, columns, types, NOT NULL, nocase, indexes), with and without a new migration. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -241,6 +241,18 @@ collation. New SQL should run on both: use `db.Insert` (not `LastInsertId`),
|
||||
`ON CONFLICT … DO NOTHING` (not `INSERT OR IGNORE`), table-qualified columns
|
||||
in an upsert's `DO UPDATE`, and every selected column in `GROUP BY`.
|
||||
|
||||
**Changing the schema.** Add a `db.Migration` to `both` in
|
||||
`internal/db/migrations.go`, written in SQLite's SQL. Postgres gets the same
|
||||
statements translated (`INTEGER` → `BIGINT`, `INTEGER PRIMARY KEY` → an identity
|
||||
column, `REAL` → `DOUBLE PRECISION`, `COLLATE NOCASE` → the `nocase` collation,
|
||||
`strftime(… 'now')` defaults → `now()`). A migration that changes data with
|
||||
SQL only SQLite has gives its `Postgres` SQL too. Never edit one that has
|
||||
shipped. Run the tests on Postgres: `TestSchemasMatch` fails if the two
|
||||
schemas differ in a table, column, type, `NOT NULL`, case-insensitivity or
|
||||
index. On SQLite, a backup is made before migrating; on Postgres, the
|
||||
migrations run in one transaction (several copies of the app can start
|
||||
together), and each is recorded in `schema_migrations`.
|
||||
|
||||
| Path | What |
|
||||
|------|------|
|
||||
| `cmd/supermarket` | The server |
|
||||
|
||||
Reference in New Issue
Block a user