sqitchcraft

Schema changes in git

Putting each object in its own file is mostly about people: the reviewer reading a pull request, the engineer asking why a column is the way it is, the two developers whose branches meet in sqitch.plan. This page is about them.

The commits, diffs and conflicts here are from the example project in one change, end to end, captured as git printed them, with paths and long lines shortened.

What is in the pull request

Ana's change to amount touched three lines of the table's definition. Her commit has fifteen files:

$ git show --stat --format= 0e7d789
 deploy/billing/table/invoice.sql                               |  7 +++++--
 deploy/billing/table/invoice/index/invoice_customer_email.sql  | 17 +++++++++++++++++
 deploy/billing/table/invoice@2026-11-25_sqiff_2.sql            | 20 ++++++++++++++++++++
 deploy/migrations/2026/11/…-alter-billing.invoice.amount.sql   | 21 ++++++++++++++++++++
 deploy/migrations/2026/11/…-comment-billing.invoice.amount.sql | 10 ++++++++++
 revert/billing/table/invoice.sql                               |  1 +
 revert/billing/table/invoice/index/invoice_customer_email.sql  |  3 +++
 revert/billing/table/invoice@2026-11-25_sqiff_2.sql            | 19 +++++++++++++++++++
 revert/migrations/2026/11/…-alter-billing.invoice.amount.sql   | 21 ++++++++++++++++++++
 revert/migrations/2026/11/…-comment-billing.invoice.amount.sql | 10 ++++++++++
 sqitch.plan                                                    |  5 +++++
 verify/billing/table/invoice/index/invoice_customer_email.sql  | 18 ++++++++++++++++++
 verify/billing/table/invoice@2026-11-25_sqiff_2.sql            |  7 +++++++
 verify/migrations/2026/11/…-alter-billing.invoice.amount.sql   | 15 +++++++++++++++
 verify/migrations/2026/11/…-comment-billing.invoice.amount.sql |  7 +++++++
 15 files changed, 179 insertions(+), 2 deletions(-)

That looks like a lot, so sort it by what each file is for. Only the first group was written by a person.

FilesWhat they areHow to read them
deploy/billing/table/invoice.sqlThe intent: what the table should beLike code. This is the diff that matters.
deploy/migrations/…, …/index/…The effect: what runs on existing databasesCheck the hazards, the locks, and that it matches the intent.
revert/…, verify/…The way back, and the proof it workedSkim. CI runs them.
sqitch.planThe orderNew lines at the end, and nothing else.
…@2026-11-25_sqiff_2.sqlSnapshots of the old definitionNot at all. They are copies.

git can see that the snapshots are copies. Ask it to look for copies and they shrink to zero-line entries:

$ git show --stat -C --format= 0e7d789
 …
 deploy/billing/table/{invoice.sql => invoice@2026-11-25_sqiff_2.sql} | 0
 …
 revert/billing/table/{invoice.sql => invoice@2026-11-25_sqiff_2.sql} | 0
 …
 15 files changed, 140 insertions(+), 2 deletions(-)

Make that the default, and tell GitHub to fold snapshots away in pull requests:

git config diff.renames copies

.gitattributes

database/**/*@*.sql  linguist-generated=true

GitHub then collapses those files in the diff view and leaves them out of language statistics. They are still in the commit, and still one click away.

Reviewing it

A schema review answers three questions, and each one has its own file.

1. Is this what the table should be?

Read the definition's diff.

@@ -5,7 +5,7 @@ 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,
+        amount numeric(12,2) not null,
         issued_on date not null,

Context lines show the table around the change, which a bare alter table never does. Naming, types, nullability and comments are reviewed here, as design.

2. Is it safe to run on production?

Read the migration, starting with its hazards.

-- 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 …

How big is the table? Is there a lint:ignore with a reason you believe? Does everything that reads amount appear here?

3. Will running code survive it? The database deploys before the new application code, so the old code reads the new schema for a while. A column type change is safe for code that reads it as a number; a dropped or renamed column is not. For Haskell services, hasql-check answers this mechanically.

History and blame

Because each line of a definition is changed by the commit that meant to change it, git's history tools work on the schema the way they work on code.

Who touched this table, and why?

$ git log --format='%h %an %ad %s' --date=short -- database/deploy/billing/table/invoice.sql
829cc60 Ben Okafor 2026-11-25 Record when an invoice was paid
0e7d789 Ana Lima 2026-11-25 Store invoice amounts to the cent
4a38802 Ben Okafor 2026-11-25 Record when an invoice is due
251c6d2 Ana Lima 2026-03-02 Add invoices and the daily total view

Where did each column come from?

