[fix] make V181 migration idempotent and self-repair flyway history - #4326
Merged
Aias00 merged 3 commits intoAug 18, 2026
Merged
Conversation
orangeCatDeveloper
force-pushed
the
fix/v181-migration-idempotent
branch
2 times, most recently
from
August 17, 2026 08:36
799aa74 to
811a2c0
Compare
Hibernate ddl-auto creates hzb_sop_schedule before Flyway runs, so the bare CREATE TABLE in V181 fails and the recorded failure blocks every later start until the history row is deleted by hand. Make the scripts idempotent and auto-repair the history for that one failure mode.
orangeCatDeveloper
force-pushed
the
fix/v181-migration-idempotent
branch
from
August 17, 2026 11:43
811a2c0 to
b013898
Compare
Aias00
approved these changes
Aug 17, 2026
Aias00
left a comment
Contributor
There was a problem hiding this comment.
评审结论:APPROVE
修复启动自锁的 Flyway 自修复方案,两部分互补且测试到位。
修复点确认:
- 脚本幂等化:MySQL 与 PostgreSQL 的
V181均改为CREATE TABLE IF NOT EXISTS/CREATE INDEX IF NOT EXISTS,消除 Hibernate(ddl-auto)先建表、Flyway 后跑 V181 导致的CREATE TABLE冲突。 - 启动自愈:
delayedFlywayInitializer捕获FlywayValidateException——消息含"failed migration"时执行flyway.repair()+ 仅重试一次migrate();其余校验错误(如 checksum mismatch)仍直接抛出交由人工处理。仅一次重试,无无限循环风险。
测试: FlywayConfigurationTest 用 mock 钉死两种路径——失败迁移触发 migrate→repair→migrate 顺序、checksum mismatch 不 repair 直接重抛。
非阻塞说明:
- 靠异常 message 文本匹配
"failed migration"属经验性做法;若 Flyway 未来改文案或本地化会失效。但测试已钉死当前确切文案,且兜底策略(不匹配就重抛)是安全的,可接受。
无阻塞问题,批准。
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.
Fixes #4325
On a fresh MySQL/PostgreSQL database the manager can lock itself out: the first boot fails halfway through migration V181, and every boot after that refuses to start — until someone hand-deletes a row from
flyway_schema_history.Root cause: two schema managers run at startup, Hibernate (
ddl-auto) first and Flyway second. Hibernate createshzb_sop_schedulefrom the entity, so V181's bareCREATE TABLEcollides. Flyway records that failure in its history table, and validation then rejects every subsequent start:The fix has two halves. The scripts become idempotent (
CREATE TABLE IF NOT EXISTS/CREATE INDEX IF NOT EXISTSin both mysql and postgresql variants), so racing Hibernate no longer fails the migration. And for instances already locked out,FlywayConfigurationcatches this one validation failure ("failed migration"), runsflyway.repair()and retries once — other validation errors (e.g. checksum mismatch) still propagate, since those need a human decision.FlywayConfigurationTestpins both behaviors with mocks (repair + retry ordering; checksum errors rethrown without repair).Verified against real MySQL 8 and PostgreSQL 15 (Hibernate-created table first, then Flyway with production baseline settings):