/sqlty:sqlty-breakingfrom sqlty
Checks schema changes and migrations for breaking queries during rolling deploys using sqlty. Validates backward and forward compatibility, reads findings, and guides expand-and-contract migration planning.
Ships in sqlty. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
During a rolling deploy, the code of one revision runs against the schema of
another. sqlty breaking checks both mixes before you deploy:
sqlty breaking --against origin/main
| Direction | Checks | Happens when you |
|---|---|---|
| backward | the queries of --against on the schema of --head | migrate first: old code, new schema |
| forward | the queries of --head on the schema of --against | deploy first: new code, old schema |
Run sqlty version: this skill may be newer or older than the installed sqlty.
When it and sqlty breaking --help disagree, trust the command.
--head defaults to the working tree, uncommitted changes included. Each
revision is read with its own pyproject.toml, schema and Python files. It
exits 0 when both mixes work, 1 when a change breaks one with an error
diagnostic, and 2 on an error such as an unknown revision. Rules and severities
follow [tool.sqlty.rules], as in sqlty check; a warn finding is printed
without failing the run. Use --direction backward when migrations always
run before the deploy, --direction forward when the code always ships first.
app/q.py:2: [unknown-column] column `bio` does not exist in `users` (backward: the queries of origin/main against the schema of the working tree)
| Finding | Means | Fix |
|---|---|---|
backward unknown-column, unknown-table | the new schema drops or renames what running code reads | keep the old name until no deployed code reads it (contract later) |
backward insert-missing-column | a new NOT NULL column without a default breaks running INSERTs | add it nullable or with a DEFAULT, tighten it later |
backward narrow-type, nullable-mismatch | a column became nullable under code that expects a value | widen the code to T | None first |
forward unknown-column, unknown-table | new code reads what the old schema doesn't have yet | add the schema in an earlier pull request |
Only what the other schema breaks is reported: a diagnostic a query already gets
against its own schema is not repeated, and neither is stale-rewrite or
unused-ignore.
With the postgres dialect, each schema statement the baseline doesn't have is
judged too, as a migration that runs on tables with rows:
migrations/0042_index.sql:2: [missing-concurrently] `CREATE INDEX` blocks writes to `users` until the index is built: create it `CONCURRENTLY` (migration: a statement the working tree runs that origin/main does not have)
| Rule | Fix |
|---|---|
missing-concurrently | CREATE INDEX CONCURRENTLY, DROP INDEX CONCURRENTLY; UNIQUE via CREATE UNIQUE INDEX CONCURRENTLY then ADD CONSTRAINT ... USING INDEX |
concurrently-in-transaction | move the statement out of BEGIN ... COMMIT (Django atomic = False, Alembic autocommit_block()) |
constraint-scan | add the constraint NOT VALID, then VALIDATE CONSTRAINT after COMMIT; for SET NOT NULL, validate a CHECK (col IS NOT NULL) first; before ATTACH PARTITION, make the key NOT NULL and validate a CHECK matching the bounds |
table-rewrite | add the column without a volatile default and backfill; change a type through a new column |
add-required-column | add the column nullable or with a constant DEFAULT |
missing-lock-timeout | start the migration with SET lock_timeout = '2s';, under 1 s when it takes several locks (see below); with max configured, a longer timeout counts as unset |
truncate-cascade | name every table to empty: TRUNCATE a, b; |
enum-value-in-transaction | COMMIT after ALTER TYPE ... ADD VALUE, then use the value |
concurrent-index-if-not-exists | DROP INDEX CONCURRENTLY IF EXISTS, then CREATE INDEX CONCURRENTLY without IF NOT EXISTS |
disable-trigger-all | DISABLE TRIGGER USER or name the triggers |
multi-table-lock | one table per transaction |
locked-backfill | COMMIT the schema change, then backfill |
unbatched-backfill | update in batches: WHERE id IN (SELECT id ... LIMIT 5000) |
drop-primary-key | add the new key in the same transaction, or REPLICA IDENTITY USING INDEX first |
rerun-backfill | ON CONFLICT (key) DO NOTHING, or skip copied rows with NOT EXISTS |
dropped-delete-action | add a key with the same ON DELETE action first, or delete the rows in the application |
backfill-without-writer | deploy code that writes the column in every INSERT of the table and every UPDATE of its source, then backfill |
Every schema of the configuration is judged, each [tool.sqlty.databases] entry
included, with the severities of rules and of the [[tool.sqlty.overrides]] that
match the migration's path (include = ["migrations/**"], relative to the
configuration's directory, so for migrations outside it set rules or use an override
without include). A schema several configurations name is judged once, at the
strictest severity, against the baseline schema of each file checked against it (or
of the database of the same name), and each statement is reported once.
Below, the code was written for schema.sql, and against.sql is the schema
it is checked against.
[tool.sqlty]
schema = "schema.sql"
schema.sql:
CREATE TABLE users (id SERIAL PRIMARY KEY, email TEXT NOT NULL, bio TEXT);
against.sql:
CREATE TABLE users (id SERIAL PRIMARY KEY, email TEXT NOT NULL);
from sqlty import SQL
# error: [unknown-column] "column `bio` does not exist in `users`"
bios = SQL[tuple[str | None]]("--sql\nSELECT bio FROM users")
emails = SQL[tuple[str]]("--sql\nSELECT email FROM users")
Split a breaking change into pull requests that each pass sqlty check and
sqlty breaking in both directions, and deploy each before the next. Renaming
users.name to display_name:
| Pull request | Schema | Code |
|---|---|---|
| 1. Widen the code | unchanged | annotate name as str | None, with # sqlty: ignore[stale-type] |
| 2. Expand the schema | add display_name, nullable or with a DEFAULT; relax name to nullable | drop the ignore |
| 3. Expand the code | unchanged | write both columns, read display_name |
| 4. Migrate the data | backfill display_name | unchanged |
| 5. Contract the code | unchanged | read and write only display_name |
| 6. Contract the schema | drop name; make display_name NOT NULL | sqlty check --fix narrows display_name to str |
str | None reads the
rows of both schemas. Against the old schema stale-type says it could be
str: suppress it on that line until the schema changes.Adding a column that nothing reads yet needs only steps 2 and 3. Dropping one needs steps 5 and 6.
sqlty breaking compares two revisions. It can't see the release order, the
row counts, or which code is deployed, so a data migration also follows these
rules:
| Step | Rule | Why |
|---|---|---|
| Readers first | Ship code that reads both the old and the new shape one release before code writes the new one | during a rollout, old code reads what new code writes |
| Dual write | Write the new column or table in the same transaction as the old one, before any backfill | a row written after the backfill is never copied; backfill-without-writer names a query of the code that leaves the column out |
| Backfill last | Run the backfill once the dual-writing release is fully deployed, from a job the deploy doesn't wait for | every writer is live before it reads |
| Small batches | Batch by key, one transaction per batch, SET LOCAL lock_timeout and statement_timeout, a short pause, SKIP LOCKED | unbatched-backfill and locked-backfill report the rest |
| Rerunnable | ON CONFLICT ... DO UPDATE ... WHERE the stored row is older; a rerun writes nothing | a retried batch must not fail or overwrite newer rows; rerun-backfill reports an INSERT ... SELECT without ON CONFLICT |
| Switch reads | Read the new source and fall back to the old one while the new one can lag | a missed write must not change an answer |
One batch, starting after the last key of the batch before, which the migration rules accept:
against.sql:
CREATE TABLE users (id BIGINT PRIMARY KEY, name TEXT NOT NULL);
CREATE TABLE user_names (user_id BIGINT PRIMARY KEY REFERENCES users (id), name TEXT NOT NULL);
-- error:
BEGIN;
SET LOCAL lock_timeout = '400ms';
SET LOCAL statement_timeout = '60s';
INSERT INTO user_names (user_id, name)
SELECT id, name FROM users WHERE id > 5000 ORDER BY id LIMIT 500
ON CONFLICT (user_id) DO UPDATE SET name = excluded.name
WHERE user_names.name IS DISTINCT FROM excluded.name;
COMMIT;
Before you drop anything:
--against a directory with one snapshot per release.ON DELETE action before you drop the column or
constraint that carries it; dropped-delete-action reports the drop.sqlty breaking reports each call to a dropped function as
unknown-function, a warning by default.Locks across releases:
lock_timeout below deadlock_timeout (1 s by default) when a migration
takes several locks, and retry on timeout. missing-lock-timeout checks that it
is set, and its value with [tool.sqlty.rules.missing-lock-timeout] max = "900ms".ADD CONSTRAINT ... NOT VALID and VALIDATE CONSTRAINT go in separate
transactions, best in separate releases. In one transaction the ADD lock is
held through the scan, and constraint-scan reports the VALIDATE.The base branch must be in the clone:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- run: uv run sqlty breaking --output-format github --against origin/${{ github.base_ref }}
See sqlty-ci for the whole job.
When the baseline is not in git history, save a snapshot at release time and
check against it, or against a directory holding one snapshot per version still
deployed. A schema .sql file (a dump) is a baseline for the forward check only:
sqlty breaking --save .sqlty/releases/v1.4.json
sqlty breaking --against .sqlty/releases/
sqlty breaking --against dump.sql
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
7 skills in this plugin
/sqltyWrite, type and check raw SQL in Python with sqlty. Use when adding or editing a query in a project that uses sqlty (strings starting with --sql, SQL[T] or SQLTemplate[T] wrappers, a [tool.sqlty] section), when generating Python row types for a query, when typing raw SQL in Go as sqlty.SQL[R, P], or when checking that queries still fit a schema migration before deploying. Routes to sqlty-rewrite, sqlty-check, sqlty-breaking, sqlty-ci and sqlty-adopt for each task./sqlty-adoptIntroduce sqlty into an existing Python project with raw SQL queries, one module at a time. Use when a project doesn't use sqlty yet and you are asked to add it, type its SQL queries, configure [tool.sqlty] (dialect, schema from a database dump or migrations), choose which queries to convert first, mark the ones sqlty can't type yet, or add the first CI gate./npx claudepluginhub jirikuncar/sqlty --plugin sqltyOther plugins' skills for the same job