Repository navigation
fix(migrations): make MySQL migrations safe to re-run after a partial failure - #28
Merged
Merged
Conversation
… failure MySQL DDL auto-commits, so a migration that failed part-way stayed half-applied and every retry failed with "Duplicate column name" / "Duplicate key name". Guard each ADD COLUMN, CREATE INDEX, DROP INDEX and ADD/DROP CHECK in migrations 002-016 against information_schema (PREPARE/EXECUTE, as migration 010 already does), one statement per column. The resulting schema is unchanged. On PostgreSQL, guard the remaining non-idempotent statements (ADD CONSTRAINT in 007/011/012, indexes in 013, and the one-way JSONB-to-array conversion in 012). Add migration_idempotency_tests: migrate a scratch database, forget every recorded migration, migrate again and compare the schema, for both backends. Part of #7 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Oct 8, 2026
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.
Part of #7
MySQL DDL auto-commits, so a migration that fails part-way keeps its earlier statements but is not recorded in
hammerwork_migrations. Re-running then failed withDuplicate column nameorDuplicate key name.Changes
ADD COLUMN,CREATE INDEX,DROP INDEXandADD/DROP CHECKis guarded byinformation_schema(columns,statistics,table_constraints) using thePREPARE/EXECUTEpattern migration 010 already used. Multi-columnALTERs are split into one guarded statement per column. 001, 010, 012 and 017 were already idempotent.DROP CONSTRAINT IF EXISTSbeforeADD CONSTRAINT(007, 011, 012),IF NOT EXISTSon the 013 indexes, and guarded the one-way JSONB toUUID[]conversion in 012 so a replay does not drop the converted columns.tests/migration_idempotency_tests.rs(#[ignore], one test per backend): creates a uniquely named scratch database fromMYSQL_DATABASE_URL/DATABASE_URL, runs all migrations, snapshots columns, indexes and constraints, deletes every row fromhammerwork_migrations, runs all migrations again, compares the snapshots, and drops the database.Verification
cargo run -p cargo-hammerwork -- migration runsucceeds on fresh MySQL and PostgreSQL databases.cargo fmt --all --checkandcargo clippy --workspace --all-targets --all-features -- -D warningsare clean.🤖 Generated with Claude Code