sqitchcraft

One change, end to end

Ana and Ben work on a shop's billing database. Over one morning they change the invoice table three times. Follow them, and you will have seen everything the tools do for an ordinary change, and where they stop and ask you to decide.

Everything below is real: we ran these commands with sqiff 0.1.2.0, sqint 0.1.0.2, sqitch 1.5.2 and PostgreSQL 18, and copied the output. Only long lines are wrapped.

The starting point

Ana created the table in March with create-migration table --schema billing --table-name invoice, filled in the columns, and added a view the finance dashboard reads.

deploy/billing/table/invoice.sql

begin;

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

    comment on table billing.invoice is
      'An invoice sent to a customer.';

    comment on column billing.invoice.amount is
      'What the customer owes, in whole currency units.';

commit;

deploy/billing/view/daily_invoiced.sql

begin;

    create or replace view billing.daily_invoiced as
    select issued_on, sum(amount) as total
    from billing.invoice
    group by issued_on;

    comment on view billing.daily_invoiced is
      'Total invoiced per day, for the finance dashboard.';

commit;

To follow along you need sqitch, PostgreSQL, sqiff and sqint. With Nix, one command gives you all four:

nix shell nixpkgs#sqitchPg nixpkgs#postgresql_18 github:kronor-io/sqiff github:kronor-io/sqint

A simple change: due_on

Ben needs to record when an invoice is due. He adds one line to the definition:

@@ -7,6 +7,7 @@ begin;
         customer_email text not null,
         amount integer not null,
         issued_on date not null,
+        due_on date,
         created_at timestamptz not null default clock_timestamp()
     );

Then asks sqiff what an existing database needs:

$ sqiff table database/deploy/billing/table/invoice.sql
-- table column billing.invoice.due_on
alter table billing.invoice add column due_on date;

No hazards: a nullable column without a default is a catalog change. sqiff compared the file on master with his edit by building both in a transaction it rolled back, and reading the result from PostgreSQL's catalogs, so it is the database's own opinion of the difference, not a guess from parsing SQL. He writes it:

$ sqiff table database/deploy/billing/table/invoice.sql --write
Created database/deploy/migrations/2026/11/2026-11-25-100552-add-column-billing.invoice.due_on.sql
Created database/revert/migrations/2026/11/2026-11-25-100552-add-column-billing.invoice.due_on.sql
Created database/verify/migrations/2026/11/2026-11-25-100552-add-column-billing.invoice.due_on.sql
Added "migrations/2026/11/2026-11-25-100552-add-column-billing.invoice.due_on" to database/sqitch.plan
Tagged "migrations/2026/11/2026-11-25-100552-add-column-billing.invoice.due_on" with @2026-11-25_sqiff in database/sqitch.plan
Added "billing/table/invoice [billing/table/invoice@2026-11-25_sqiff]" to database/sqitch.plan.
…
added migrations/2026/11/2026-11-25-100552-add-column-billing.invoice.due_on
reworked billing/table/invoice

Because sqiff.yaml points at create-migration's templates, the migration is the same one create-migration add-column would have written, with the type filled in:

deploy

set lock_timeout to '50ms';
set statement_timeout to '50ms';

begin;

alter table billing.invoice
    add column if not exists due_on date;

commit;

revert

begin;

    alter table billing.invoice
        drop column if exists due_on;

commit;

The deploy on his local database shows the rework doing its job: the migration adds the column, and the definition's if not exists finds the table already there.

$ sqitch deploy
Deploying changes to db:pg:shop
  + migrations/2026/11/2026-11-25-100552-add-column-billing.invoice.due_on @2026-11-25_sqiff .. ok
  + billing/table/invoice .. psql:database/deploy/billing/table/invoice.sql:12: NOTICE:  relation "invoice" already exists, skipping
ok

A harder one: amount

Partial refunds have started producing amounts with cents. Ana changes amount to numeric(12,2), and while she is there adds the index on customer_email that support has been asking for:

     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()
@@ -15,6 +15,9 @@ begin;
     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);

Three lines of diff. Here is what they cost on a database that has the table:

$ 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
-- hazard: fails when an existing value does not convert to the new type
-- hazard: recreating a view fails when its query relies on the old type; then edit the view
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;
comment on view billing.daily_invoiced is 'Total invoiced per day, for the finance dashboard.';

-- index billing.invoice_customer_email
-- outside a transaction
-- hazard: builds an index
do $$
begin
    if exists (
        select from pg_index
        where indexrelid = to_regclass('billing.invoice_customer_email') and not indisvalid
    ) then
        raise exception using message = 'index billing.invoice_customer_email is invalid from an earlier failed build; drop it first';
    end if;
end
$$;

create index concurrently invoice_customer_email on billing.invoice using btree (customer_email);

-- table column billing.invoice.amount
comment on column billing.invoice.amount is 'What the customer owes, to the cent.';

Read it the way a reviewer would.

The invoice table is small, so Ana accepts the rewrite and writes it:

$ sqiff table database/deploy/billing/table/invoice.sql --write
billing.invoice.amount
  dropped and created again around it
    view billing.daily_invoiced
