Files
PaperClipAI/.github/scripts/tests/check-pr-migration-order.test.mjs
T
DottaandClaude Opus 5.5 2821af0b52 fix(db): fail PR CI on a requires_staging migration without an override (#22628)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Paperclip Cloud deploys a new release while the previous release
still serves the same database
> - Core classifies every new migration as `online_compatible`,
`requires_staging`, or `unknown`, and the pull-request guard checks the
migrations a PR adds
> - The guard fails an `unknown` migration without an override, but it
only prints a notice for a `requires_staging` migration
> - A `requires_staging` release is refused by every stack that serves
two releases at once, so continuous deploy stalls until somebody runs a
maintenance deploy
> - This pull request makes the guard fail a `requires_staging`
migration unless the file carries a justified `requires_staging`
override
> - The benefit is that a migration that forces a maintenance window
cannot merge by accident. The author must split it or accept the
maintenance deploy in writing.

## Linked Issues or Issue Description

Refs #20913 (the classifier and the guard). Refs #22513 (the migration
that stalled deploys).

**What existing behavior does this improve?**
The pull-request migration guard in
`packages/db/src/migration-compatibility.ts` and
`.github/scripts/check-pr-migration-order.mjs`.

**Current behavior**
A new migration classified `requires_staging` passes the guard with a
`::notice`. Nothing stops it from merging. Stacks that deploy with
overlap then refuse the release.

**Proposed behavior**
A new `requires_staging` migration fails the guard unless the file
starts with `-- paperclip:migration-compat requires_staging:
<justification>`. The error lists the reasons and tells the author to
split the migration into expand and contract steps, or to add the
override and accept a maintenance deploy. A justified override passes
with a notice that says the release ships with a maintenance deploy.

**Reason and benefit**
On 2026-11-22 a `requires_staging` migration merged. Continuous deploy
to the overlap-deploy staging stacks stalled for about 4.5 hours, and a
manual maintenance deploy took 38 stacks offline for a median of about
2.5 minutes each. An explicit decision in the migration file makes this
cost visible at review time.

**Breaking changes**
None for existing migrations. The guard judges only the migrations that
a PR adds. A PR that adds a `requires_staging` migration must now add
the override or split the migration.

## What Changed

- `checkNewMigrationCompatibility` returns an error for a
`requires_staging` migration without an override. The error lists the
`requires_staging` and `unknown` reasons and the two ways forward.
- A `requires_staging` migration with a valid override passes with a
notice. It reuses the existing in-file directive and its minimum
justification length. There is no new mechanism.
- `doc/DATABASE.md` ("Migration compatibility with the running release")
says that `requires_staging` fails the PR and when to use the override.
- Doc comments in the CI script and the `--check` CLI usage now name
both blocking classes. The CLI success line says "none blocked".
- Red/green tests in `packages/db/src/migration-compatibility.test.ts`
and `.github/scripts/tests/check-pr-migration-order.test.mjs`:
`requires_staging` without an override fails, with a justified override
passes, with an empty justification fails, and `online_compatible`
passes.

## Verification

- Red: with the old guard, the new tests failed (vitest: 3 failed of
116; node test: 3 failed of 14).
- Green: `cd packages/db && pnpm exec vitest run
src/migration-compatibility.test.ts` passes 116 of 116.
- Green: `node --test
.github/scripts/tests/check-pr-migration-order.test.mjs` passes 14 of
14.
- `pnpm --filter @paperclipai/db typecheck` passes, including
`check:migrations`.
- `pnpm verify` exits 2 because a global CI input changed
(`.github/scripts/check-pr-migration-order.mjs`). Required CI supplies
the broad coverage.

## Risks

- Low risk. The change affects only the PR guard for new migrations.
Deployers and the cloud migrator manifest do not change.
- An open PR that adds a `requires_staging` migration fails the guard
after it rebases. The error says how to fix it.
- Note: migration `0500_funny_malcolm_colcord.sql` from #22513 already
has a justified `requires_staging` override. This guard passes it with a
notice. To stop such a migration at review time, a further policy is
necessary, for example a required reviewer for the override.

> For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and
discuss it in `#dev` before opening the PR. Feature PRs that overlap
with planned core work may need to be redirected — check the roadmap
first. See `CONTRIBUTING.md`.

## Model Used

- Claude Opus 5.5 (Anthropic, model ID `claude-opus-5-5`), in Claude
Code with tool use (shell, file edits) and extended reasoning.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 14:49:17 -06:00

148 lines
6.5 KiB
JavaScript

