Every migration ships with its undo.
shift runs safe, reversible migrations for Postgres and MySQL from one CLI.
$ brew install shift-cli~ $ shift plan 0043_add_order_status.up.sql ALTER TABLE orders ADD COLUMN status text 0043_add_order_status.down.sql found ALTER TABLE orders DROP COLUMN status nothing applied: this was a plan ~ $ shift up 0043_add_order_status applied in one transaction, lock_timeout 3s ~ $ shift down 0043_add_order_status reverted schema matches 0042 again
shift won’t apply a migration until its down file has run cleanly on a scratch copy.
How the check worksOne migration, start to undo
Each box is a command; the dashed line is the way back.
- Write 0043.up.sql and 0043.down.sql together.
- shift plan prints the SQL and applies nothing.
- shift check runs up, then down, on a scratch copy.
- shift up applies it and records its checksum: production is at 0043.
- shift down runs 0043.down.sql: the schema is at 0042 again.
Postgres and MySQL, handled differently
Two engines, two ways to fail.
| When | On Postgres | On MySQL |
|---|---|---|
| A migration fails | Rolls back with the transaction | Runs the down file |
| A lock is held | SET lock_timeout before each statement | SET SESSION lock_wait_timeout |
| An index is added | CREATE INDEX CONCURRENTLY | ALGORITHM=INPLACE, LOCK=NONE |
| History is kept | shift_migrations, one row a file | |
Read the plan before it runs
Every statement and the lock it takes.
~/shop $ shift plan --to 0045 file statement lock down ───────────────────────────────── ───────────────────────────────────────────── ────────── ───── 0044_index_orders_customer.up.sql CREATE INDEX CONCURRENTLY orders_customer_idx none found 0045_add_refunds_table.up.sql CREATE TABLE refunds (id bigserial, …) new table found 2 to apply, 0 changes made
From shift new to shift down
Plain SQL files, five commands.
shift new <name>Writes
0046_<name>.up.sqland its.down.sqlshift planTakes
--dband--to, like every commandshift checkNeeds a throwaway database in
--scratchshift up --to <n>Stops at file
<n>, checksums each oneshift down --to <n>Reverts newest first, back to
<n>
Run shift plan on staging first
Install it, then read what it would do.
$ brew install shift-cli$ shift plan --db $STAGING_URL