Created database/deploy/migrations/2026/11/2026-11-25-100616-alter-billing.invoice.amount.sql
…
Created database/deploy/billing/table/invoice/index/invoice_customer_email.sql
…
Created database/deploy/migrations/2026/11/2026-11-25-100617-comment-billing.invoice.amount.sql
…
Tagged "migrations/2026/11/2026-11-25-100617-comment-billing.invoice.amount" with @2026-11-25_sqiff_2 in database/sqitch.plan
Added "billing/table/invoice [billing/table/invoice@2026-11-25_sqiff_2]" to database/sqitch.plan.
…
added migrations/2026/11/2026-11-25-100616-alter-billing.invoice.amount
added billing/table/invoice/index/invoice_customer_email
added migrations/2026/11/2026-11-25-100617-comment-billing.invoice.amount
reworked billing/table/invoice

The migration carries its hazards as comments, so they reach the reviewer and anyone who reads the file later:

deploy/migrations/2026/11/2026-11-25-100616-alter-billing.invoice.amount.sql

begin;

  -- Do not remove this line
  select set_config('lock_timeout', '50ms', false);

  -- hazard: rewrites the table and its indexes
  -- hazard: holds an ACCESS EXCLUSIVE lock, which blocks reads and writes, while it runs
  -- hazard: fails when an existing value does not convert to the new type
  -- hazard: recreating a view fails when its query relies on the old type; then edit the view
  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;
  comment on view billing.daily_invoiced is 'Total invoiced per day, for the finance dashboard.';

commit;

Its revert goes back to integer the same way, view included. Reverting cannot bring back cents that the cast to integer rounds away; a revert restores the shape of the schema, not data that was lost.

When sqiff asks instead. If you drop a column and add another of the same type, sqiff will not guess whether it is a rename: it stops and asks you to say --rename old:new or --drop-and-add. It does not drop data on a hunch.

What the linter says

sqint reads the deploy scripts on the branch with PostgreSQL's own parser. It needs no database, so it runs in a pre-commit hook in a fraction of a second:

$ sqint $(git diff --cached --name-only --diff-filter=AM master -- database/deploy)
WARN database/deploy/billing/table/invoice.sql:20 [lock/create-index] CREATE INDEX (non-concurrent) — Share lock blocks writes for the duration of the build
BLOCK database/deploy/migrations/2026/11/2026-11-25-100616-alter-billing.invoice.amount.sql:13 [lock/alter-column-type] ALTER COLUMN TYPE — table rewrite under AccessExclusiveLock unless binary-coercible
1 informational or suppressed finding hidden — pass --verbose to show.

Two findings, and both deserve an answer in the code rather than a shrug in the review:

+  -- lint:ignore reason="invoice has a few thousand rows; the rewrite takes milliseconds"
   alter table billing.invoice alter column amount type numeric(12,2) using amount::numeric(12,2);
+    -- lint:ignore reason="no-op: billing/table/invoice/index/invoice_customer_email builds it concurrently first"
     create index if not exists invoice_customer_email
         on billing.invoice (customer_email);
$ sqint --verbose database/deploy/billing/table/invoice.sql
SUPPRESSED database/deploy/billing/table/invoice.sql:22 [lock/create-index] CREATE INDEX (non-concurrent) — Share lock blocks writes for the duration of the build
        reason: no-op: billing/table/invoice/index/invoice_customer_email builds it concurrently first

A suppression without a reason is itself reported. In CI, sqint's second pass deploys each change into a scratch database and reports the locks it really took; see shipping safely.

If your services use hasql-th, this is also where hasql-check earns its place: a query that still reads amount :: int4 keeps working after this change but loses the cents, and hasql-check --types reports it as a narrowing cast.

Deploy

After review, the branch merges and deploys. In production the client is psql-retry, so a migration that cannot get its lock within 50 ms gives up, lets the traffic it was blocking through, and tries again:

sqitch deploy --client database/psql-retry.sh db:pg://deploy@db.internal/shop

Why 50 ms, and what psql-retry does.

Meanwhile Ben has added paid_at on another branch, from the master he had before Ana merged. Both of them appended to sqitch.plan, and git will say so. That story is on the next page.

Changes that take two releases

Some changes cannot be read off the final definition, because the application has to change in the middle. create-migration has recipes for them, each one a series of changes you deploy across releases.

A not null column on a big table

create-migration non-nullable-column --schema billing --table-name invoice --column-name currency
  1. add_column: the column, nullable. A catalog change.
  2. Before the next step ships, every insert must set the column, through the application, a default or a before trigger. The next step starts rejecting rows without it.
  3. add_not_null_constraint: check (currency is not null) not valid. New writes are checked; existing rows are not scanned.
  4. bulk_update: backfill existing rows in batches, committing between them.
  5. validate_constraint: one scan under a lock that does not block reads or writes.
  6. make_column_not_null: set not null, which PostgreSQL proves from the validated check without scanning, then drops the check.

Renaming a column

Release 1
  1. rename-column-step-1
  2. new column + sync trigger + backfill
Application
  1. read and write the new name
Release 2
  1. rename-column-step-2
  2. drop old column and trigger

Between the releases both names work, so old and new application code can run side by side during the rollout.

New objects

For something that does not exist yet, start from a template. create-migration names the change, writes the three scripts, and adds it to the plan:

$ create-migration table --schema billing --table-name invoice
Created database/deploy/billing/table/invoice.sql
Created database/revert/billing/table/invoice.sql
Created database/verify/billing/table/invoice.sql
Added "billing/table/invoice" to database/sqitch.plan

Leave out a flag and it asks, completing schema and table names from your deploy/ folders. There are templates for views, functions, triggers, domains and statechart callbacks; the full list.