Write a migration
Goal
Add a .sql file that the migrate command applies exactly once, in order, tracked in the _spine_migrations ledger table. See Database reference for the table map.
Steps
Pick the directory.
custom/migrations/— project/app migrations (default choice).collectMigrationsreads.framework/migrations/*.sqlfirst, thencustom/migrations/*.sql, each sorted lexically — custom migrations always run after every framework migration (.framework/cli/commands/migrate.ts)..framework/migrations/— only for framework-owned changes (you are editing the framework itself).
- Pick the number. Framework files use
NNN_name.sqlwith zero-padded sequence numbers. Occupied ranges today:001–021,032–038,042–062,064–069; gaps22–31,39–41,63are reserved/ consolidated — do not reuse gap numbers (see Database reference). Custom migrations get their own files incustom/migrations/; filenames are tracked verbatim in_spine_migrations, so keep them globally unique. Write idempotent SQL. Migrations run once per filename — but installs re-run them against databases in unknown drifted states, so guard everything:
ALTER TABLE public.my_table ADD COLUMN IF NOT EXISTS my_col text; CREATE INDEX IF NOT EXISTS idx_my_table_col ON public.my_table (my_col); INSERT INTO public.roles (slug, name, is_active) SELECT 'my_role', 'My Role', true WHERE NOT EXISTS (SELECT 1 FROM public.roles WHERE slug = 'my_role');CREATE TABLE IF NOT EXISTS,ADD COLUMN IF NOT EXISTS,DROP … IF EXISTS,ON CONFLICT … DO NOTHING, andINSERT … SELECT … WHERE NOT EXISTSare the house patterns (see.framework/migrations/053_builtin_action_seeds.sql).Apply it.
npx spine-framework migrate # framework + custom npx spine-framework migrate --dry-run # list pending only npx spine-framework migrate --framework-onlyRequires
SUPABASE_URL(a hostedhttps://<ref>.supabase.coURL — the CLI rejects anything else before connecting) andSUPABASE_DB_PASSWORDor--db-password(.framework/cli/commands/migrate.ts).- Check state.
npx spine-framework migrations listreads the_spine_migrationsledger;migrations statusdiffs local files vs applied — see CLI reference.
Verify
_spine_migrationscontains the filename: each file runs in a transaction, recordsINSERT INTO _spine_migrations (filename), and the runner stops at the first failure — fix and re-run (.framework/cli/commands/migrate.ts).- Re-running
migratereports the file as skipped.
Common mistakes
- Renaming a file after it ran. Tracking is by filename — a rename makes it a new migration and re-applies the SQL.
- Non-idempotent DDL.
ALTER TABLE … ADD COLUMNwithoutIF NOT EXISTSfails on the second run (e.g. a fresh install that already got the column from a later reconciliation migration). - Gap numbers.
022–031,039–041,063are reserved — using them collides with framework history. - Depending on framework ordering. Custom files all sort after every framework file; you cannot interleave a custom migration between framework ones — express dependencies inside the SQL (e.g.
CREATE OR REPLACE, existence checks). SUPABASE_URLwith a port, path, or credentials — rejected by the CLI before it ever connects.
Reference
- Database reference — table map, numbering gaps
- CLI reference —
migrate,migrations list,migrations status - Package and install an app — app installers run
migrateautomatically before seeding