import { test } from 'node:test';
import assert from 'node:assert/strict';
import { checkMigrationCompatibility, checkMigrationOrder } from '../check-pr-migration-order.mjs';
const migration = (name) => `packages/db/src/migrations/${name}.sql`;
test('passes when a PR has no new migrations', () => {
const result = checkMigrationOrder([migration('0230_on_master')], []);
assert.equal(result.passed, true);
});
test('passes when every PR migration follows the target branch', () => {
const result = checkMigrationOrder(
[migration('0230_on_master')],
[migration('0231_first_in_pr'), migration('0232_second_in_pr')],
);
assert.equal(result.passed, true);
});
test('fails with renumbering guidance when a PR reuses the target branch number', () => {
const result = checkMigrationOrder(
[migration('0230_on_master')],
[migration('0230_from_stale_branch')],
);
assert.equal(result.passed, false);
assert.match(result.message, /already contains migrations through .*0230_on_master\.sql/);
assert.match(result.message, /renumber this PR's migrations starting at 0231/);
assert.match(result.message, /meta\/_journal\.json/);
});
test('fails when a PR inserts a migration before the target branch tip', () => {
const result = checkMigrationOrder(
[migration('0230_on_master')],
[migration('0229_from_stale_branch'), migration('0231_valid_but_after_stale')],
);
assert.equal(result.passed, false);
assert.match(result.message, /0229_from_stale_branch\.sql/);
assert.doesNotMatch(result.message, /- packages\/db\/src\/migrations\/0231_valid_but_after_stale\.sql/);
});
test('fails a new migration the classifier cannot prove safe under the running release', async () => {
const result = await checkMigrationCompatibility([
{ fileName: migration('0231_additive'), sql: 'ALTER TABLE "issues" ADD COLUMN "note" text;' },
{ fileName: migration('0232_plain_index'), sql: 'CREATE INDEX "issues_note_idx" ON "issues" ("note");' },
]);
assert.equal(result.passed, false);
assert.equal(result.errors.length, 1);
assert.match(result.errors[0], /0232_plain_index\.sql/);
assert.match(result.errors[0], /lock_hazard_create_index/);
assert.match(result.errors[0], /-- paperclip:migration-compat online_compatible: </);
assert.match(result.errors[0], /expand migration now and a contract migration in a later release/);
});
test('passes an unknown migration with a justified in-file override', async () => {
const result = await checkMigrationCompatibility([{
fileName: migration('0232_plain_index'),
sql: '-- paperclip:migration-compat online_compatible: issues stays under 10k rows on every stack\nCREATE INDEX "issues_note_idx" ON "issues" ("note");',
}]);
assert.equal(result.passed, true);
assert.match(result.notices[0], /online_compatible by override/);
});
test('fails an override that has no justification', async () => {
const result = await checkMigrationCompatibility([{
fileName: migration('0232_plain_index'),
sql: '-- paperclip:migration-compat online_compatible:\nCREATE INDEX "issues_note_idx" ON "issues" ("note");',
}]);
assert.equal(result.passed, false);
assert.match(result.errors[0], /needs a justification/);
});
test('fails a requires_staging migration without an override', async () => {
const result = await checkMigrationCompatibility([
{ fileName: migration('0233_drop'), sql: 'ALTER TABLE "issues" DROP COLUMN "note";' },
]);
assert.equal(result.passed, false);
assert.equal(result.errors.length, 1);
assert.match(result.errors[0], /0233_drop\.sql: requires_staging/);
assert.match(result.errors[0], /drop_column/);
assert.match(result.errors[0], /expand and contract migrations/);
assert.match(result.errors[0], /-- paperclip:migration-compat requires_staging: </);
});
test('passes a requires_staging migration with a justified in-file override', async () => {
const result = await checkMigrationCompatibility([{
fileName: migration('0233_drop'),
sql: '-- paperclip:migration-compat requires_staging: note is unused and ops accepts a maintenance deploy\nALTER TABLE "issues" DROP COLUMN "note";',
}]);
assert.equal(result.passed, true);
assert.deepEqual(result.errors, []);
assert.match(result.notices[0], /0233_drop\.sql: requires_staging by override/);
});
test('passes an online_compatible migration without a notice', async () => {
const result = await checkMigrationCompatibility([
{ fileName: migration('0233_add'), sql: 'ALTER TABLE "issues" ADD COLUMN "note" text;' },
]);
assert.deepEqual(result, { passed: true, errors: [], notices: [] });
});
test('passes when a PR adds no migrations', async () => {
assert.deepEqual(await checkMigrationCompatibility([]), { passed: true, errors: [], notices: [] });
});
const executorSource = 'const MIGRATIONS_WITH_TOP_LEVEL_STATEMENTS = new Set([\n "0232_listed.sql",\n]);';
const concurrentIndex = 'CREATE INDEX CONCURRENTLY IF NOT EXISTS "issues_note_idx" ON "issues" ("note");';
test('fails a CONCURRENTLY migration that the migrator would run in a transaction', async () => {
const result = await checkMigrationCompatibility(
[{ fileName: migration('0232_unlisted'), sql: concurrentIndex }, { fileName: migration('0232_listed'), sql: concurrentIndex }],
{ executorSource },
);
assert.equal(result.passed, false);
assert.equal(result.errors.length, 1);
assert.match(result.errors[0], /0232_unlisted\.sql: uses CONCURRENTLY/);
assert.match(result.errors[0], /Add "0232_unlisted\.sql" to MIGRATIONS_WITH_TOP_LEVEL_STATEMENTS in packages\/db\/src\/client\.ts/);
});
test('fails closed when the migrator source no longer names its non-transactional migrations', async () => {
const result = await checkMigrationCompatibility([], { executorSource: 'export const nothing = 1;' });
assert.equal(result.passed, false);
assert.match(result.errors[0], /MIGRATIONS_WITH_TOP_LEVEL_STATEMENTS was not found/);
});
test('treats CREATE TABLE IF NOT EXISTS as new only when the target branch never created the table', async () => {
const sql = 'CREATE TABLE IF NOT EXISTS "widgets" ("id" uuid);\nALTER TABLE "widgets" DROP COLUMN "title";';
const fresh = await checkMigrationCompatibility([{ fileName: migration('0231_widgets'), sql }], { priorMigrationSql: [] });
const existing = await checkMigrationCompatibility([{ fileName: migration('0231_widgets'), sql }], {
priorMigrationSql: ['CREATE TABLE "widgets" ("id" uuid, "title" text);'],
});
assert.equal(fresh.passed, true);
assert.equal(existing.passed, false);
assert.match(existing.errors[0], /0231_widgets\.sql: requires_staging/);
});