Skip to content

feat: check that migrations are ordered - #30

Merged
ilbertt merged 6 commits into
mainfrom
claude/migration-ordering-flag-70b751
Sep 1, 2026
Merged

feat: check that migrations are ordered#30
ilbertt merged 6 commits into
mainfrom
claude/migration-ordering-flag-70b751

Conversation

@ilbertt

@ilbertt ilbertt commented Sep 1, 2026

Copy link
Copy Markdown
Owner

Migrations apply in filename order, and that is the order you meant only while every filename carries a sequence prefix of the same width — 1, 2, 10 applies as 1, 10, 2, silently building a schema production never had.

Naming a prefix pattern is what turns the check on — in the config, or as the flag's value:

// sqlgen.config.ts
export default defineConfig({
  checkMigrationOrder: { prefixPattern: /^\d+/ }, // 0001_init.sql, 0002_add_users.sql, …
});
bun bun-sqlgen generate 'src/**/*.ts' --migrations db/migrations --check-migration-order '^\d+'

There's no default pattern: only the project knows which convention its filenames were named for, and a wrong guess passes a check that never looked at the right prefix.

Width, not "is it a number", is what's checked: equal-width prefixes sort the same way in any positional scheme, so a scheme that doesn't number at all is held to its own terms (/^[a-z]{4}_/, /^\d{14}_/). It fails on a migration with no prefix, on two claiming the same prefix (0035 twice), and on prefixes of differing widths. Gaps are fine.

Deliberately not folded into --check: unlike the other checks it guards generation itself rather than the output, so it runs in every mode — and folding it in would break CI for projects with unprefixed migrations that work fine today.

Migrations apply in filename order, which matches the order their numbers
imply only while the prefixes are padded to the same width. Opt-in via
`--check-migration-order`, or `checkMigrationOrder` in the config.
Equal-width prefixes sort the same way in any positional scheme, so width
is the invariant that makes filename order the intended order — a letter or
timestamp convention holds on its own terms. `checkMigrationOrder` now also
takes a RegExp saying where the prefix ends.
`checkMigrationOrder: { enabled, prefixPattern }` replaces the
boolean-or-RegExp union, and core merges the CLI flag over the config
instead of replacing it, so `--check-migration-order` turns the check on
without discarding a configured prefixPattern.
No default: only the project knows which convention its filenames were
named for, and a wrong guess passes a check that never looked at the right
prefix. `--check-migration-order` contributes only `enabled`, so the
pattern has to come from the config.
With prefixPattern required, naming a pattern is already the opt-in. The
CLI flag takes the pattern as its value, so it no longer needs merging
over the config — it overrides it like every other option.
One paragraph, one example. Drops the duplicate from the config block,
which is about shaping the introspection DB.
@ilbertt
ilbertt force-pushed the claude/migration-ordering-flag-70b751 branch from 3bab5d3 to afb66ba Compare September 1, 2026 12:46
@ilbertt
ilbertt merged commit 000a13e into main Sep 1, 2026
10 checks passed
@ilbertt
ilbertt deleted the claude/migration-ordering-flag-70b751 branch September 1, 2026 14:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant