Give every object a home
A database schema is code that many people read and few people write. Lay it out for the readers: one file per object, at a path you can guess, saying what that object is today.
Every tree, file and plan line on this page comes from the example project the site follows: a shop database with an invoice table, changed three times by two developers.
The question it answers
Where do you look to understand billing.invoice?
- Its first migration tells you how it began, months ago.
- Each later migration tells you one thing that happened to it, if you can find them.
database/deploy/billing/table/invoice.sqltells you what the team means it to be now, columns, comments and indexes together, in a form you can read top to bottom.
That is the whole idea. Everything else on this page exists to keep that file honest while existing databases still get upgraded step by step.
The whole tree
sqitch keeps three parallel trees: deploy/ makes a change, revert/ undoes it, verify/ checks it worked. Under each, the path of a file is the name of its change.
sqitch.conf # at the repository root; top_dir = database sqiff.yaml # sqiff's settings (optional) database/ sqitch.plan # the order things are deployed in templates/ # create-migration's templates, if you vendor them deploy/ base_schemas.sql # create schema …; comment on schema … billing/ table/ invoice.sql # the table as it is today: edit this invoice@2026-11-25_sqiff.sql # snapshots of earlier versions, invoice@2026-11-25_sqiff_2.sql # kept so old changes can still invoice@2026-11-25_paid_at.sql # deploy and revert: never edit invoice/ index/invoice_customer_email.sql view/ daily_invoiced.sql migrations/ 2026/11/ 2026-11-25-100552-add-column-billing.invoice.due_on.sql 2026-11-25-100616-alter-billing.invoice.amount.sql 2026-11-25-100617-comment-billing.invoice.amount.sql 2026-11-25-100724-add-column-billing.invoice.paid_at.sql revert/ # the same tree verify/ # the same tree
In a larger project the same shape repeats per schema: deploy/<schema>/{table,view,function,type,trigger/<table>}/<name>.sql. Kronor's production database has around twenty schemas and thousands of deploy files in this layout, and finding any one of them is still a matter of knowing its name.
Definitions and migrations
There are two kinds of deploy script, and keeping them apart is what the rest depends on.
A definition
deploy/billing/table/invoice.sql: what the object is
begin;
create table if not exists billing.invoice (
id bigint generated always as identity primary key,
customer_email text not null,
amount numeric(12,2) not null,
issued_on date not null,
due_on date,
paid_at timestamptz,
created_at timestamptz not null default clock_timestamp()
);
comment on column billing.invoice.amount is
'What the customer owes, to the cent.';
create index if not exists invoice_customer_email
on billing.invoice (customer_email);
commit;
Written as if creating the table today. Its if not exists guards make it safe to run where the table already exists.
A migration
deploy/migrations/2026/11/…-add-column-billing.invoice.due_on.sql: how a database got there
set lock_timeout to '50ms';
set statement_timeout to '50ms';
begin;
alter table billing.invoice
add column if not exists due_on date;
commit;
Runs once on each database, in plan order. Dated, so the folder reads as a log. sqiff and create-migration write these; you rarely start one from nothing.
Never put alter table in a definition file. if not exists only checks that the table exists; it does not add the columns you added to the file. The change to an existing table always comes from a migration.
How a file changes without rewriting history
Once a change has been deployed anywhere, its scripts are history: sqitch will never run them again on that database, and a fresh database still needs them as they were. So how can invoice.sql change at all?
sqitch's answer is a rework. It copies the current scripts to invoice@<tag>.sql, where the tag marks the point in the plan they belong to, and lets you edit the original. From then on, the plan deploys the snapshot where the old change was and the edited file where the new one is.
Doing this by hand is fiddly: a tag, a rework, a migration that comes before it, and reverts for both. sqiff table --write does all of it from your edit. Here is the plan after the third change to the invoice table, planners and timestamps left out:
base_schemas billing/table/invoice # deployed from invoice@2026-11-25_sqiff.sql billing/view/daily_invoiced migrations/2026/11/2026-11-25-100552-add-column-billing.invoice.due_on @2026-11-25_sqiff billing/table/invoice [billing/table/invoice@2026-11-25_sqiff] migrations/2026/11/2026-11-25-100616-alter-billing.invoice.amount billing/table/invoice/index/invoice_customer_email migrations/2026/11/2026-11-25-100617-comment-billing.invoice.amount @2026-11-25_sqiff_2 billing/table/invoice [billing/table/invoice@2026-11-25_sqiff_2] migrations/2026/11/2026-11-25-100724-add-column-billing.invoice.paid_at @2026-11-25_paid_at billing/table/invoice [billing/table/invoice@2026-11-25_paid_at] # deployed from invoice.sql
- Each migration comes before the rework it belongs to, so the
alterruns before the definition that already includes it. - The revert of a reworked definition is the previous definition, not a
drop table: undoing the latest change must not delete the table. - Snapshots are written by sqitch and sqiff. Never create or edit an
@tagfile by hand.
Fresh and existing databases take the same road
There is no separate "build from scratch" script that could drift from the migrations. Every database walks the same plan; it just starts at a different place.
- invoice, as first written
- add due_on
- invoice: no-op
- alter amount
- index
- invoice: no-op
- add paid_at
- invoice: no-op
- invoice, as first written
- add due_on
- invoice
- alter amount
- index
- invoice: no-op
- add paid_at
- invoice: no-op
Dashed steps run, and find the table already in the shape they describe. Struck-out steps were deployed earlier. We checked both roads on the example project: a fresh sqitch deploy, an upgrade of a database at the earlier commit, and sqitch rebase --onto @2026-11-25_sqiff, which reverts to that tag and deploys again.
Names
The change name is the file's path without .sql. sqiff and create-migration use the same patterns, so the plan reads the same whichever wrote it.
| Object | Change name |
|---|---|
| Table | {schema}/table/{name} |
| View | {schema}/view/{name} |
| Function | {schema}/function/{name} |
| Trigger | {schema}/trigger/{table}/{name} |
| Index built on its own | {schema}/table/{table}/index/{name} |
| Domain | type/{name} |
| Migration | migrations/YYYY/MM/YYYY-MM-DD-HHMMSS-{what} |
| Statechart | statechart/{name}-{version} |
Migration timestamps are UTC to the second, which makes accidental clashes between branches rare. The note on each plan line starts with the template that wrote it (add_column: add column billing.invoice.due_on), so the plan also reads as a log of intent.
Configuration
Keep sqitch.conf at the repository root and run every command from there: sqitch, create-migration and sqiff all read it from the current directory. This is the example project's, as sqitch init wrote it plus a [deploy] section:
sqitch.conf
[core]
engine = pg
top_dir = database
[engine "pg"]
target = db:pg:shop
[deploy]
verify = true
mode = change # on failure, revert only the change that failed
sqiff.yaml
templates: database/templates # write migrations with create-migration's templates
With the templates set, sqiff's migrations start with the same 50 ms lock timeout as hand-written ones. sqint's default --database-root is database, so it needs no flags here. hasql-check and psql-retry do not read the project at all: they take a database URL and psql's arguments.
What it costs
- More files. Every rework adds three snapshots. After three changes the invoice table has twelve files across the three trees. They are never edited, and code review can collapse them (see how).
- Reworks to learn. The rules above are few, but each one matters.
sqiff tablefollows them for tables. For a view or a function, runsqitch reworkand edit the file; functions and views walks through it. - A shared plan. Every branch appends to
sqitch.plan, so two branches that both change the schema will conflict there. Resolving it is mechanical, but it has to be done right.
Adopting it
A new project: sqitch init with --top-dir database, a base_schemas change, then create-migration table for each table.
An existing sqitch project: you do not have to move history. Pick the next table you are about to change, and add a definition change for it: create table if not exists with the table as it is now, comments and indexes included. On existing databases it does nothing; on fresh ones the earlier migrations have already created the table, so it does nothing there either. To check the file matches the live table, sqiff table FILE --against-database compares the two and prints any difference. From then on, edit that file.
Not ready for either: sqint and psql-retry work on any sqitch project, whatever its layout.