Skip to content

Commit a7328e4

Browse files
noreplyclaude
andcommitted
fork: schema check judges its own files against the base's copy
- A check file or .gitattributes that the branch carries unchanged from the base passes. After main is rebased onto a new upstream release, a branch from the old main is told to rebase onto main (its commits are matched on main by author, author time and subject), never to merge past the red check. - Package root imports (@t3tools/contracts) are followed to the files that declare the imported names, so migration 064's schemas in threadPullRequest.ts are covered while the rest of the package is not. - Schema statements also match across SQL comments and string joins, and with upper-case keywords around an interpolation (CREATE ${kind}). - parseRawDiff throws on output it cannot read; actions/checkout is pinned by commit SHA. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent e9afd0c commit a7328e4

3 files changed

Lines changed: 335 additions & 35 deletions

File tree

‎.github/scripts/fork-schema-check.cjs‎

Lines changed: 169 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// Refuses fork changes to the database schema: T3 has no down-migrations, so a fork migration
22
// strands installs that return to upstream. Usage: fork-schema-check.cjs <base> <head>.
3-
// Refused: migrations, schema setup and what they import; shipped files holding a schema statement
4-
// (matched across lines); this check itself and .gitattributes. Files are read as git blobs.
3+
// Refused: migrations, schema setup and what they import; shipped source files holding a schema
4+
// statement; a PR copy of this check or of .gitattributes that differs from the base's copy; and a
5+
// branch built on an old (since rebased) base. Files are read as git blobs.
56

67
const { execFileSync } = require("node:child_process");
78
const path = require("node:path");
@@ -23,21 +24,40 @@ const CHECK_FILES = [
2324
];
2425
const GITATTRIBUTES = /(^|\/)\.gitattributes$/;
2526

