test(p1r): KnowledgeFlywayMigrationIT 版本断言动态化——消除 V→V 硬编码复发(Codex+Opus 合并方案)

原硬编码 TARGET_VERSION/TARGET_MIGRATION_COUNT=21,V22/V23(market 索引修复)加入后断言失败(第 2 次复发 V14→V21→V23)。改用本次 Flyway MigrateResult 的 targetSchemaVersion/migrationsExecuted 动态校验(随 sql/muse 增长自适应,无需再维护常量),并加 result.success/migrationsExecuted>0 守护;反假绿:仍校验迁移成功 + schema_history 计数与本次执行数一致 + Knowledge 表/索引/列存在(未改)。

方案来源:Codex(MigrateResult 字段,javap 验证 Flyway 11.7.2,避开 fork 指出的 info().all()/pending() 弃用)+ Opus fork(同样判定动态优于硬编码、count==executed 反假绿)双代理并行提案合并。验证:真实 PG(mini-infra)单测 1/1,target_schema_version=23 动态命中。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
lili 2026-06-15 20:53:06 -07:00
parent 338b33a406
commit 4d46d7ac8a

View File

@ -29,14 +29,12 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
* P1R-5 Knowledge / RAGFlow 真实 PostgreSQL / Flyway 迁移验收
*
* <p>本测试只接受显式传入的 Flyway URL/用户/位置参数与环境变量密码并强制数据库名以 _test 结尾
* 运行时会 clean 隔离测试库的 public schema再执行 sql/muse V1-V21 全量迁移</p>
* 运行时会 clean 隔离测试库的 public schema再执行 sql/muse V1 到最新版本全量迁移</p>
*/
class P1rKnowledgeFlywayMigrationIT {
// schema 已演进至 V21(V15-V21 为各域 events-publish-outbox 等迁移);Flyway 全量执行 sql/muse 下所有迁移,
// 故当前版本与成功迁移数随 schema 增长本测试聚焦验证 V14 引入的 Knowledge/RAGFlow 表与索引在全量迁移后仍存在
private static final String TARGET_VERSION = "21";
private static final int TARGET_MIGRATION_COUNT = 21;
// Flyway 全量执行 sql/muse 下所有迁移;当前版本与成功迁移数以本次 MigrateResult 为准(targetSchemaVersion/migrationsExecuted),
// 避免每加一条迁移(V22/V23)就改硬编码常量(历史已复发两次:V14V21V23)本测试聚焦验证 V14 引入的 Knowledge/RAGFlow 表与索引在全量迁移后仍存在
private static final List<String> REQUIRED_TABLES = List.of(
"muse_knowledge_command",
"muse_knowledge_processing_task",
@ -110,12 +108,16 @@ class P1rKnowledgeFlywayMigrationIT {
flyway.clean();
MigrateResult result = flyway.migrate();
assertTrue(result.success, "Flyway migrate 必须成功");
assertTrue(result.migrationsExecuted > 0, "必须从 clean 后的隔离库执行至少一个 SQL 迁移");
// 期望版本/数量动态取自本次 MigrateResult, sql/muse 增长自适应;反假绿仍靠:迁移成功 + history 计数一致 + 下方 Knowledge /索引/列存在
String targetVersion = Objects.requireNonNull(result.targetSchemaVersion, "Flyway 必须返回本次迁移目标版本");
MigrationInfo current = flyway.info().current();
assertEquals(TARGET_VERSION, Objects.requireNonNull(current, "必须存在当前 Flyway 版本").getVersion().getVersion(),
"真实 Flyway 当前版本必须到 V" + TARGET_VERSION);
assertEquals(targetVersion, Objects.requireNonNull(current, "必须存在当前 Flyway 版本").getVersion().getVersion(),
"真实 Flyway 当前版本必须到本次迁移目标 V" + targetVersion);
int successfulMigrationCount = successfulMigrationCount(url, user, password);
assertEquals(TARGET_MIGRATION_COUNT, successfulMigrationCount,
"必须在隔离库中执行 V1-V" + TARGET_VERSION + "" + TARGET_MIGRATION_COUNT + " 个成功 SQL 迁移");
assertEquals(result.migrationsExecuted, successfulMigrationCount,
"必须在隔离库中执行 V1-V" + targetVersion + "" + result.migrationsExecuted + " 个成功 SQL 迁移");
try (Connection connection = DriverManager.getConnection(url, user, password)) {
Map<String, Boolean> tableChecks = tableChecks(connection);
@ -135,7 +137,7 @@ class P1rKnowledgeFlywayMigrationIT {
System.out.println("flyway_locations_effective=" + effectiveLocations);
System.out.println("migrations_executed=" + result.migrationsExecuted);
System.out.println("successful_migration_count=" + successfulMigrationCount);
System.out.println("target_schema_version=" + TARGET_VERSION);
System.out.println("target_schema_version=" + targetVersion);
System.out.println("flyway_latest=" + current.getVersion() + ":" + current.getDescription());
System.out.println("schema_version=" + current.getVersion());
System.out.println("v14_tables=" + String.join(",", REQUIRED_TABLES));