$ git blame --date=short database/deploy/billing/table/invoice.sql
^251c6d2 (Ana Lima   2026-03-02  5)     create table if not exists billing.invoice (
^251c6d2 (Ana Lima   2026-03-02  6)         id bigint generated always as identity primary key,
^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,
^251c6d2 (Ana Lima   2026-03-02 12)         created_at timestamptz not null default clock_timestamp()

What has amount been over time?

$ git log -L '/amount /,+1:database/deploy/billing/table/invoice.sql' --format='%h %an %ad %s' --date=short
0e7d789 Ana Lima 2026-11-25 Store invoice amounts to the cent
@@ -8,1 +8,1 @@ begin;
-        amount integer not null,
+        amount numeric(12,2) not null,
251c6d2 Ana Lima 2026-03-02 Add invoices and the daily total view
@@ -0,0 +8,1 @@
+        amount integer not null,

Each answer leads to a commit that holds the edit, the migration that carried it out and the message that explains it, together. With numbered migrations, the same questions start with a full-text search across hundreds of files.

Two limits worth knowing. Blame shows the last commit to touch a line, not when that commit reached production; sqitch log on a database tells you that. And it shows the definition's history, not what a migration did to the data: that lives in the migration and its commit message, so write the message for the person who will read it.

Two branches, one plan

Ben branched from master to add paid_at before Ana merged her change. Both of them edited invoice.sql, on different lines, and both appended to sqitch.plan. When Ben rebased onto the new master, git merged the definition by itself, and stopped at the plan:

$ git rebase master
CONFLICT (content): Merge conflict in database/sqitch.plan

$ tail database/sqitch.plan
billing/table/invoice [billing/table/invoice@2026-11-25_sqiff] … # sqiff: billing/table/invoice as edited
<<<<<<< HEAD
migrations/2026/11/2026-11-25-100616-alter-billing.invoice.amount … Ana Lima … # migration: alter billing.invoice.amount
billing/table/invoice/index/invoice_customer_email … Ana Lima … # add_index: add index billing.invoice.invoice_customer_email
migrations/2026/11/2026-11-25-100617-comment-billing.invoice.amount … Ana Lima … # migration: comment billing.invoice.amount
@2026-11-25_sqiff_2 … Ana Lima … # sqiff: before the changes it writes
billing/table/invoice [billing/table/invoice@2026-11-25_sqiff_2] … Ana Lima … # sqiff: billing/table/invoice as edited
=======
migrations/2026/11/2026-11-25-100709-add-column-billing.invoice.paid_at … Ben Okafor … # add_column: add column billing.invoice.paid_at
@2026-11-25_paid_at … Ben Okafor … # sqiff: before the changes it writes
billing/table/invoice [billing/table/invoice@2026-11-25_paid_at] … Ben Okafor … # sqiff: billing/table/invoice as edited
>>>>>>> b1ae481 (Record when an invoice was paid)

This conflict is expected, not a sign that something went wrong: every schema change appends to the same file. The tempting fix is to keep both blocks, Ana's first. For a hand-written migration that is exactly right. For a rework it is wrong, and the reason is worth understanding.

The stale snapshot. Ben's invoice@2026-11-25_paid_at.sql is a copy of the definition as it was when he ran sqiff, with amount integer and no index. After the rebase it sits after Ana's change in the plan, so it should hold her version. For a table the harm is small, because create table if not exists skips a table that exists, but the file now misstates history. For a view or a function, whose scripts use create or replace, the same mistake is real: reverting Ben's change would put back the definition from before Ana's.

So do not hand-merge the generated files. Throw them away and generate them again on top of the new master. Your edit to the definition is the only thing you wrote, and git has already merged it:

# take everything generated from master
git checkout master -- database/sqitch.plan \
  database/revert/billing/table/invoice.sql database/verify/billing/table/invoice.sql
git rm -q --cached database/*/migrations/2026/11/*paid_at.sql database/*/billing/table/invoice@2026-11-25_paid_at.sql
rm database/*/migrations/2026/11/*paid_at.sql database/*/billing/table/invoice@2026-11-25_paid_at.sql

# the merged definition is still there; ask again what it needs
$ sqiff table database/deploy/billing/table/invoice.sql
-- table column billing.invoice.paid_at
alter table billing.invoice add column paid_at timestamp with time zone;

$ sqiff table database/deploy/billing/table/invoice.sql --write --tag 2026-11-25_paid_at
…
added migrations/2026/11/2026-11-25-100724-add-column-billing.invoice.paid_at
reworked billing/table/invoice

git add -A && git rebase --continue

The new snapshot holds Ana's numeric(12,2) and her index, the plan has her lines followed by Ben's, and the whole plan deploys to both a fresh database and the existing one. Regenerating takes a minute and cannot be subtly wrong in the way a hand merge can.

For a change with no rework, such as a hand-written migration or a new object, the plain resolution is enough: keep master's lines, then yours, and run the checks again.

The opposite happens too. One branch renames customer_email; another adds an index on customer_email. They touch different files, so git merges them without a word, and the result fails to deploy.

No layout prevents that. What prevents it is CI deploying the merged result before it reaches master: require branches to be up to date before merging, or use a merge queue, so the checks on the next page always run against what will actually ship.

Rules worth automating

The first two fit in a pre-commit hook; see shipping safely.