c4d2349cda
判据是逐支把 import 行的简单名拿去文件其余部分(含注释与 javadoc)做词边界匹配, 名字只在注释里出现的当作「在用」保留,所以这把尺子是往保守侧偏的。 验收不靠静态回声:`mvn -o -q test-compile` rc=0,再 `mvn -o clean package` = Tests run 4264, Failures 0, Errors 0, Skipped 7,与清之前逐项一致(4,313→4,264 那 49 例是上午清设计文档摘掉的读 docs 用例,不是这一笔)。 diff 形状只认 `+0 / -248`,行尾没被整片改写。
110 lines
5.4 KiB
Java
110 lines
5.4 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.List;
|
||
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;
|
||
|
||
/**
|
||
* V146(users.expires_at,管理员到期停用的数据模型)的文件审计。
|
||
* 设计口径见 AccountExpiryService(级联判定)与 ExpiredAccountTaskStopService(过期停任务)的类注释。
|
||
*
|
||
* <p>本仓 pom 无 H2、无 testcontainers,迁移跑不起来,文件审计是唯一能守住的层——
|
||
* 与 {@link CompanyMigrationV144Test} / {@link CompanyScopeMigrationV145Test} 同口径:
|
||
* 断言打在剥掉注释后的可执行 SQL 上。</p>
|
||
*/
|
||
class UsersExpiryMigrationV146Test {
|
||
|
||
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 Path migration() throws IOException {
|
||
List<Path> hits;
|
||
try (Stream<Path> s = Files.list(DB_DIR)) {
|
||
hits = s.filter(p -> p.getFileName().toString().matches("^V\\d+(?:_\\d+)?__.*\\.sql$"))
|
||
.filter(p -> p.getFileName().toString().startsWith("V146__"))
|
||
.toList();
|
||
}
|
||
assertFalse(hits.isEmpty(), "缺少 V146__*.sql:users.expires_at 还没落盘");
|
||
assertEquals(1, hits.size(), "V146 只允许一个迁移文件(撞号会让 Flyway 启动失败)");
|
||
return hits.get(0);
|
||
}
|
||
|
||
private static String rawMigration() throws IOException {
|
||
return Files.readString(migration(), StandardCharsets.UTF_8);
|
||
}
|
||
|
||
/** 粗剥离注释后的可执行 SQL(本迁移无字符串内 -- 字面量,简化实现即可,勿复制到含 -- 的迁移)。 */
|
||
private static String statements() throws IOException {
|
||
String body = rawMigration();
|
||
// 防呆:字符串字面量里若出现 "--"(如注释文案),先按行处理会误伤——这里断言它不存在
|
||
assertFalse(body.contains("'--"), "字符串字面量内出现 -- 时不能用简化剥离,请改用逐字扫描");
|
||
return body.replaceAll("(?m)^\\s*--.*$", "")
|
||
.replaceAll("\\s+", " ")
|
||
.trim();
|
||
}
|
||
|
||
// ---------------------------------------------------------------- 断言
|
||
|
||
@Test
|
||
void versionIsUniqueAndSequential() throws IOException {
|
||
assertEquals("V146__users_expires_at_admin_expiry.sql",
|
||
migration().getFileName().toString(), "迁移名与描述须一致");
|
||
}
|
||
|
||
@Test
|
||
void headerCarriesTemplateFields() throws IOException {
|
||
String head = rawMigration().substring(0, Math.min(2400, rawMigration().length()));
|
||
assertTrue(head.contains("变更目的"), "头注释缺「变更目的」");
|
||
assertTrue(head.contains("影响表与数据量"), "头注释缺「影响表与数据量」");
|
||
assertTrue(head.contains("锁表风险"), "头注释缺「锁表风险」");
|
||
assertTrue(head.contains("验证 SQL"), "头注释缺「验证 SQL」");
|
||
assertTrue(head.contains("回滚步骤"), "头注释缺「回滚步骤」");
|
||
assertTrue(head.contains("上线窗口"), "头注释缺「上线窗口」");
|
||
}
|
||
|
||
@Test
|
||
void onlyTouchesUsersTable() throws IOException {
|
||
String sql = statements();
|
||
assertTrue(sql.contains("ALTER TABLE `users` ADD COLUMN `expires_at`"), "必须给 users 加 expires_at");
|
||
// 业务表零 DDL:这条迁移不得 CREATE TABLE / DROP,也不得碰 company / columns
|
||
assertFalse(sql.toUpperCase().contains("CREATE TABLE"), "本迁移不应建表");
|
||
assertFalse(sql.toUpperCase().contains("DROP "), "本迁移不应删任何对象");
|
||
assertFalse(sql.contains("`company`") || sql.contains("`columns`"), "company/columns 不许动");
|
||
assertFalse(sql.contains("biz_"), "业务表零 DDL(57 张业务表不许出现在本迁移)");
|
||
}
|
||
|
||
@Test
|
||
void addColumnIsConditionalAndRerunnable() throws IOException {
|
||
String sql = statements();
|
||
assertTrue(sql.contains("information_schema.COLUMNS"), "加列前必须查 information_schema(可重跑性)");
|
||
assertTrue(sql.contains("PREPARE stmt FROM @ddl"), "条件 DDL 要走 PREPARE");
|
||
assertTrue(sql.contains("DEALLOCATE PREPARE stmt"), "PREPARE 之后必须 DEALLOCATE");
|
||
assertTrue(sql.contains("already exists"), "列已存在时要有跳过分支(flyway repair 重跑不撞 1060)");
|
||
}
|
||
|
||
@Test
|
||
void columnIsNullableWithoutDefault() throws IOException {
|
||
String sql = statements();
|
||
assertTrue(sql.contains("datetime NULL"), "expires_at 必须可空且无默认值:NULL=永不过期");
|
||
assertFalse(sql.contains("NOT NULL"), "不许 NOT NULL:全部存量账号必须保持永不过期");
|
||
}
|
||
|
||
@Test
|
||
void shipsVerificationSql() throws IOException {
|
||
String sql = rawMigration();
|
||
assertTrue(sql.contains("SHOW COLUMNS FROM `users` LIKE 'expires_at'"), "验证段要能核对列存在性");
|
||
assertTrue(sql.contains("with_expiry"), "验证段要断言没有任何存量回填(with_expiry = 0)");
|
||
}
|
||
}
|