fix: make migrations 034 and 035 idempotent for partial reapply#803
Merged
ascorbic merged 1 commit intoemdash-cms:mainfrom Apr 28, 2026
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: |
Collaborator
|
/review |
Contributor
|
LGTM! |
Merged
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.