The missing CLI for PostgreSQL.
retrofit checks each schema change, advises on its risks, and records it or stops it. Given any point in that history, it writes the schema; given two, the migrations between them, forward and reverse. Plain text in your repository, reviewed like code.
$ retrofit op add RenameColumn track.name title -qnote: schema-context: chinook+ dfa58dac8274 RenameColumn chinook.track.namewarning: apply-risk-rename-in-use: renaming column "chinook.track.name" breaks readers still using the old name
$ retrofit migrate -qq | grep '^ALTER'ALTER TABLE chinook.track RENAME COLUMN name TO title;$ retrofit migrate --down -qq | grep '^ALTER'ALTER TABLE chinook.track RENAME COLUMN title TO name;
retrofit op add records a rename as one line, with a warning for code that still reads the old name. retrofit migrate writes the SQL from that line, and with --down, its reverse. Details in the demo.Every schema change recorded, checked and written to SQL.
No more hand-written migrations. Just describe the change using retrofit.
Oplog. retrofit appends one line per change to a plain text file in your repository. That file is the whole history of your schema, and git diffs it like any other.
Advisories. Record a change that would scan or lock a table, break code still reading an old name, or lose data, and retrofit prints an advisory before any migration runs.
Reverse. From one recorded line, retrofit writes the forward migration and, with --down, the reverse from the same line. Nobody writes the undo by hand.
Identity. Every table, column, index and constraint gets a number at creation, and the record names it by that number. A rename, type change or move changes nothing that points at it.
Snapshot. The whole schema as CREATE statements, in the format pg_dump --schema-only writes, so it fits anywhere a schema dump already does.
Seal. Place one anywhere in the record and every line before it becomes tamper-evident: an edit to any of them surfaces the next time retrofit reads the file.
You know what a change will cost before it runs.
The risks come back on the line you recorded, and so does the undo.
- 01
The warning, on the line
Record a rename and retrofit says what it breaks, before any SQL.
$ retrofit op add RenameColumn track.name title -qnote: schema-context: chinook+ dfa58dac8274 RenameColumn chinook.track.namewarning: apply-risk-rename-in-use: renaming column "chinook.track.name" breaks readers still using the old name
- 02
The undo, from the same line
migrate --downwrites the reverse of the change you recorded. Where it cannot bring the data back, the line says so.$ retrofit migrate --down --from seal --at last -qq | grep -E '^(-- data-loss|-- reverse-irreversible|ALTER)'-- data-loss-on-apply: DropColumn fa88cb050f40 discards the rows in column chinook.track.composer-- reverse-irreversible: chinook.track.composer comes back empty: its rows left with the drop
- 03
The stop, before anything runs
Not every change can be undone with its data. A one-line check in the Makefile reads that line and stops the run.
! retrofit migrate --down --from seal --at last -qq | grep '^-- data-loss-on-apply:'-- data-loss-on-apply: DropColumn fa88cb050f40 discards the rows in column chinook.track.composermake: *** [db-up] Error 1
The warning, the undo and the stop all came from the one line you recorded, before any migration file existed.
A text file in git, reviewed like code.
The schema change travels in the same commit as the code that needs it. Your team reviews, merges and deploys it the same way.
$ retrofit reconcile --all --clean -qnote: both orders clean; chose --keep (default bias)reconcile diff for fork at d32f: before: a0a448af6e07 RenameColumn name="title" 632782644a31 AddColumn name="download_count" type="integer" after: a0a448af6e07 RenameColumn name="title" 632782644a31 AddColumn name="download_count" type="integer" moved: 632782644a31 prev=d32f24c17907 -> a0a448af6e07next: the second-seen block (632782644a31) now follows the first-seen block (a0a448af6e07); if it is upstream's, as after a merge of main into this branch, it stays first when the merged oplog is reconciled with `retrofit reconcile --clean --swap`. To merge from clean sides, run `retrofit reconcile --base <file> --ours <file> --theirs <file> --result <file>`: `--base` the oplog where your branch forked from main, `--ours` main's at its tip, `--theirs` your branch's before this integration, `--result` the schema's oplog.next: chinook.oplog holds the reconciled tail in the working tree, not yet committed; it lands with the work it reconciles.
retrofit reconcile chose an order, made the tail one straight line again, and left the commit to you. Details in the demo.Pull requests. A schema change arrives in a pull request as one added line, in the same commit as the code that reads it.
Two branches. Git keeps both branches’ lines, and retrofit reconcile orders them or reports a conflict.
Identify. retrofit identify reads a dump of a database and names the point in the record that database is at.
Scripts. Every command prints one JSON object per line under --as=json, so a pipeline reads it without parsing prose.
Start with your existing database and migration stack.
Import. retrofit import reads a schema dump from pg_dump, or the migration scripts already in your repository, and records each statement as one line.
Drift. When a database has moved without the record, retrofit drift reads a dump of it and records what changed.
$ retrofit import db/structure.sql --ddl-only --seal -q | grep '^imported'note: apply-risk-window-unsealed: schema public: no Seal before this run, so its migration window began at root and 5 tables in it were treated as newborn (empty); the Seal this run writes starts the next windowimported 46 ops; skipped 7 statements (3 sequences, 3 AlterSeqStmt, 1 declined by --ddl-only) [decision 6, preserved 1]
retrofit import on a Rails schema, read from the db/structure.sql that pg_dump writes. Details in the demo.Compatible with your existing migration runner: retrofit writes the migration, and the runner you already have applies it.
Recorded walkthroughs: Flyway, Liquibase, Rails, plain PostgreSQL.