Skip to content

fix(migrations): make MySQL migrations safe to re-run after a partial failure - #28

Merged
CodingAnarchy merged 1 commit into
masterfrom
fix/idempotent-mysql-migrations
Oct 8, 2026
Merged

CodingAnarchy merged 1 commit into
masterfrom
fix/idempotent-mysql-migrations

Conversation

@CodingAnarchy

Copy link
Copy Markdown
Owner

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 with Duplicate column name or Duplicate key name.

Changes

  • MySQL migrations 002-009, 011, 013-016: every ADD COLUMN, CREATE INDEX, DROP INDEX and ADD/DROP CHECK is guarded by information_schema (columns, statistics, table_constraints) using the PREPARE/EXECUTE pattern migration 010 already used. Multi-column ALTERs are split into one guarded statement per column. 001, 010, 012 and 017 were already idempotent.
  • PostgreSQL (each migration already runs in a transaction, so it was all-or-nothing): added DROP CONSTRAINT IF EXISTS before ADD CONSTRAINT (007, 011, 012), IF NOT EXISTS on the 013 indexes, and guarded the one-way JSONB to UUID[] conversion in 012 so a replay does not drop the converted columns.
  • New tests/migration_idempotency_tests.rs (#[ignore], one test per backend): creates a uniquely named scratch database from MYSQL_DATABASE_URL / DATABASE_URL, runs all migrations, snapshots columns, indexes and constraints, deletes every row from hammerwork_migrations, runs all migrations again, compares the snapshots, and drops the database.
  • CHANGELOG entry under Fixed.

Verification

  • The schema produced on a fresh database is identical before and after this change (285 MySQL / 262 PostgreSQL snapshot lines, diffed).
  • The new tests fail on the old migrations and pass on the new ones.
  • cargo run -p cargo-hammerwork -- migration run succeeds on fresh MySQL and PostgreSQL databases.
  • cargo fmt --all --check and cargo clippy --workspace --all-targets --all-features -- -D warnings are clean.

🤖 Generated with Claude Code

… 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>
@CodingAnarchy
CodingAnarchy merged commit 5c4f0f7 into master Oct 8, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant