fix: 索引键长守卫漏检 CREATE INDEX

守卫一直没真正生效,两个独立缺陷:

1. CREATE INDEX 用 matches() 匹配,语句行尾的分号让它整条不匹配 ——
   仓库里 8 条独立 CREATE INDEX(含 V20261009_1 那批业务索引)全被静默跳过;
2. 多行 ALTER TABLE 的子句写在下一行,逐行解析拿到的 rest 是空的,
   由它新增的列从未登记;且守卫是在所有迁移解析完之后才解析列,
   用最终 schema 去解析早期索引,列被后续迁移改名时会误报「列无法解析」。

改为:ALTER TABLE 拼回续行、列类型在索引声明当时记录。
已用三个探针验证:引用不存在的列被拦、4080 字节超长被拦、真实迁移全绿。
This commit is contained in:
2026-10-10 20:28:17 +08:00
parent 1ba3ae5a04
commit 6075839937
@@ -60,7 +60,8 @@ class MigrationIndexKeyLengthTest {
Map.entry("time", 3),
Map.entry("year", 1));
private record IndexDef(String file, String table, String name, List<String> columns) {
private record IndexDef(String file, String table, String name, List<String> columns,
Map<String, String> declaredTypes) {
}
@Test
@@ -78,11 +79,10 @@ class MigrationIndexKeyLengthTest {
List<String> violations = new ArrayList<>();
for (IndexDef index : indexes) {
Map<String, String> columns = tableColumns.get(index.table());
if (columns == null) {
violations.add(index.file() + " 的索引 " + index.name() + " 引用了未知表 " + index.table());
continue;
}
// 用「索引声明当时」的列类型,而不是所有迁移跑完后的最终 schema:
// 后续迁移可能把列改名(如 session_num → context_num),用最终 schema 去解析
// 早期索引会得到「列无法解析」的假报。索引键长本来就由建索引那一刻的列类型决定。
Map<String, String> columns = index.declaredTypes();
int total = 0;
List<String> unknown = new ArrayList<>();
for (String column : index.columns()) {
@@ -160,7 +160,8 @@ class MigrationIndexKeyLengthTest {
Pattern.compile("(?i)^\\s*create\\s+table\\s+(?:if\\s+not\\s+exists\\s+)?(\\w+)\\s*\\(?\\s*$");
private static final Pattern COLUMN =
Pattern.compile("(?i)^\\s*(\\w+)\\s+([a-z]+)\\s*(?:\\(\\s*(\\d+)(?:\\s*,\\s*\\d+)?\\s*\\))?");
private static final Pattern ALTER_TABLE = Pattern.compile("(?i)^\\s*alter\\s+table\\s+(\\w+)\\s+(.*)$");
// 允许表名后面没有内容:本仓库多处的 ALTER TABLE 把子句写在下一行
private static final Pattern ALTER_TABLE = Pattern.compile("(?i)^\\s*alter\\s+table\\s+(\\w+)\\s*(.*)$");
private static final Pattern ADD_OR_MODIFY_COLUMN =
Pattern.compile("(?i)^(?:add|modify)\\s+column\\s+(\\w+)\\s+([a-z]+)\\s*(?:\\(\\s*(\\d+)(?:\\s*,\\s*\\d+)?\\s*\\))?");
private static final Pattern RENAME_COLUMN =
@@ -175,14 +176,39 @@ class MigrationIndexKeyLengthTest {
private final String fileName;
private String currentTable;
private boolean insideTableBody;
/**
* CREATE TABLE 体内的 KEY/INDEX 声明先存着,等表体结束再算。
* 列定义可能写在 KEY 行之后,声明当时就解析会得到假报。
*/
private final List<InlineKey> pendingInlineKeys = new ArrayList<>();
private record InlineKey(String name, List<String> columns) {
}
Parser(String fileName) {
this.fileName = fileName;
}
/**
* 记下索引声明**当时**各列的声明类型。
*
* <p>不能等所有迁移跑完再拿最终 schema 去解析:后续迁移可能把列改名
* (如 {@code session_num → context_num}),那时早期索引会变成「列无法解析」的假报。</p>
*/
private IndexDef indexOf(String table, String name, List<String> columns,
Map<String, Map<String, String>> tableColumns) {
Map<String, String> known = tableColumns.get(table);
Map<String, String> declared = new LinkedHashMap<>();
for (String column : columns) {
declared.put(column, known == null ? null : known.get(column));
}
return new IndexDef(fileName, table, name, columns, declared);
}
void parse(String sql, Map<String, Map<String, String>> tableColumns, List<IndexDef> indexes) {
for (String rawLine : sql.split("\r?\n")) {
String line = rawLine.strip();
String[] lines = sql.split("\r?\n");
for (int i = 0; i < lines.length; i += 1) {
String line = lines[i].strip();
if (line.isEmpty()) {
continue;
}
@@ -198,6 +224,11 @@ class MigrationIndexKeyLengthTest {
if (insideTableBody) {
if (line.startsWith(")")) {
insideTableBody = false;
// 表体结束,此时所有列都已登记,可以安全解析体内的 KEY 声明
for (InlineKey key : pendingInlineKeys) {
indexes.add(indexOf(currentTable, key.name(), key.columns(), tableColumns));
}
pendingInlineKeys.clear();
continue;
}
if (line.startsWith("(")) {
@@ -205,8 +236,7 @@ class MigrationIndexKeyLengthTest {
}
Matcher inlineKey = INLINE_KEY.matcher(line);
if (inlineKey.find()) {
indexes.add(new IndexDef(fileName, currentTable, inlineKey.group(1),
splitColumns(inlineKey.group(2))));
pendingInlineKeys.add(new InlineKey(inlineKey.group(1), splitColumns(inlineKey.group(2))));
continue;
}
if (isConstraintKeyword(line)) {
@@ -225,9 +255,12 @@ class MigrationIndexKeyLengthTest {
}
Matcher createIndex = CREATE_INDEX.matcher(line);
if (createIndex.matches()) {
indexes.add(new IndexDef(fileName, createIndex.group(2).toLowerCase(Locale.ROOT),
createIndex.group(1), splitColumns(createIndex.group(3))));
// 必须用 find() 而不是 matches():语句行尾带分号(`... (a, b);`)时
// matches() 要求整行完全匹配,会把这条 CREATE INDEX 静默跳过——
// 守卫看着在跑,实际一条独立 CREATE INDEX 都没检查。
if (createIndex.find()) {
indexes.add(indexOf(createIndex.group(2).toLowerCase(Locale.ROOT), createIndex.group(1),
splitColumns(createIndex.group(3)), tableColumns));
continue;
}
@@ -236,12 +269,20 @@ class MigrationIndexKeyLengthTest {
continue;
}
String table = alterTable.group(1).toLowerCase(Locale.ROOT);
String rest = alterTable.group(2).strip();
// ALTER TABLE 的子句常写在下一行(本仓库多处如此)。逐行解析会把 rest 当成空,
// 于是新加的列从未登记 → 后面引用它的 CREATE INDEX 会误报「列无法解析」。
// 这里把直到语句结束(行尾分号)的续行拼回来。
StringBuilder restBuilder = new StringBuilder(alterTable.group(2));
while (!restBuilder.toString().strip().endsWith(";") && i + 1 < lines.length) {
i += 1;
restBuilder.append(' ').append(lines[i].strip());
}
String rest = restBuilder.toString().strip();
Map<String, String> columns = tableColumns.computeIfAbsent(table, key -> new LinkedHashMap<>());
Matcher addUnique = ADD_UNIQUE_CONSTRAINT.matcher(rest);
if (addUnique.find()) {
indexes.add(new IndexDef(fileName, table, addUnique.group(1), splitColumns(addUnique.group(2))));
indexes.add(indexOf(table, addUnique.group(1), splitColumns(addUnique.group(2)), tableColumns));
continue;
}