Skip to content

fix: make migrations 034 and 035 idempotent for partial reapply - #803

Merged
ascorbic merged 1 commit into
emdash-cms:mainfrom
mvanhorn:osc/788-idempotent-migrations
Apr 28, 2026
Merged

ascorbic merged 1 commit into
emdash-cms:mainfrom
mvanhorn:osc/788-idempotent-migrations

Conversation

@mvanhorn

Copy link
Copy Markdown
Contributor

What does this PR do?

Closes #788

Migrations 034 and 035 weren't safe to rerun against a partially-applied schema. If a previous run's schema commit succeeded but the row write to _emdash_migrations failed (D1 subrequest limit, transient error mid-batch, Worker CPU limit), the next migration run reapplies the schema and crashes with index already exists: SQLITE_ERROR (migration: 034_published_at_index) — which is the exact failure the reporter hit upgrading from 0.1.1 to 0.6.0/0.7.0.

This makes both migrations safe to re-run on a partially-applied schema while preserving accumulated _emdash_404_log data.

  • 034_published_at_index.ts: CREATE INDEX → CREATE INDEX IF NOT EXISTS so re-creating an existing per-table index is a no-op.
  • 035_bounded_404_log.ts: capture hitsExists once before the column adds. Guard addColumn("hits") and addColumn("last_seen_at") with columnExists checks. On rerun (when hits already exists), skip the dedup rollup UPDATE/DELETE block so live counters and last_seen_at aren't reset back to COUNT(*) = 1 / created_at. Add .ifNotExists() to the new index creates and .ifExists() to the path-index drop (and the symmetric calls in down()).
  • dialect-helpers.ts: add columnExists and indexExists helpers. Both branch on isPostgres and follow the same pattern as the existing tableExists. Use pragma_table_info(table) on SQLite, information_schema.columns / pg_indexes on Postgres.
  • Added integration test migrations.test.ts that runs all migrations, deletes the rows for 034 and 035 from _emdash_migrations, and re-runs runMigrations against the partially-applied schema. Both names appear in applied, no error is thrown, and the migration table count returns to MIGRATION_COUNT.
  • Patch-level changeset.

Type of change

  • Bug fix
  • Feature (requires maintainer-approved Discussion)
  • Refactor (no behavior change)
  • Translation
  • Documentation
  • Performance improvement
  • Tests
  • Chore (dependencies, CI, tooling)

Checklist

  • I have read CONTRIBUTING.md
  • pnpm typecheck passes
  • pnpm lint passes (no new diagnostics)
  • pnpm test passes (packages/core/tests/integration/database/migrations.test.ts 15/15)
  • pnpm format has been run
  • I have added/updated tests for my changes (if applicable)
  • User-visible strings in the admin UI are wrapped for translation (n/a)
  • I have added a changeset (if this PR changes a published package)
  • New features link to an approved Discussion: n/a (bug fix)

AI-generated code disclosure

  • This PR includes AI-generated code

Screenshots / test output

Test Files  1 passed (1)
     Tests  15 passed (15)
  Duration  341ms

The new test exercises the partial-reapply path:

it("should re-run migrations 034 and 035 when schema changes were partially applied", async () => {
    await db.destroy();
    db = await setupTestDatabaseWithCollections();

    await db
        .deleteFrom("_emdash_migrations")
        .where("name", "in", ["034_published_at_index", "035_bounded_404_log"])
        .execute();

    const { applied } = await runMigrations(db);

    expect(applied).toContain("034_published_at_index");
    expect(applied).toContain("035_bounded_404_log");

    const migrations = await db.selectFrom("_emdash_migrations").selectAll().execute();
    expect(migrations).toHaveLength(MIGRATION_COUNT);
});

Out of scope

The middleware behavior the reporter also flagged (auth middleware swallowing the migration error and falling through to a permissionless ghost user) is a separate failure mode in auth.ts and worth its own issue. Per CLAUDE.md's "no drive-by changes" rule, that's not addressed here.

…sh-cms#788)

When an earlier migration attempt commits its schema changes but the row
write to _emdash_migrations fails (e.g. D1 subrequest limit, transient
error mid-batch), the next migration run reapplies the schema and crashes
with "index already exists" or "duplicate column".

Make 034 and 035 safe to re-run on a partially-applied schema:

- 034: CREATE INDEX -> CREATE INDEX IF NOT EXISTS
- 035: guard addColumn with columnExists; ifNotExists on createIndex;
  ifExists on dropIndex (both up and down). Capture hitsExists before
  schema changes; skip the dedup rollup/delete on rerun so existing 404
  counters and last_seen_at values are preserved.
- Add columnExists and indexExists helpers in dialect-helpers.ts.
- Add an integration test for the idempotent re-run case.
- Add a changeset (patch).
@pkg-pr-new

