94f160b316
V143 已于 2026-10-04 10:48:54 应用到生产库(校验和 1539570901),而 spring.flyway.validate-on-migrate=true 是硬编码 —— 想摘 DEFAULT 就不能原地改 V143, 否则下一次启动校验失败、两台 java 都起不来。所以按前向修复写成 V144,并让 CompanyMigrationV144Test 按 Flyway 的 CRC32 复算 V142/V143 与生产存量校验和比对, 把这件事从口头纪律变成 mvn test 阶段能咬红的护栏。 取舍:宁可让绕过 createUser 的写入撞 MySQL 1364 炸在日志里,也不要被静默塞进 厦门米椿花(users 全仓只有 1 条 INSERT,且它恒显式赋值,故不会新增失败面)。 - V144:ALTER TABLE users MODIFY COLUMN company_id bigint NOT NULL(不带 DEFAULT), 头注释含模板六项 + 验证 SQL(期望 Default = NULL)+ 人工回滚步骤 - CompanyMigrationV144Test 8 例,变异自证 7/7 被咬死(含「原地改 V143」那条) - CompanyMigrationV143Test 删掉「V143 必须是当前最大版本」——V144 落盘后它是假前提, 号段连续性仍由 MigrationInventoryTest 把守 - migration-inventory.md:文件数 143、范围 V1..V144、取号起点 V145、新增存量校验和表
437 lines
23 KiB
Java
437 lines
23 KiB
Java
package com.nanri.aiimage.config;
|
||
|
||
import org.junit.jupiter.api.Test;
|
||
|
||
import java.io.IOException;
|
||
import java.nio.charset.StandardCharsets;
|
||
import java.nio.file.Files;
|
||
import java.nio.file.Path;
|
||
import java.util.ArrayList;
|
||
import java.util.List;
|
||
import java.util.regex.Matcher;
|
||
import java.util.regex.Pattern;
|
||
import java.util.stream.Stream;
|
||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||
|
||
/**
|
||
* 多公司一期 V143 迁移契约(设计文档 docs/ops/company-multi-tenant-design-2026-10-04.md §3/§6)。
|
||
*
|
||
* 本环境无 H2 / testcontainers(见 pom.xml),迁移不能真跑,因此按仓库既有口径做文件审计
|
||
* (对照 MigrationInventoryTest / IndexMigrationV101Test)。与它们不同的是:断言一律打在
|
||
* **剥掉注释后的可执行 SQL** 上,避免"注释里写了这句话就算通过";解析用的正则不匹配即红。
|
||
*/
|
||
class CompanyMigrationV143Test {
|
||
|
||
private static final Path ROOT = Path.of(System.getProperty("user.dir"));
|
||
private static final Path DB_DIR = ROOT.resolve("src/main/resources/db");
|
||
private static final Pattern VERSION_FILE = Pattern.compile("^V(\\d+)(?:_(\\d+))?__.*\\.sql$");
|
||
private static final String ROLLBACK_RELATIVE = "docs/rollback-V143-company.sql";
|
||
|
||
// ---------------------------------------------------------------- 取文件
|
||
|
||
private static List<Path> versionFiles() throws IOException {
|
||
try (Stream<Path> s = Files.list(DB_DIR)) {
|
||
return s.filter(p -> VERSION_FILE.matcher(p.getFileName().toString()).matches())
|
||
.sorted()
|
||
.toList();
|
||
}
|
||
}
|
||
|
||
private static int versionOf(Path p) {
|
||
Matcher m = VERSION_FILE.matcher(p.getFileName().toString());
|
||
return m.matches() ? Integer.parseInt(m.group(1)) : -1;
|
||
}
|
||
|
||
private static int maxVersionOnDisk() throws IOException {
|
||
return versionFiles().stream().mapToInt(CompanyMigrationV143Test::versionOf).max().orElse(0);
|
||
}
|
||
|
||
private static Path migration() throws IOException {
|
||
List<Path> hits = versionFiles().stream()
|
||
.filter(p -> p.getFileName().toString().startsWith("V143__"))
|
||
.toList();
|
||
assertFalse(hits.isEmpty(), "缺少 V143__*.sql:多公司迁移还没落盘");
|
||
assertEquals(1, hits.size(), "V143 只允许一个迁移文件(撞号会让 Flyway 启动失败)");
|
||
return hits.get(0);
|
||
}
|
||
|
||
private static String rawMigration() throws IOException {
|
||
return Files.readString(migration(), StandardCharsets.UTF_8);
|
||
}
|
||
|
||
private static String statements() throws IOException {
|
||
return normalize(stripComments(rawMigration()));
|
||
}
|
||
|
||
// ------------------------------------------------------------ SQL 预处理
|
||
|
||
/** 去掉 -- 行注释与块注释,保留单引号字符串字面量(避免把字面量里的 -- 当注释吃掉)。 */
|
||
private static String stripComments(String sql) {
|
||
StringBuilder out = new StringBuilder(sql.length());
|
||
int i = 0;
|
||
while (i < sql.length()) {
|
||
char c = sql.charAt(i);
|
||
if (c == '\'') {
|
||
int end = i + 1;
|
||
while (end < sql.length()) {
|
||
if (sql.charAt(end) == '\'') {
|
||
if (end + 1 < sql.length() && sql.charAt(end + 1) == '\'') {
|
||
end += 2;
|
||
continue;
|
||
}
|
||
break;
|
||
}
|
||
end++;
|
||
}
|
||
out.append(sql, i, Math.min(end + 1, sql.length()));
|
||
i = end + 1;
|
||
continue;
|
||
}
|
||
if (c == '-' && i + 1 < sql.length() && sql.charAt(i + 1) == '-') {
|
||
while (i < sql.length() && sql.charAt(i) != '\n') {
|
||
i++;
|
||
}
|
||
continue;
|
||
}
|
||
if (c == '/' && i + 1 < sql.length() && sql.charAt(i + 1) == '*') {
|
||
int end = sql.indexOf("*/", i + 2);
|
||
i = end < 0 ? sql.length() : end + 2;
|
||
continue;
|
||
}
|
||
out.append(c);
|
||
i++;
|
||
}
|
||
return out.toString();
|
||
}
|
||
|
||
private static String normalize(String sql) {
|
||
return sql.replaceAll("\\s+", " ").trim();
|
||
}
|
||
|
||
/** 按顶层逗号切分(跳过括号内与字符串内的逗号)。 */
|
||
private static List<String> splitTopLevel(String s) {
|
||
List<String> parts = new ArrayList<>();
|
||
StringBuilder cur = new StringBuilder();
|
||
int depth = 0;
|
||
boolean inQuote = false;
|
||
for (int i = 0; i < s.length(); i++) {
|
||
char c = s.charAt(i);
|
||
if (inQuote) {
|
||
cur.append(c);
|
||
if (c == '\'') {
|
||
if (i + 1 < s.length() && s.charAt(i + 1) == '\'') {
|
||
cur.append(s.charAt(++i));
|
||
} else {
|
||
inQuote = false;
|
||
}
|
||
}
|
||
continue;
|
||
}
|
||
if (c == '\'') {
|
||
inQuote = true;
|
||
cur.append(c);
|
||
continue;
|
||
}
|
||
if (c == '(') {
|
||
depth++;
|
||
} else if (c == ')') {
|
||
depth--;
|
||
} else if (c == ',' && depth == 0) {
|
||
parts.add(cur.toString().trim());
|
||
cur.setLength(0);
|
||
continue;
|
||
}
|
||
cur.append(c);
|
||
}
|
||
if (!cur.toString().trim().isEmpty()) {
|
||
parts.add(cur.toString().trim());
|
||
}
|
||
return parts;
|
||
}
|
||
|
||
private static void assertMatches(String sql, String regex, String message) {
|
||
assertTrue(Pattern.compile(regex).matcher(sql).find(), message + "(未匹配到 " + regex + ")");
|
||
}
|
||
|
||
// ------------------------------------------------------------------ 用例
|
||
|
||
/**
|
||
* 破坏点:版本号取错。V132/V133 是永久退役洞,取到它们或取到已被占用的号,
|
||
* Flyway 启动期直接 "resolved migration not applied" / 撞号失败。
|
||
*/
|
||
@Test
|
||
void migrationIsVersionedOneFourThree() throws IOException {
|
||
Path file = migration();
|
||
assertEquals(143, versionOf(file), "多公司迁移必须是 V143(V142 之后的下一号)");
|
||
assertTrue(file.getFileName().toString().matches("^V143__[A-Za-z0-9_.-]+$"),
|
||
"命名应 V143__<snake 描述>.sql: " + file.getFileName());
|
||
// 这里原本还断言"V143 必须是当前最大版本"。V143 已在 2026-10-04 10:48 落库、字节封死,
|
||
// 后续口径变更只能写成 V144 及以后 ⇒ 那条断言变成了假前提。号段连续性与撞号仍由
|
||
// MigrationInventoryTest(integerVersionsSequentialFromOne / 无重复文件名)把守。
|
||
}
|
||
|
||
/** 破坏点:头注释缺项 → 上线评审拿不到锁表风险与回滚口径。 */
|
||
@Test
|
||
void headerCarriesSixMandatoryTemplateFields() throws IOException {
|
||
String raw = rawMigration();
|
||
assertTrue(raw.contains("V143"), "头注释应标明迁移版本 V143");
|
||
for (int i = 1; i <= 6; i++) {
|
||
assertTrue(raw.contains(i + ". "), "头注释应含模板第 " + i + " 项必填");
|
||
}
|
||
for (String field : List.of("变更目的", "影响表与数据量", "锁表风险", "验证 SQL", "回滚步骤", "上线窗口")) {
|
||
assertTrue(raw.contains(field), "头注释应含必填项:" + field);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 破坏点:company_name 不唯一。公司名是二期登录定位({@code UNIQUE(company_id, username)})
|
||
* 与后台公司下拉的唯一人类可读标识,重名会让"选公司"这一步不可判定。
|
||
*/
|
||
@Test
|
||
void createsCompanyTableWithUniqueCompanyName() throws IOException {
|
||
String sql = statements();
|
||
assertMatches(sql, "CREATE TABLE (IF NOT EXISTS )?`company`\\s*\\(", "应新建 company 主表");
|
||
assertMatches(sql, "`company_name`\\s+varchar\\(\\d+\\)[^,]*NOT NULL", "company_name 应为非空 varchar");
|
||
assertMatches(sql, "UNIQUE INDEX `uk_company_name`\\s*\\(\\s*`company_name`",
|
||
"company_name 上应有唯一索引 uk_company_name");
|
||
assertMatches(sql, "PRIMARY KEY \\(`id`\\)", "company 应有主键 id");
|
||
}
|
||
|
||
/**
|
||
* 破坏点:初始公司 id 不是字面量 1。users.company_id 的 DEFAULT 只能是字面量
|
||
* (MySQL 不允许 DEFAULT 子查询),回填也写死 = 1;id 一旦不是 1,
|
||
* 混合版本窗口里旧 jar 建的号与全部存量账号都会指向一个不存在的公司。
|
||
*/
|
||
@Test
|
||
void seedsInitialCompanyPinnedToIdOne() throws IOException {
|
||
String sql = statements();
|
||
Matcher m = Pattern.compile("INSERT(?: IGNORE)? INTO `company`\\s*\\(([^)]*)\\)\\s*VALUES\\s*\\((.*?)\\)\\s*;")
|
||
.matcher(sql);
|
||
assertTrue(m.find(), "应有一条显式插入初始公司的 INSERT(未匹配到 INSERT INTO `company` (...) VALUES (...);)");
|
||
|
||
List<String> columns = splitTopLevel(m.group(1)).stream()
|
||
.map(c -> c.replace("`", "").trim())
|
||
.toList();
|
||
List<String> values = splitTopLevel(m.group(2));
|
||
assertEquals(columns.size(), values.size(),
|
||
"INSERT 的列数与值数应一致,否则解析口径变了、本用例的断言已失效");
|
||
|
||
int idIdx = columns.indexOf("id");
|
||
assertTrue(idIdx >= 0, "初始公司必须显式指定 id 列,不能交给 AUTO_INCREMENT");
|
||
assertEquals("1", values.get(idIdx), "初始公司 id 必须钉死为 1(users.company_id DEFAULT 1 依赖它)");
|
||
|
||
int nameIdx = columns.indexOf("company_name");
|
||
assertTrue(nameIdx >= 0, "初始公司必须写入 company_name");
|
||
assertEquals("'厦门米椿花'", values.get(nameIdx), "存量数据归属的初始公司名");
|
||
|
||
int statusIdx = columns.indexOf("status");
|
||
assertTrue(statusIdx >= 0, "初始公司必须写入 status");
|
||
assertEquals("1", values.get(statusIdx), "初始公司应为启用状态");
|
||
}
|
||
|
||
/**
|
||
* 破坏点:users 没挂 company_id 或没建索引。公司维度全靠 user_id → users.company_id 派生,
|
||
* 缺列则整个方案不成立;缺索引则二期每次 scope 展开都要全表扫 users。
|
||
* 另一半破坏点是"顺手给业务表也加了 company_id"——那与定案(数据按账号归属、公司维度跟着账号走)
|
||
* 相反,且会把一期从"业务表零 DDL"变成 57 张表的回填工程。
|
||
*/
|
||
@Test
|
||
void addsUsersCompanyIdColumnAndIndex() throws IOException {
|
||
String sql = statements();
|
||
assertMatches(sql, "ALTER TABLE `users`\\s+ADD COLUMN `company_id`\\s+bigint",
|
||
"users 应新增 bigint 列 company_id");
|
||
assertMatches(sql, "ADD INDEX `idx_users_company_id`\\s*\\(\\s*`company_id`",
|
||
"company_id 上应建索引 idx_users_company_id");
|
||
|
||
List<String> altered = new ArrayList<>();
|
||
Matcher alter = Pattern.compile("ALTER TABLE `([A-Za-z0-9_]+)`").matcher(sql);
|
||
while (alter.find()) {
|
||
altered.add(alter.group(1));
|
||
}
|
||
assertFalse(altered.isEmpty(), "应有 ALTER TABLE 语句(否则本用例的前两条断言是被注释喂绿的)");
|
||
assertEquals(List.of("users"), altered.stream().distinct().toList(),
|
||
"一期业务表零 DDL:只允许改 users,实际改到了 " + altered);
|
||
|
||
List<String> created = new ArrayList<>();
|
||
Matcher create = Pattern.compile("CREATE TABLE (?:IF NOT EXISTS )?`([A-Za-z0-9_]+)`").matcher(sql);
|
||
while (create.find()) {
|
||
created.add(create.group(1));
|
||
}
|
||
assertEquals(List.of("company"), created, "只应新建 company 一张表,实际 " + created);
|
||
}
|
||
|
||
/**
|
||
* 破坏点:回填缺失或顺序颠倒。
|
||
* 顺序颠倒(先 MODIFY NOT NULL 再 UPDATE)在严格模式下直接 ER_INVALID_USE_OF_NULL 让迁移失败;
|
||
* 回填缺失而 NOT NULL 闸门也缺失,则存量账号 company_id 静默为 NULL——公司维度对老数据整体失明。
|
||
*/
|
||
@Test
|
||
void backfillsExistingAccountsBeforeNotNullGate() throws IOException {
|
||
String sql = statements();
|
||
Pattern backfill = Pattern.compile(
|
||
"UPDATE `users`\\s+SET\\s+`company_id`\\s*=\\s*1\\s+WHERE\\s+`company_id`\\s+IS\\s+NULL");
|
||
Pattern gate = Pattern.compile(
|
||
"ALTER TABLE `users`\\s+MODIFY COLUMN `company_id`\\s+bigint\\s+NOT NULL\\s+DEFAULT 1");
|
||
|
||
Matcher b = backfill.matcher(sql);
|
||
Matcher g = gate.matcher(sql);
|
||
assertTrue(b.find(), "应把存量账号回填到公司 1(厦门米椿花)");
|
||
assertTrue(g.find(), "应有 MODIFY ... NOT NULL 自检闸门:回填漏行时让迁移失败,而不是留一堆 NULL 静默通过");
|
||
assertTrue(b.start() < g.start(), "回填必须在 NOT NULL 闸门之前,否则闸门会先撞上存量 NULL");
|
||
}
|
||
|
||
/**
|
||
* 破坏点:DEFAULT 1 被提前摘掉。滚动发布期间一台新 jar、一台旧 jar,/api 经 clb 打到两台;
|
||
* 旧 jar 的 AdminUserService.create 不给 company_id 赋值,列若无 DEFAULT,
|
||
* 严格模式报 "Field 'company_id' doesn't have a default value",打到旧节点的建号请求全失败(约一半)。
|
||
*/
|
||
@Test
|
||
void notNullGateKeepsTemporaryDefaultOne() throws IOException {
|
||
String sql = statements();
|
||
assertMatches(sql, "MODIFY COLUMN `company_id`\\s+bigint\\s+NOT NULL\\s+DEFAULT 1",
|
||
"company_id 应落为 NOT NULL DEFAULT 1(DEFAULT 只为混合版本窗口存在,摘除时机由启动期巡检盯)");
|
||
assertTrue(rawMigration().contains("混合版本"),
|
||
"头注释应写清 DEFAULT 的存在理由与摘除时机,否则后人会当它是永久默认值");
|
||
}
|
||
|
||
/**
|
||
* 破坏点:菜单行插重、挂错父级、route_path 与前端路由不一致。
|
||
* route_path 必须与 admin-frontend-vue/src/router/routes.ts 的 path 逐字一致,否则菜单点进去 404;
|
||
* 非幂等写法在重跑或补数时会插出两行"公司管理"。
|
||
*/
|
||
@Test
|
||
void insertsAdminCompaniesMenuIdempotently() throws IOException {
|
||
String sql = statements();
|
||
Matcher m = Pattern.compile("INSERT INTO `columns`\\s*\\(([^)]*)\\)(.*?);").matcher(sql);
|
||
assertTrue(m.find(), "应插入后台「公司管理」菜单(未匹配到 INSERT INTO `columns`)");
|
||
|
||
List<String> columns = splitTopLevel(m.group(1)).stream()
|
||
.map(c -> c.replace("`", "").trim())
|
||
.toList();
|
||
for (String required : List.of("name", "column_key", "menu_type", "route_path", "parent_id")) {
|
||
assertTrue(columns.contains(required), "菜单插入应显式给出列 " + required);
|
||
}
|
||
assertTrue(columns.contains("sort_order"), "菜单插入应给出 sort_order,否则同级顺序不确定");
|
||
|
||
String body = m.group(2);
|
||
assertTrue(body.contains("'admin_companies'"), "column_key 必须是 admin_companies(前端 menuKey 与权限判定都用它)");
|
||
assertTrue(body.contains("'account/companies'"), "route_path 必须是 account/companies(与前端路由逐字一致)");
|
||
assertTrue(body.contains("'admin_group_account'"), "父级应取「账号与权限」分组 admin_group_account");
|
||
assertMatches(body, "NOT EXISTS\\s*\\(", "菜单插入必须幂等:按 column_key 判存在");
|
||
assertTrue(body.contains("SELECT"), "菜单插入应为 INSERT ... SELECT ... WHERE NOT EXISTS,裸 VALUES 重跑必重行");
|
||
}
|
||
|
||
/**
|
||
* 破坏点:迁移不可重跑。MySQL 的 DDL 不进事务 —— V143 若在 NOT NULL 闸门处失败,
|
||
* 前面的 CREATE TABLE / INSERT / ADD COLUMN 已经落库。按 §8.1 的恢复步骤
|
||
* (补齐漏行 → flyway repair → 重启)Flyway 会**从第一条语句重跑整个文件**,
|
||
* 届时非幂等语句依次报 1050 table exists / 1060 duplicate column / 1062 duplicate entry,
|
||
* 把"补数据后重启"变成"必须手工清库"。
|
||
*/
|
||
@Test
|
||
void migrationIsRerunnableAfterPartialFailure() throws IOException {
|
||
String sql = statements();
|
||
assertMatches(sql, "CREATE TABLE IF NOT EXISTS `company`",
|
||
"建表必须带 IF NOT EXISTS:重跑时表已在库里");
|
||
assertMatches(sql, "INSERT IGNORE INTO `company`",
|
||
"初始公司必须 INSERT IGNORE(id 钉死 + 公司名唯一,重复即本次重跑)");
|
||
assertMatches(sql, "information_schema\\.COLUMNS",
|
||
"ADD COLUMN 必须先查 information_schema 判存在(对齐 V142 的条件 DDL 写法)");
|
||
assertMatches(sql, "TABLE_NAME\\s*=\\s*'users'", "存在性检查要按 TABLE_NAME='users' 筛");
|
||
assertMatches(sql, "COLUMN_NAME\\s*=\\s*'company_id'", "存在性检查要按 COLUMN_NAME='company_id' 筛");
|
||
assertMatches(sql, "SET\\s+@sql\\s*:=\\s*IF\\s*\\(\\s*@col_exists\\s*=\\s*0",
|
||
"条件 DDL 必须真的用存在性标志分支:查了 information_schema 却无条件执行 ALTER,重跑照样报 1060");
|
||
assertMatches(sql, "PREPARE\\s+stmt\\s+FROM", "条件 DDL 要用 PREPARE/EXECUTE 执行");
|
||
assertMatches(sql, "DEALLOCATE\\s+PREPARE\\s+stmt", "游标语句用完必须 DEALLOCATE,否则同名 stmt 二次 PREPARE 报错");
|
||
assertMatches(sql, "INSERT INTO `columns`[\\s\\S]*NOT EXISTS\\s*\\(\\s*SELECT",
|
||
"菜单行必须按 column_key 判存在后再插");
|
||
}
|
||
|
||
/**
|
||
* 破坏点:文件内没留验证 SQL。上线的人只能凭"启动没报错"判断迁移成功,
|
||
* 而本迁移最需要核实的三件事(回填有没有漏行、NOT NULL 与 DEFAULT 有没有真落上、
|
||
* 菜单挂对父级没有)都不体现在启动日志里。
|
||
*/
|
||
@Test
|
||
void migrationShipsPostDeployVerificationSql() throws IOException {
|
||
String raw = rawMigration();
|
||
assertTrue(raw.contains("验证 SQL"), "头注释应含「验证 SQL」段(模板第 4 项)");
|
||
assertTrue(raw.contains("company_id IS NULL"),
|
||
"验证 SQL 应能查出回填漏行的计数,这是 NOT NULL 闸门之外唯一的复核手段");
|
||
assertTrue(raw.contains("SHOW COLUMNS FROM `users` LIKE 'company_id'")
|
||
|| raw.contains("SHOW COLUMNS FROM users LIKE 'company_id'"),
|
||
"验证 SQL 应核对 company_id 的 Null / Default,DEFAULT 1 被摘没摘一眼可见");
|
||
assertTrue(raw.contains("admin_companies"), "验证 SQL 应核对「公司管理」菜单行");
|
||
}
|
||
|
||
/**
|
||
* 破坏点:回滚被写成 Flyway 版本化迁移(例如 V144__rollback_company.sql)放进 src/main/resources/db。
|
||
* 那样它会在下次启动时被 Flyway 自动执行,静默把 V143 撤销掉,同时打乱版本连续性审计。
|
||
* 回滚必须是扫描路径之外、由人按顺序执行的补丁脚本。
|
||
*/
|
||
@Test
|
||
void rollbackLivesOutsideFlywayScanPath() throws IOException {
|
||
Path rollback = ROOT.resolve(ROLLBACK_RELATIVE);
|
||
assertTrue(Files.isRegularFile(rollback),
|
||
"回滚脚本应存在于 " + ROLLBACK_RELATIVE + "(Flyway 扫描路径之外),否则事故时无从回退");
|
||
|
||
String rb = normalize(stripComments(Files.readString(rollback, StandardCharsets.UTF_8)));
|
||
assertMatches(rb, "ALTER TABLE `users`\\s+DROP INDEX `idx_users_company_id`", "回滚应删掉索引");
|
||
assertMatches(rb, "DROP COLUMN `company_id`", "回滚应删掉 users.company_id");
|
||
assertMatches(rb, "DROP TABLE (IF EXISTS )?`company`", "回滚应删掉 company 表");
|
||
assertTrue(rb.contains("flyway_schema_history") && rb.contains("143"),
|
||
"回滚必须清掉 flyway_schema_history 里 V143 的记录,否则重装 jar 时 Flyway 认为已应用而跳过");
|
||
assertTrue(rb.contains("admin_companies"), "回滚应删掉后台「公司管理」菜单行");
|
||
|
||
assertTrue(rawMigration().contains(ROLLBACK_RELATIVE),
|
||
"V143 头注释的「回滚步骤」必须点名该脚本,否则值班的人找不到它");
|
||
assertFalse(rawMigration().contains("db/V144"),
|
||
"回滚不得写成版本化迁移 V144:Flyway 会在下次启动时自动执行它,静默撤销 V143");
|
||
|
||
for (Path f : versionFiles()) {
|
||
String name = f.getFileName().toString().toLowerCase();
|
||
assertFalse(name.contains("rollback"), "Flyway 扫描路径内不得出现回滚迁移: " + f.getFileName());
|
||
String ex = normalize(stripComments(Files.readString(f, StandardCharsets.UTF_8)));
|
||
assertFalse(ex.contains("DROP COLUMN `company_id`"),
|
||
"扫描路径内的迁移不得撤销 company_id: " + f.getFileName());
|
||
assertFalse(Pattern.compile("DROP TABLE (IF EXISTS )?`company`").matcher(ex).find(),
|
||
"扫描路径内的迁移不得删掉 company 表: " + f.getFileName());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 破坏点:docs/migration-inventory.md 的数字没跟着改。
|
||
* 下一个人按文档里的"取号起点"取号就会撞号(Flyway 启动失败),或以为 V143 还没被占用。
|
||
*/
|
||
@Test
|
||
void inventoryDocTracksTheNewMigration() throws IOException {
|
||
Path doc = ROOT.resolve("docs/migration-inventory.md");
|
||
assertTrue(Files.isRegularFile(doc), "盘点文档应存在");
|
||
String text = Files.readString(doc, StandardCharsets.UTF_8);
|
||
|
||
List<Path> files = versionFiles();
|
||
int maxOnDisk = files.stream().mapToInt(CompanyMigrationV143Test::versionOf).max().orElse(0);
|
||
|
||
assertTrue(text.contains(migration().getFileName().toString()),
|
||
"盘点文档应记录最新迁移文件名 " + migration().getFileName());
|
||
|
||
Matcher count = Pattern.compile("版本化迁移文件数:\\*\\*(\\d+)\\*\\*").matcher(text);
|
||
assertTrue(count.find(), "盘点文档应给出版本化迁移文件数");
|
||
assertEquals(files.size(), Integer.parseInt(count.group(1)),
|
||
"文档记录的迁移文件数与磁盘不符");
|
||
|
||
Matcher range = Pattern.compile("整数版本范围:\\*\\*V1\\.\\.V(\\d+)\\*\\*").matcher(text);
|
||
assertTrue(range.find(), "盘点文档应给出整数版本范围");
|
||
assertEquals(maxOnDisk, Integer.parseInt(range.group(1)),
|
||
"文档记录的整数版本上界与磁盘不符");
|
||
|
||
Matcher next = Pattern.compile("从 \\*\\*V(\\d+)\\*\\* 起取号").matcher(text);
|
||
assertTrue(next.find(), "盘点文档应给出取号起点");
|
||
assertEquals(maxOnDisk + 1, Integer.parseInt(next.group(1)),
|
||
"取号起点必须 = 磁盘最大版本 + 1,否则会撞号或留洞");
|
||
}
|
||
}
|