26-
// Schema statements in shipped source (tests may build their own tables). \s spans line breaks.
27-
const SCHEMA_STATEMENT =
28-
/\b(CREATE\s+(UNIQUE\s+)?(TABLE|INDEX|VIEW|TRIGGER|VIRTUAL\s+TABLE)|ALTER\s+TABLE|DROP\s+(TABLE|INDEX|VIEW|TRIGGER))\b/i;
27+
// Schema statements in shipped source (tests may build their own tables). Between the keywords
28+
// may stand line breaks, SQL comments and string joins ("CREATE " + "TABLE"). An interpolation
29+
// where the object kind belongs (`CREATE ${kind}`) counts too, but only with upper-case keywords,
30+
// so UI text such as `Create ${label}` stays free.
31+
const GAP = String.raw`(?:\s|["'\x60+]|/\*[\s\S]*?\*/|--[^\n]*\n)+`;
32+
const HOLE = String.raw`\$\{[^}]*\}`;
33+
const SCHEMA_STATEMENTS = [
34+
new RegExp(
35+
String.raw`\b(?:CREATE${GAP}(?:UNIQUE${GAP})?(?:TABLE|INDEX|VIEW|TRIGGER|VIRTUAL${GAP}TABLE)|ALTER${GAP}TABLE|DROP${GAP}(?:TABLE|INDEX|VIEW|TRIGGER))\b`,
36+
"i",
37+
),
38+
new RegExp(String.raw`\b(?:CREATE${GAP}(?:UNIQUE${GAP})?|ALTER${GAP}|DROP${GAP})${HOLE}`),
39+
];
40+
/** The first schema statement in `text`, or undefined. */
41+
const schemaStatement = (text) =>
42+
text === undefined
43+
? undefined
44+
: SCHEMA_STATEMENTS.map((pattern) => text.match(pattern)?.[0]).find(Boolean);
2945
const SHIPPED_SOURCE = /^(apps|packages)\/.+\.(ts|tsx|mts|cts|js|mjs|cjs|sql)$/;
3046
const TEST_SOURCE = /(\.test\.|\.spec\.|\.testkit\.|\/testkit\/|\/__tests__\/|\/test\/)/;
3147
const SOURCE_EXTENSIONS = ["", ".ts", ".tsx", ".mts", ".js", "/index.ts", "/index.tsx"];
3248
const PACKAGE_MANIFEST = /^(apps|packages)\/[^/]+\/package\.json$/;
3349
const IMPORT_SPECIFIER = /\b(?:from|import|require)\s*\(?\s*["']([^"'\n]+)["']/g;
50+
// `import { A, type B as C } from "x"` and `export { A } from "x"`: the names a static clause takes.
51+
const IMPORT_CLAUSE =
52+
/\b(?:import|export)\s+(?:type\s+)?([\w$*{}\s,]*?)\s*from\s*["']([^"'\n]+)["']/g;
3453

3554
const isTest = (file) => TEST_SOURCE.test(file);
3655
const isShippedSource = (file) => SHIPPED_SOURCE.test(file) && !isTest(file);
3756

3857
/**
39-
* Files the schema roots import, transitively: relative and package subpath imports. Package
40-
* roots (`@t3tools/contracts`) re-export the whole package, so they are not followed.
58+
* Files the schema roots import, transitively: relative and package subpath imports, and for a
59+
* package root (`@t3tools/contracts`) the files that declare the names imported from it. The
60+
* root's barrel itself is not added, so the rest of the package stays free to change.
4161
* @param {{ files: string[]; has(file: string): boolean; read(file: string): string }} tree
4262
* @returns {Set<string>}
4363
*/
@@ -50,11 +70,13 @@ function schemaClosure(tree) {
5070
while (queue.length > 0) {
5171
const file = queue.pop();
5272
const text = tree.read(file);
73+
const names = importedNames(text);
5374
for (const match of text.matchAll(IMPORT_SPECIFIER)) {
54-
const target = resolveImport(tree, packages, file, match[1]);
55-
if (target && !isTest(target) && !closure.has(target)) {
56-
closure.add(target);
57-
queue.push(target);
75+
for (const target of resolveImport(tree, packages, file, match[1], names.get(match[1]))) {
76+
if (!isTest(target) && !closure.has(target)) {
77+
closure.add(target);
78+
queue.push(target);
79+
}
5880
}
5981
}
6082
}
@@ -77,19 +99,93 @@ function packageDirectories(tree) {
7799
return packages;
78100
}
79101

80-
function resolveImport(tree, packages, from, specifier) {
102+
/**
103+
* Per specifier, the names static clauses take from it; `*` for a namespace or a clause it cannot
104+
* read. A specifier with no entry (dynamic import, require, side-effect import) takes everything.
105+
* @returns {Map<string, Set<string>>}
106+
*/
107+
function importedNames(text) {
108+
const names = new Map();
109+
for (const [, clause, specifier] of text.matchAll(IMPORT_CLAUSE)) {
110+
const set = names.get(specifier) ?? new Set();
111+
names.set(specifier, set);
112+
const braces = clause.match(/\{([^}]*)\}/);
113+
const outside = clause.replace(/\{[^}]*\}/, "").replace(/,/g, " ").trim();
114+
if (/\*/.test(outside)) set.add("*");
115+
else if (outside) set.add("default");
116+
for (const part of braces ? braces[1].split(",") : []) {
117+
const name = part.trim().replace(/^type\s+/, "").split(/\s+as\s+/)[0].trim();
118+
if (name) set.add(name);
119+
}
120+
if (!braces && !outside) set.add("*");
121+
}
122+
return names;
123+
}
124+
125+
/** The files an import reaches: one file, or for a package root the files declaring `names`. */
126+
function resolveImport(tree, packages, from, specifier, names) {
81127
if (specifier.startsWith(".")) {
82-
return resolveFile(tree, path.posix.join(path.posix.dirname(from), specifier));
128+
const target = resolveFile(tree, path.posix.join(path.posix.dirname(from), specifier));
129+
return target ? [target] : [];
83130
}
84-
const scoped = specifier.match(/^(@[^/]+\/[^/]+)\/(.+)$/);
85-
const pkg = scoped && packages.get(scoped[1]);
86-
if (!pkg || !pkg.exports || typeof pkg.exports !== "object") return undefined;
87-
const entry = pkg.exports[`./${scoped[2]}`];
131+
const parts = specifier.match(/^(@[^/]+\/[^/]+)(?:\/(.+))?$/);
132+
const pkg = parts && packages.get(parts[1]);
133+
if (!pkg || !pkg.exports) return [];
134+
const exports =
135+
typeof pkg.exports === "string" || "import" in pkg.exports || "default" in pkg.exports
136+
? { ".": pkg.exports }
137+
: pkg.exports;
138+
const entry = exports[parts[2] ? `./${parts[2]}` : "."];
88139
const target =
89140
typeof entry === "string" ? entry : entry && (entry.import ?? entry.default ?? entry.types);
90-
return typeof target === "string"
91-
? resolveFile(tree, path.posix.join(pkg.dir, target))
92-
: undefined;
141+
const file =
142+
typeof target === "string" ? resolveFile(tree, path.posix.join(pkg.dir, target)) : undefined;
143+
if (!file) return [];
144+
if (parts[2] || !names || names.has("*") || names.has("default")) return [file];
145+
const found = new Set();
146+
for (const name of names) declaringFiles(tree, packages, file, name, found, new Set());
147+
return [...found];
148+
}
149+
150+
/** Follows `export * from` and `export { name } from` until a file declares `name`. */
151+
function declaringFiles(tree, packages, file, name, found, seen) {
152+
if (seen.has(`${file}\0${name}`)) return;
153+
seen.add(`${file}\0${name}`);
154+
const text = tree.read(file);
155+
const word = name.replace(/\$/g, "\\$");
156+
const declares = new RegExp(
157+
String.raw`\bexport\s+(?:declare\s+)?(?:abstract\s+)?(?:const|let|var|function\*?|class|type|interface|enum|namespace)\s+${word}(?![\w$])`,
158+
);
159+
const local = [...text.matchAll(/\bexport\s+(?:type\s+)?\{([^}]*)\}(?!\s*from)/g)].some(
160+
(match) => match[1].split(",").some((part) => part.trim().split(/\s+as\s+/).pop() === name),
161+
);
162+
if (declares.test(text) || local) {
163+
found.add(file);
164+
return;
165+
}
166+
for (const match of text.matchAll(
167+
/\bexport\s+(?:type\s+)?(\*(?:\s+as\s+([\w$]+))?|\{[^}]*\})\s*from\s*["']([^"'\n]+)["']/g,
168+
)) {
169+
const [, clause, namespace, specifier] = match;
170+
const targets = resolveImport(tree, packages, file, specifier, new Set(["*"]));
171+
if (clause.startsWith("*") && !namespace) {
172+
for (const target of targets) declaringFiles(tree, packages, target, name, found, seen);
173+
continue;
174+
}
175+
const exported = namespace
176+
? namespace === name && "*"
177+
: clause
178+
.slice(1, -1)
179+
.split(",")
180+
.map((part) => part.trim().replace(/^type\s+/, "").split(/\s+as\s+/))
181+
.find((pair) => (pair[1] ?? pair[0]).trim() === name)?.[0]
182+
.trim();
183+
if (!exported) continue;
184+
for (const target of targets) {
185+
if (exported === "*") found.add(target);
186+
else declaringFiles(tree, packages, target, exported, found, seen);
187+
}
188+
}
93189
}
94190

