Database Migrations
Spine's migration system is deliberately simple: numbered .sql files applied once, in filename order, each inside its own transaction, recorded in a _spine_migrations ledger table. Everything runs through npx spine-framework migrate (.framework/cli/commands/migrate.ts).
File layout and numbering
- Framework migrations live in
.framework/migrations/— sealed, upgrade-synced. Naming isNNN_snake_case_description.sqlwith a three-digit prefix; plain lexicographic sort is the apply order. - Custom migrations live in
custom/migrations/— project-owned, applied after all framework files in the same run (skip them with--framework-only). - Files are filtered to
*.sqlnot starting with.(collectMigrationsin.framework/cli/commands/migrate.ts). - Numbering gaps are expected.
22–31,39–41, and63are reserved or consolidated numbers — a gap is not a missing migration (see Database Reference). The same number can also appear twice across the framework and custom dirs: the ledger key is the filename, not the numeric prefix, so062_send_email_action.sqland a custom062_*.sqldo not collide.
The _spine_migrations ledger
migrate creates and reads public._spine_migrations (id SERIAL, filename TEXT UNIQUE NOT NULL, applied_at TIMESTAMPTZ).
Per file, inside one transaction: split the SQL into statements, run each, insert the ledger row, COMMIT. On the first statement error the file rolls back, the error is printed, and the run stops — later files are never attempted. Files already in the ledger are skipped, so reruns are safe.
migrations list and migrations status read this same _spine_migrations table (migrations.ts shares collectMigrations with migrate), so status is a trustworthy pending/applied check. Do not confuse it with supabase_migrations.schema_migrations — the Supabase CLI's own ledger, which spine-framework migrate never writes and which always looks empty for Spine.
Running migrations
npx spine-framework migrate # apply pending framework + custom
npx spine-framework migrate --framework-only
npx spine-framework migrate --dry-run- Connects directly to Postgres —
db.<project-ref>.supabase.co:5432, userpostgres, databasepostgres.SUPABASE_URLmust be a hostedhttps://<ref>.supabase.coURL andSUPABASE_DB_PASSWORDmust be set (--db-passwordoverrides;.envis preloaded). --dry-runprints every collected file, not just pending ones — the pending filter lives insiderunMigrations, which dry-run never reaches. To see real pending state, runnpx spine-framework migrations statusor query_spine_migrationsdirectly.npx spine-framework install-appalso callsrunMigrationsbefore seeding; a migration failure aborts the install (--no-seedinstalls files only and tolerates missing DB credentials).
Custom migration rules
- One file per change,
NNN_name.sql, incustom/migrations/. - Idempotent by convention. The ledger makes files apply-once, but drift happens (rows applied by hand, shared dev DBs, ledger restores). Write
IF NOT EXISTS,ON CONFLICT DO NOTHING/DO UPDATE, andCREATE OR REPLACEso a re-run is survivable — framework files follow the same convention. - Never edit a file after it has been applied anywhere — the ledger keys on filename and will not re-run it. Add a new file instead.
- Never put runtime-record seed data (types, roles, pipelines) in a migration when an app manifest
seedblock can express it — seeds get conflict handling, validation, and reconciliation that raw SQL does not.
Rolling out schema changes with app updates
Two different channels, don't confuse them:
- Framework schema changes ship inside
.framework/migrations/in the npm package.npx spine-framework upgradesyncs the files and reports new migrations — it never runs them.npx spine-framework migrateapplies them (see Upgrade). - App-level schema is data, not DDL:
public.typesdesign_schemarows, roles, pipelines ship in the app package'smanifest.jsonseedblock, applied byinstall-app/seed-app.install-appcopies the package'sAPP_COPY_ENTRIES(index.tsx,manifest.json,package.json,pages,components,hooks,utils,lib,views,docs,functions,api,seed,public,installer,mcp,migrations,migrations-archive,tests,agent,decisions,standalone.config.json) intocustom/apps/<slug>/.runMigrations/collectMigrationsonly scan.framework/migrationsandcustom/migrations— an app package'smigrations/dir is copied but not applied byinstall-app; project DDL belongs incustom/migrations/.
Production rollout order: deploy code first or migrate first — both work because migrations are additive-by-convention, but the safe default is migrate, then deploy, so new code never meets a schema older than it expects.
Checklist
- [ ]
SUPABASE_URL+SUPABASE_DB_PASSWORDset (.envor--db-password). - [ ]
SELECT filename FROM _spine_migrations ORDER BY filenamereviewed for drift before major upgrades. - [ ] New custom migration is idempotent and numbered after the highest local prefix.
- [ ] After
spine-framework upgrade:npx spine-framework migraterun — upgrade only syncs files. - [ ]
migrations statusused as the pending gate — it reads_spine_migrations; onlysupabase_migrations.schema_migrationsoutput (Supabase CLI tooling) is meaningless for Spine.
Related
- Database Reference — table catalog and numbering gaps
- Upgrade · Deployment · Troubleshooting
- CLI Reference —
migrate,migrations,install-appflags