Browse documentation

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 is NNN_snake_case_description.sql with 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 *.sql not starting with . (collectMigrations in .framework/cli/commands/migrate.ts).
  • Numbering gaps are expected. 22–31, 39–41, and 63 are 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, so 062_send_email_action.sql and a custom 062_*.sql do 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, user postgres, database postgres. SUPABASE_URL must be a hosted https://<ref>.supabase.co URL and SUPABASE_DB_PASSWORD must be set (--db-password overrides; .env is preloaded).
  • --dry-run prints every collected file, not just pending ones — the pending filter lives inside runMigrations, which dry-run never reaches. To see real pending state, run npx spine-framework migrations status or query _spine_migrations directly.
  • npx spine-framework install-app also calls runMigrations before seeding; a migration failure aborts the install (--no-seed installs files only and tolerates missing DB credentials).

Custom migration rules

  1. One file per change, NNN_name.sql, in custom/migrations/.
  2. 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, and CREATE OR REPLACE so a re-run is survivable — framework files follow the same convention.
  3. 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.
  4. Never put runtime-record seed data (types, roles, pipelines) in a migration when an app manifest seed block 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 upgrade syncs the files and reports new migrations — it never runs them. npx spine-framework migrate applies them (see Upgrade).
  • App-level schema is data, not DDL: public.types design_schema rows, roles, pipelines ship in the app package's manifest.json seed block, applied by install-app/seed-app. install-app copies the package's APP_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) into custom/apps/<slug>/. runMigrations/collectMigrations only scan .framework/migrations and custom/migrations — an app package's migrations/ dir is copied but not applied by install-app; project DDL belongs in custom/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_PASSWORD set (.env or --db-password).
  • [ ] SELECT filename FROM _spine_migrations ORDER BY filename reviewed for drift before major upgrades.
  • [ ] New custom migration is idempotent and numbered after the highest local prefix.
  • [ ] After spine-framework upgrade: npx spine-framework migrate run — upgrade only syncs files.
  • [ ] migrations status used as the pending gate — it reads _spine_migrations; only supabase_migrations.schema_migrations output (Supabase CLI tooling) is meaningless for Spine.

Framework documentation — Verified November 17, 2026 · source revision de75df4.

Was this page helpful? Ask a question · Report a documentation problem