sqitchcraft

Keep data-driven PostgreSQL applications solid as they change.

sqitchcraft is a set of tools and a development philosophy built on sqitch. It starts from one idea: each table, view and function has one file that says what it is today. Edit the file, and sqiff writes the migration.

You edit the table's file

database/deploy/billing/table/invoice.sql

     create table if not exists billing.invoice (
         id bigint generated always as identity primary key,
         customer_email text not null,
-        amount integer not null,
+        amount numeric(12,2) not null,
         issued_on date not null,
         due_on date,
         created_at timestamptz not null default clock_timestamp()
     );

     comment on column billing.invoice.amount is
-      'What the customer owes, in whole currency units.';
+      'What the customer owes, to the cent.';
+
+    create index if not exists invoice_customer_email
+        on billing.invoice (customer_email);

sqiff writes what an existing database needs

$ sqiff table database/deploy/billing/table/invoice.sql

-- billing.invoice.amount
--   dropped and created again around it
--     view billing.daily_invoiced

-- table column billing.invoice.amount
-- hazard: rewrites the table and its indexes
-- hazard: holds an ACCESS EXCLUSIVE lock, which blocks
--         reads and writes, while it runs
drop view billing.daily_invoiced;
alter table billing.invoice alter column amount
  type numeric(12,2) using amount::numeric(12,2);
create view billing.daily_invoiced as
select issued_on, sum(amount) as total
   from billing.invoice group by issued_on;

-- index billing.invoice_customer_email
-- outside a transaction
-- hazard: builds an index
create index concurrently invoice_customer_email
  on billing.invoice using btree (customer_email);

This is real output, shortened only in the line breaks, from the example project this site follows. sqiff found the view that depends on amount without being told, moved the index into a concurrent build of its own, and named the cost of each step. With --write, it adds these as sqitch changes with reverts.

Where does billing.invoice live?

Ask a new teammate to find out what the invoice table looks like. In a migration-centred project the answer is "read every file that mentions it, in order, or ask the database". Here it is one path you can guess.

Numbered migrations

A typical layout, for comparison

migrations/
  0001_create_invoice.sql
  0002_create_daily_invoiced.sql
  …
  0147_add_invoice_due_on.sql
  …
  0212_alter_invoice_amount.sql
  0213_recreate_daily_invoiced.sql
  0214_add_invoice_customer_index.sql
  …
  0230_add_invoice_paid_at.sql

The table is the sum of five files, 230 numbers apart. None of them shows it whole.

One file per object

The example project, as it is

database/deploy/
  base_schemas.sql
  billing/
    table/
      invoice.sql                  # the table, today
      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-100724-add-column-billing.invoice.paid_at.sql

The migrations are still there, because existing databases need them. They are just no longer the only way to read the schema.

What a reviewer sees

A schema change is two things: what the object should become, and how a running database gets there. Most workflows only show the reviewer the second.

A new migration file

The whole pull request

+ alter table billing.invoice
+   alter column amount type numeric(12,2);

Is anything else defined on amount? Does a view read it? Is the table big? The reviewer has to go and find out, and usually doesn't.

The definition and the migration, together

Three things to read, each with one job

# intent: the table's file, as a normal diff
M  deploy/billing/table/invoice.sql
# effect: what runs on existing databases, with hazards
A  deploy/migrations/…-alter-billing.invoice.amount.sql
A  deploy/billing/table/invoice/index/invoice_customer_email.sql
# order: where it goes in the deployment
M  sqitch.plan

Plus revert and verify scripts, and a snapshot of the old definition. How to review all of it.

Months later

Someone asks why amounts became numeric. In a migration-centred project you search for the migration, hope its name says amount, and read it without context. Here you ask git about one line of one file.

Because each line of the table's definition was last changed by a real commit, git blame and git log work on the schema the way they work on code. The commit carries the edit, the migration and the reason together.

More on history and blame

$ git blame --date=short database/deploy/billing/table/invoice.sql
^251c6d2 (Ana Lima   2026-03-02  7)  customer_email text not null,
0e7d7894 (Ana Lima   2026-11-25  8)  amount numeric(12,2) not null,
^251c6d2 (Ana Lima   2026-03-02  9)  issued_on date not null,
4a38802a (Ben Okafor 2026-11-25 10)  due_on date,
829cc609 (Ben Okafor 2026-11-25 11)  paid_at timestamptz,

$ git log -1 --format=%B 0e7d789
Store invoice amounts to the cent

Partial refunds produce fractional amounts. The daily total
view is recreated around the type change; the rewrite is
acceptable while the table is small.

The path of a change

You do the parts that need judgement. A tool does each part that has a right answer, and stops to ask when there isn't one.

  1. Edit

    Change the object's file, or start a new one from a template.

    you, create-migration
  2. Preview

    See the SQL an existing database needs, what depends on it, and what each step costs.

    sqiff
  3. Write

    Add the migration, its revert and the rework to the plan, ready for review.

    sqiff --write
  4. Lint

    Flag statements that take heavy locks, rewrite tables or defeat indexes.

    sqint
  5. Check the app

    For Haskell services: prepare every query against the new schema.

    hasql-check
  6. Deploy

    Give up on a busy lock after 50 ms instead of queueing traffic, and try again.

    psql-retry

When a table's rows go through a lifecycle (drafted, sent, paid), statecharts let the database enforce it. That is a separate choice from the workflow above, and you can make it later.

Read on

  • The layout

    Every folder and file, why objects and migrations are kept apart, and how reworks let a file change without rewriting history.

  • One change, end to end

    The invoice table from its first file to production, with real output at every step.

  • Functions and views

    Add a function, change its body, change its signature, and survive a column rename it depends on.

  • Git and review

    What a pull request contains, how to review it, what blame tells you, and how to resolve two branches that both changed the plan.

  • Shipping safely

    Why a 50 ms lock timeout matters, what CI should check, and the order to deploy in.

  • Database design

    Shared rules beside the data: schemas by area, lifecycles as statecharts, roles per duty.

  • Lifecycles

    An invoice run as a statechart: only the moves the chart allows, work on entry and exit, one transaction, and the history for free.

  • Practices

    How Kronor keeps a busy database fast and its code simple: serializable with automatic retries, no side effects inside a transaction, timeouts on every role.

  • For agents

    The AGENTS.md we recommend, and a check script, so a coding agent can make schema changes on its own.

  • Tools

    Install steps and the commands you will use for each tool.

The tools

Each one is useful alone. They fit together because they share the conventions on the next page.

ToolWhat it doesAdopt it when
sqiffCompares schemas from PostgreSQL's catalogs and writes the difference as sqitch changes, with dependents, hazards and reverts.You want to edit tables instead of writing alters
create-migrationAdds sqitch changes from templates for new objects and for changes that take several releases.You create objects or write migrations by hand
sqintLints deploy scripts for lock and rewrite hazards, with an optional pass that deploys them and watches the locks.Today, in any sqitch project
pgfmtFormats SQL files with PostgreSQL's own parser, and keeps a statement as written unless the new text parses to the same thing.You want one format for SQL without arguing about it
psql-retryReruns psql when a script gives up on a lock, with jittered backoff. Used as sqitch's client.You deploy to a database with live traffic
hasql-checkPrepares every hasql-th statement against a live schema, and flags nullable and narrowing mistakes.Your services are written in Haskell
statechartsA statechart interpreter in SQL. Transitions and callbacks run in the transaction that sends the event.Rows have a lifecycle worth enforcing
sqitch-planA Haskell library to parse, query and render sqitch.plan files.You write your own plan tooling