Migrations and integration tests
Overview
A migration is a versioned schema or data change. A migration ledger records applied versions, but does not prove actual schema matches expectations. Test new and existing data, constraints, application compatibility and rollback or forward-fix strategies in isolated databases.
First example
SELECT version,name FROM ai_migration ORDER BY version;Observed SQLite result (rows in JSON):
[ [ 1, "initial" ]]How it works
Use expand–migrate–contract for compatible rolling deployments: add a compatible field, backfill in bounded batches, switch readers/writers and later remove obsolete fields after verification. DDL transaction support varies by engine and statement. The host migration adds a defaulted column, records a version, verifies rows, rejects repeat application and rolls back a failed migration. Schema drift checks compare actual catalog metadata, not just the ledger. Run integration tests with the production engine and version before release.
Practical example
BEGIN;ALTER TABLE ai_account ADD COLUMN active INTEGER NOT NULL DEFAULT 1;SELECT id,active FROM ai_account ORDER BY id;ROLLBACK;Observed SQLite result (rows in JSON):
[ [ 1, 1 ], [ 2, 1 ], [ 3, 1 ]]Practice and mistakes
Practice: add a nullable or defaulted field, verify old rows and run foreign-key checks. Hint: put compatible DDL and ledger insertion in one transaction where supported. Check rollback leaves no new column. Mistake: treating a successful migration command as proof of correct data.
PostgreSQL 17 example — NOT RUN
SELECT column_name,data_type FROM information_schema.columns WHERE table_schema='public' AND table_name='ai_account' ORDER BY ordinal_position;Expected result, not executed: "Environment-dependent catalog check; no migration performed."