pkg-pr-new Bot commented Apr 28, 2026

Copy link
Copy Markdown

Open in StackBlitz

@emdash-cms/admin

npm i https://pkg.pr.new/@emdash-cms/admin@803

@emdash-cms/auth

npm i https://pkg.pr.new/@emdash-cms/auth@803

@emdash-cms/blocks

npm i https://pkg.pr.new/@emdash-cms/blocks@803

@emdash-cms/cloudflare

npm i https://pkg.pr.new/@emdash-cms/cloudflare@803

emdash

npm i https://pkg.pr.new/emdash@803

create-emdash

npm i https://pkg.pr.new/create-emdash@803

@emdash-cms/gutenberg-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/gutenberg-to-portable-text@803

@emdash-cms/x402

npm i https://pkg.pr.new/@emdash-cms/x402@803

@emdash-cms/plugin-ai-moderation

npm i https://pkg.pr.new/@emdash-cms/plugin-ai-moderation@803

@emdash-cms/plugin-atproto

npm i https://pkg.pr.new/@emdash-cms/plugin-atproto@803

@emdash-cms/plugin-audit-log

npm i https://pkg.pr.new/@emdash-cms/plugin-audit-log@803

@emdash-cms/plugin-color

npm i https://pkg.pr.new/@emdash-cms/plugin-color@803

@emdash-cms/plugin-embeds

npm i https://pkg.pr.new/@emdash-cms/plugin-embeds@803

@emdash-cms/plugin-forms

npm i https://pkg.pr.new/@emdash-cms/plugin-forms@803

@emdash-cms/plugin-webhook-notifier

npm i https://pkg.pr.new/@emdash-cms/plugin-webhook-notifier@803

commit: 1f6e22c

@ascorbic

Copy link
Copy Markdown
Member

/review

@ask-bonk

ask-bonk Bot commented Apr 28, 2026

Copy link
Copy Markdown
Contributor

LGTM!

github run

@ascorbic ascorbic left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@ascorbic
ascorbic merged commit 0041d76 into emdash-cms:main Apr 28, 2026
29 checks passed
@emdashbot emdashbot Bot mentioned this pull request Apr 28, 2026
@ask-bonk ask-bonk Bot mentioned this pull request May 10, 2026
9 of 18 tasks
ascorbic pushed a commit that referenced this pull request May 10, 2026
* test: reproduce migration 016 failing on partial reapply (#954)

Reproduces the symptom from #954: when migration 016's up() partially
completes — for example, the first createTable succeeds but a later
statement crashes due to a D1 subrequest limit, isolate cancellation,
or transient connection error — the migration record never gets
inserted into _emdash_migrations. On the next request, Kysely sees 016
still pending and re-runs up() from the top, which currently crashes
with 'table _emdash_api_tokens already exists'.

The new tests directly invoke up() twice, and also simulate the
partial-apply state by pre-creating just the first table 016 owns.
Both currently fail; the next commit makes 016 idempotent so they
pass.

* fix(migrations): make 016_api_tokens idempotent on partial reapply (#954)

When migration 016's up() crashes mid-way -- D1 subrequest limit,
isolate cancellation, transient connection error -- the migration
record never gets inserted into _emdash_migrations. Kysely's next
attempt re-runs up() from the top and crashes with 'table
_emdash_api_tokens already exists', blocking every subsequent boot
of the Worker. This is the failure path the user hit on a fresh D1
database: setup wizard runs, the first table creates, something
later in 016 fails, and the deploy is wedged.

Adds .ifNotExists() to every CREATE TABLE and CREATE INDEX in 016's
up() and .ifExists() to the symmetric drops in down(). The retry now
treats already-applied steps as no-ops and proceeds to create the
remaining tables, matching the recovery pattern from #803 (migrations
034 and 035).

* chore: changeset for #954 migration 016 idempotency fix

* test: assert recovered schema integrity in 016 partial-reapply test

Addresses adversarial review feedback: the original partial-apply test
hand-rolled an _emdash_api_tokens schema that was meant to mimic the
state left by a crashed up(). Because CREATE TABLE IF NOT EXISTS over
an existing table is a no-op even when the new definition differs, a
schema drift in the migration would leave the test silently passing
while the recovery path no longer matched what we shipped.

Reuses the migration's own up() to set up the partial state, then
drops only the trailing tables before retrying. Adds explicit
assertions on the recovered table's columns and 016's indexes, so a
future change to the migration that drops a column or index gets
caught here rather than slipping through.

---------

Co-authored-by: ask-bonk[bot] <ask-bonk[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Migration runner crashes on partially-applied schema and the middleware silently degrades the admin to a permissionless "ghost" user

2 participants