95191
function resolveFile(tree, file) {
@@ -99,15 +195,31 @@ function resolveFile(tree, file) {
99195
}
100196

101197
/**
102-
* @param {ReadonlyArray<{ path: string; before?: string; after?: string }>} changes
103-
* one entry per changed path; `before`/`after` are the file texts (undefined when absent)
104-
* @param {{ closure: ReadonlySet<string>; baseHasCheck: boolean }} context
198+
* @param {ReadonlyArray<{ path: string; before?: string; after?: string; sameAsBase?: boolean }>} changes
199+
* one entry per path the branch changes since the merge-base; `before`/`after` are the file
200+
* texts (undefined when absent); `sameAsBase` says the branch's copy equals the base's copy
201+
* @param {{ closure: ReadonlySet<string>; baseHasCheck: boolean; staleCommits?: number }} context
202+
* `staleCommits`: the branch's commits that the base already holds as rebased copies
105203
* @returns {string[]} one reason per refused file
106204
*/
107-
function findSchemaChanges(changes, { closure, baseHasCheck }) {
205+
function findSchemaChanges(changes, { closure, baseHasCheck, staleCommits = 0 }) {
108206
const reasons = [];
207+
const rebase =
208+
"this branch is based on an old main that has since been rebased; rebase it onto main";
209+
if (staleCommits > 0) {
210+
reasons.push(
211+
`${staleCommits} commit(s) of this branch are already on main as rebased copies, so ${rebase}.`,
212+
);
213+
}
109214
for (const change of changes) {
110215
const file = change.path;
216+
const guarded = CHECK_FILES.includes(file) || GITATTRIBUTES.test(file);
217+
// The branch carries main's copy (e.g. it branched before main was rebased): nothing to judge.
218+
if (guarded && change.sameAsBase) continue;
219+
if (guarded && staleCommits > 0) {
220+
reasons.push(`${file}: differs from main's copy because ${rebase}.`);
221+
continue;
222+
}
111223
if (CHECK_FILES.includes(file)) {
112224
if (baseHasCheck) {
113225
reasons.push(
@@ -130,11 +242,11 @@ function findSchemaChanges(changes, { closure, baseHasCheck }) {
130242
}
131243
if (!isShippedSource(file)) continue;
132244
const statement = [change.after, change.before]
133-
.map((text) => text?.match(SCHEMA_STATEMENT)?.[0])
245+
.map(schemaStatement)
134246
.find(Boolean);
135247
if (statement) {
136248
reasons.push(
137-
`${file}: holds a schema statement (${statement.replace(/\s+/g, " ")}), so any change ` +
249+
`${file}: holds a schema statement (${statement.replace(/[^\w${}]+/g, " ")}), so any change ` +
138250
"to it may change the schema",
139251
);
140252
}
@@ -149,9 +261,13 @@ function findSchemaChanges(changes, { closure, baseHasCheck }) {
149261
function parseRawDiff(raw) {
150262
const fields = raw.split("\0");
151263
const entries = [];
152-
for (let index = 0; index + 1 < fields.length; index += 2) {
264+
if (fields.at(-1) === "") fields.pop();
265+
if (fields.length % 2 !== 0) throw new Error("cannot parse git diff --raw output (odd fields)");
266+
for (let index = 0; index < fields.length; index += 2) {
153267
const header = fields[index];
154-
if (!header.startsWith(":")) break;
268+
if (!/^:\d+ \d+ [0-9a-f]+ [0-9a-f]+ [A-Z]\d*$/.test(header)) {
269+
throw new Error(`cannot parse git diff --raw record: ${JSON.stringify(header)}`);
270+
}
155271
const [oldMode, newMode, oldSha, newSha, status] = header.slice(1).split(" ");
156272
if (/^[RC]/.test(status)) throw new Error(`unexpected rename in diff: ${header}`);
157273
const blob = (mode, sha) => (/^10|^12/.test(mode) && !/^0+$/.test(sha) ? sha : undefined);
@@ -194,7 +310,27 @@ function readBlob(sha) {
194310
}
195311

196312
function treeHas(rev, file) {
197-
return git(["ls-tree", "--full-tree", "--name-only", rev, "--", file]).trim() === file;
313+
return blobAt(rev, file) !== undefined;
314+
}
315+
316+
/** The blob id of `file` in `rev`, or undefined when absent. */
317+
function blobAt(rev, file) {
318+
const record = git(["ls-tree", "-z", "--full-tree", rev, "--", file]).split("\0")[0];
319+
const match = record.match(/^\d+ \w+ ([0-9a-f]+)\t(.+)$/s);
320+
return match && match[2] === file ? match[1] : undefined;
321+
}
322+
323+
/**
324+
* The branch's commits (merge-base..head) that the base already holds as rebased copies, matched
325+
* by author, author time and subject: a rebase keeps all three. Needs no file contents.
326+
*/
327+
function staleCommitCount(mergeBase, base, head) {
328+
const ids = (range) =>
329+
git(["log", "--no-merges", "--format=%an%x00%ae%x00%at%x00%s", range])
330+
.split("\n")
331+
.filter(Boolean);
332+
const onBase = new Set(ids(`${mergeBase}..${base}`));
333+
return ids(`${mergeBase}..${head}`).filter((id) => onBase.has(id)).length;
198334
}
199335

200336
function main(argv) {
@@ -209,10 +345,12 @@ function main(argv) {
209345
);
210346
// Only shipped source is searched for statements, so only its texts are read.
211347
const text = (entry, sha) => (sha && isShippedSource(entry.path) ? readBlob(sha) : undefined);
348+
const guarded = (file) => CHECK_FILES.includes(file) || GITATTRIBUTES.test(file);
212349
const changes = entries.map((entry) => ({
213350
path: entry.path,
214351
before: text(entry, entry.oldBlob),
215352
after: text(entry, entry.newBlob),
353+
sameAsBase: guarded(entry.path) ? blobAt(base, entry.path) === entry.newBlob : undefined,
216354
}));
217355
const closure = new Set([
218356
...schemaClosure(readTree(mergeBase)),
@@ -221,6 +359,7 @@ function main(argv) {
221359
const reasons = findSchemaChanges(changes, {
222360
closure,
223361
baseHasCheck: treeHas(base, CHECK_SCRIPT),
362+
staleCommits: staleCommitCount(mergeBase, base, head),
224363
});
225364
if (reasons.length === 0) {
226365
console.log(`No database schema changes (${changes.length} changed files checked).`);
@@ -231,7 +370,7 @@ function main(argv) {
231370
return 1;
232371
}
233372

234-
module.exports = { findSchemaChanges, parseRawDiff, schemaClosure, CHECK_FILES };
373+
module.exports = { findSchemaChanges, parseRawDiff, schemaClosure, CHECK_FILES, schemaStatement };
235374

236375
if (require.main === module) {
237376
process.exitCode = main(process.argv.slice(2));

0 commit comments

Comments
 (0)