feat(schema-diff): support multiple table exclusion rules
This commit is contained in:
1 parent
5433f067d7
commit
3ffff92c8f
5 files changed
+122
-7
No files matched your search
@@ -10,6 +10,43 @@ const tables: TableInfo[] = [
|
||||
];
|
||||
|
||||
describe("schemaDiffTableFilter", () => {
|
||||
it("excludes tables matching any comma-separated rule on both sides", () => {
|
||||
const names = ["im_users", "ib_orders", "audit_log", "users", "catalog"];
|
||||
const input = names.map((name) => ({ name, table_type: "BASE TABLE" }));
|
||||
const options = { ...DEFAULT_MYSQL_OPTIONS, tableExcludePattern: " ^im_, ^ib_, log$ " };
|
||||
const result = filterSchemaDiffTables(input, input, compileSchemaDiffTableFilter(options), options);
|
||||
expect(result.sourceTables.map((table) => table.name)).toEqual(["users"]);
|
||||
expect(result.targetTables.map((table) => table.name)).toEqual(["users"]);
|
||||
});
|
||||
|
||||
it.each([
|
||||
["^a{2,3}$, ^ib_", ["aa", "aaa", "ib_orders"], ["a", "aaaa", "users"]],
|
||||
["^[a,b]+$, ^ib_", ["a,b", "ib_orders"], ["c", "users"]],
|
||||
["^(a,b)$, ^ib_", ["a,b", "ib_orders"], ["a", "b", "users"]],
|
||||
[String.raw`^a\,b$, ^ib_`, ["a,b", "ib_orders"], ["a", "b", "users"]],
|
||||
[String.raw`^(a)\1$, ^(b)\1$`, ["aa", "bb"], ["ab", "ba"]],
|
||||
])("preserves regex syntax and independent backreferences in %s", (pattern, excluded, kept) => {
|
||||
const options = { ...DEFAULT_MYSQL_OPTIONS, tableExcludePattern: pattern };
|
||||
const input = [...excluded, ...kept].map((name) => ({ name, table_type: "BASE TABLE" }));
|
||||
expect(filterSchemaDiffTables(input, [], compileSchemaDiffTableFilter(options), options).sourceTables.map((table) => table.name)).toEqual(kept);
|
||||
});
|
||||
|
||||
it("ignores empty rules without matching every table", () => {
|
||||
const options = { ...DEFAULT_MYSQL_OPTIONS, tableExcludePattern: " , , " };
|
||||
expect(filterSchemaDiffTables(tables, tables, compileSchemaDiffTableFilter(options), options).sourceTables).toEqual(tables);
|
||||
});
|
||||
|
||||
it("keeps include priority when a table matches any exclude rule", () => {
|
||||
const options = { ...DEFAULT_MYSQL_OPTIONS, tableIncludePattern: "^users$", tableExcludePattern: "^im_, users$", tableFilterPriority: "include" as const };
|
||||
expect(filterSchemaDiffTables(tables, [], compileSchemaDiffTableFilter(options), options).sourceTables.map((table) => table.name)).toEqual(["users"]);
|
||||
const excludePriority = { ...options, tableFilterPriority: "exclude" as const };
|
||||
expect(filterSchemaDiffTables(tables, [], compileSchemaDiffTableFilter(excludePriority), excludePriority).sourceTables).toEqual([]);
|
||||
});
|
||||
|
||||
it("rejects an invalid rule even if other rules are valid", () => {
|
||||
expect(() => compileSchemaDiffTableFilter({ ...DEFAULT_MYSQL_OPTIONS, tableExcludePattern: "^im_, [" })).toThrow(/Invalid exclude table name regex/);
|
||||
});
|
||||
|
||||
it("recognizes regular and materialized views", () => {
|
||||
expect(isSchemaDiffView(tables[0])).toBe(false);
|
||||
expect(isSchemaDiffView(tables[1])).toBe(true);
|
||||
|
||||
@@ -4,7 +4,7 @@ import type { SchemaDiffCompareOptions } from "@/types/schemaDiff";
|
||||
|
||||
export interface CompiledSchemaDiffTableFilter {
|
||||
include?: RegExp;
|
||||
exclude?: RegExp;
|
||||
exclude?: RegExp[];
|
||||
priority: SchemaDiffCompareOptions["tableFilterPriority"];
|
||||
}
|
||||
|
||||
@@ -24,17 +24,60 @@ function compilePattern(pattern: string, label: "include" | "exclude"): RegExp |
|
||||
}
|
||||
}
|
||||
|
||||
function compileExcludePatterns(input: string): RegExp[] | undefined {
|
||||
const patterns: RegExp[] = [];
|
||||
let start = 0;
|
||||
let escaped = false;
|
||||
let inCharacterClass = false;
|
||||
let groupDepth = 0;
|
||||
let braceDepth = 0;
|
||||
const appendPattern = (end: number) => {
|
||||
const pattern = compilePattern(input.slice(start, end), "exclude");
|
||||
if (pattern) patterns.push(pattern);
|
||||
};
|
||||
|
||||
// Split only outer commas; regex groups, character classes, quantifiers and
|
||||
// escaped commas remain part of their rule. Compile rules separately so
|
||||
// numbered backreferences do not change meaning across rules.
|
||||
for (let index = 0; index < input.length; index++) {
|
||||
const char = input[index];
|
||||
if (escaped) {
|
||||
escaped = false;
|
||||
continue;
|
||||
}
|
||||
if (char === "\\") {
|
||||
escaped = true;
|
||||
continue;
|
||||
}
|
||||
if (inCharacterClass) {
|
||||
if (char === "]") inCharacterClass = false;
|
||||
continue;
|
||||
}
|
||||
if (char === "[") inCharacterClass = true;
|
||||
else if (char === "(") groupDepth++;
|
||||
else if (char === ")") groupDepth = Math.max(0, groupDepth - 1);
|
||||
else if (char === "{") braceDepth++;
|
||||
else if (char === "}") braceDepth = Math.max(0, braceDepth - 1);
|
||||
else if (char === "," && groupDepth === 0 && braceDepth === 0) {
|
||||
appendPattern(index);
|
||||
start = index + 1;
|
||||
}
|
||||
}
|
||||
appendPattern(input.length);
|
||||
return patterns.length > 0 ? patterns : undefined;
|
||||
}
|
||||
|
||||
export function compileSchemaDiffTableFilter(options: SchemaDiffCompareOptions): CompiledSchemaDiffTableFilter {
|
||||
return {
|
||||
include: compilePattern(options.tableIncludePattern, "include"),
|
||||
exclude: compilePattern(options.tableExcludePattern, "exclude"),
|
||||
exclude: compileExcludePatterns(options.tableExcludePattern),
|
||||
priority: options.tableFilterPriority,
|
||||
};
|
||||
}
|
||||
|
||||
export function matchesSchemaDiffTableFilter(tableName: string, filter: CompiledSchemaDiffTableFilter): boolean {
|
||||
const includeMatches = filter.include ? filter.include.test(tableName) : true;
|
||||
const excludeMatches = filter.exclude ? filter.exclude.test(tableName) : false;
|
||||
const excludeMatches = filter.exclude?.some((pattern) => pattern.test(tableName)) ?? false;
|
||||
|
||||
if (filter.include && filter.exclude && includeMatches && excludeMatches) {
|
||||
return filter.priority === "include";
|
||||
|
||||
Reference in new issue
Block a user