Repository navigation
fix: make migrations 034 and 035 idempotent for partial reapply - #803
Merged
Merged
Conversation
…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).
@emdash-cms/admin
@emdash-cms/auth
@emdash-cms/blocks
@emdash-cms/cloudflare
emdash
create-emdash
@emdash-cms/gutenberg-to-portable-text
@emdash-cms/x402
@emdash-cms/plugin-ai-moderation
@emdash-cms/plugin-atproto
@emdash-cms/plugin-audit-log
@emdash-cms/plugin-color
@emdash-cms/plugin-embeds
@emdash-cms/plugin-forms
@emdash-cms/plugin-webhook-notifier
commit: |
Member
|
/review |
Contributor
|
LGTM! |
Merged
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_migrationsfailed (D1 subrequest limit, transient error mid-batch, Worker CPU limit), the next migration run reapplies the schema and crashes withindex 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_logdata.034_published_at_index.ts:CREATE INDEX→CREATE INDEX IF NOT EXISTSso re-creating an existing per-table index is a no-op.035_bounded_404_log.ts: capturehitsExistsonce before the column adds. GuardaddColumn("hits")andaddColumn("last_seen_at")withcolumnExistschecks. On rerun (whenhitsalready exists), skip the dedup rollupUPDATE/DELETEblock so live counters andlast_seen_ataren't reset back toCOUNT(*) = 1/created_at. Add.ifNotExists()to the new index creates and.ifExists()to the path-index drop (and the symmetric calls indown()).dialect-helpers.ts: addcolumnExistsandindexExistshelpers. Both branch onisPostgresand follow the same pattern as the existingtableExists. Usepragma_table_info(table)on SQLite,information_schema.columns/pg_indexeson Postgres.migrations.test.tsthat runs all migrations, deletes the rows for 034 and 035 from_emdash_migrations, and re-runsrunMigrationsagainst the partially-applied schema. Both names appear inapplied, no error is thrown, and the migration table count returns toMIGRATION_COUNT.Type of change
Checklist
pnpm typecheckpassespnpm lintpasses (no new diagnostics)pnpm testpasses (packages/core/tests/integration/database/migrations.test.ts15/15)pnpm formathas been runAI-generated code disclosure
Screenshots / test output
The new test exercises the partial-reapply path:
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.tsand worth its own issue. Per CLAUDE.md's "no drive-by changes" rule, that's not addressed here.