Getting started
Start from the database you already have. This run begins with the 11-table, 3,503-track Chinook sample database.
32 sec space to pause f for fullscreen
Read transcript
import-drift-demo: import existing database
Generated from the demo script. Every command below is run verbatim by the recording.
1/3 start with a database you already have
Chinook: 11 related tables and real sample data. No migration history to replay:
grep -c 'CREATE TABLE' chinook.sql | sed 's/^/ tables: /'
grep -c 'INSERT INTO' chinook.sql | sed 's/^/ insert statements: /'
2/3 import it as schema operations
one import turns the schema into operators and sets the existing rows aside:
retrofit import chinook.sql --relaxed --seal --purpose=seed -q > import.log
138 schema operations; the rows from 24 INSERT statements are preserved:
grep -E 'imported [0-9]+ ops|dml-preserved' import.log
the operation log is plain text: one operation per line, reviewable in a diff:
head -5 chinook.oplog
and the data rides alongside it in a sidecar, not buried in the schema:
ls chinook.oplog 0001-chinook.dml.sql
3/3 rebuild the database from the log
materialize the framed seed records as executable SQL:
awk '/^-- retrofit:dml-end/{print ";"} {print}' 0001-chinook.dml.sql > chinook.seed.sql && mv 0001-chinook.dml.sql chinook.seed.records
snapshot emits the schema; the preserved rows follow in the same pipe:
{ retrofit snapshot -q; echo 'SET search_path TO chinook;'; cat chinook.seed.sql; } | psql "$DB" -v ON_ERROR_STOP=1 -q
3,503 tracks and 412 invoices, live. schema and data both rebuilt from the log:
psql "$DB" -tAc 'SELECT count(*) FROM chinook.track' | sed 's/^/ tracks: /'
psql "$DB" -tAc 'SELECT count(*) FROM chinook.invoice' | sed 's/^/ invoices: /'
There is no migrations/ directory. The oplog is the source and psql is the applier.
Start with 11 related tables, 24 data inserts, and no migration history to replay.
Turn the schema into 138 schema operations and preserve its rows in a reviewable sidecar.
Rebuild all 3,503 tracks and 412 invoices from the log and preserved seed data.
Retrofit imports Chinook into a text operation log, generates SQL for each change, and reconciles a directly-added column and index from pg_dump. PostgreSQL executes the generated changes; Git keeps their source reviewable alongside application code.
Next: Chapter 02, Refactor a live table