Commands
Run each with go run ./cmd/migrate <command>.
No database needed
| Command | Does |
|---|---|
diff <name> | Write a migration for what changed in the models |
diff <name> --empty | Write an empty migration to fill by hand |
diff <name> --rename a.b=c | Treat a change as a rename, repeatable |
diff <name> --dry-run | Print the SQL and write nothing |
check | Fail if models, snapshot, and files are out of step |
snapshot | Accept the models as they are, with no migration |
adopt <dump.sql> | Make a schema dump the first migration |
squash [name] | Replace all migrations with one baseline, --keep to join them |
schema-dump | Print the whole schema as SQL, --snapshot to use the snapshot |
inspect [table] | List the tables and columns the models describe |
script | Print all migrations as one idempotent file, --plain for other tools |
With a database
| Command | Does |
|---|---|
up | Apply pending migrations, then changed repeatable ones |
up --to <version> | Stop after this version |
up --dry-run | Print what would run |
status | List every migration and where it stands |
drift | Compare the live database with the snapshot, --sql for the fix |
db-show | Summarize the live database: version, size, and tables |
baseline [version] | Record migrations as applied without running them |
repair | Accept changed files, clear failed runs, and forget missing files |
seed [name...] | Run seeders |
reset | Delete everything and apply all migrations, --seed to seed too |
These take --dsn, and otherwise read DATABASE_URL, then Config.DSN.
Look at the models
go run ./cmd/migrate inspect tickets
tickets
id bigint NOT NULL GENERATED
qty bigint NOT NULL DEFAULT 1
status ticket_status NOT NULL DEFAULT 'waiting'
views bigint
due interval NOT NULL
level ticket_level NOT NULL
primary key (id)
index tickets_views_idx
Look at the database
go run ./cmd/migrate db-show
docs_6 on postgres 17.11 (Debian 17.11-1.pgdg13+2), 8550 kB
TABLE ROWS (ESTIMATE) SIZE
ant_migrations 0 32 kB
drift lists what differs between the database and the snapshot, and exits with status 1 on any difference. --sql also prints a migration that would close the gap:
go run ./cmd/migrate drift --sql
The database differs from the snapshot. To match, it would need to:
drop index people_nick_idx on people
drop column people.nick
change column people.name: type character varying(50) to text
DROP INDEX "people_nick_idx";
-- WARNING: this drops column people.nick and its data.
ALTER TABLE "people" DROP COLUMN "nick";
-- WARNING: changing people.name from character varying(50) to text may fail or lose data, depending on existing values.
ALTER TABLE "people" ALTER COLUMN "name" TYPE text;
Before the first snapshot exists, drift compares with the models instead. That is how adopting a database works. Names in Config.Ignore are left out of the comparison.
Fix the history table
repair accepts the current contents of an applied file whose checksum changed:
go run ./cmd/migrate repair
Accepted the current contents of 0015_orders.
It also clears a failed no-transaction run and forgets a migration whose file is gone. Troubleshooting says when to use each.