|
1 | 1 | // Refuses fork changes to the database schema: T3 has no down-migrations, so a fork migration |
2 | | -// strands installs that return to upstream. Fork state goes in existing JSON fields instead. |
3 | | -// Usage: node .github/scripts/fork-schema-check.cjs <base> <head> (checks <base>...<head>). |
| 2 | +// 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. |
4 | 5 |
|
5 | 6 | const { execFileSync } = require("node:child_process"); |
| 7 | +const path = require("node:path"); |
6 | 8 |
|
7 | | -// Files that define or apply the schema. Any change to them is refused. |
8 | | -const SCHEMA_PATHS = [ |
| 9 | +// Files that define or apply the schema. Any change to them, or to what they import, is refused. |
| 10 | +const SCHEMA_ROOTS = [ |
9 | 11 | /^apps\/server\/src\/persistence\/Migrations(\/|\.ts$)/, |
10 | 12 | /^apps\/server\/src\/persistence\/databaseMigrationHead\.ts$/, |
11 | 13 | /^apps\/server\/src\/persistence\/initializeV2Database\.ts$/, |
12 | 14 | /^apps\/server\/src\/persistence\/reconcileV2PreviewMigration\.ts$/, |
13 | 15 | ]; |
14 | 16 |
|
15 | | -// Schema statements added anywhere else in shipped source (tests may build their own tables). |
| 17 | +// The check's own files. Changing them would let a PR judge itself, so it is never allowed. |
| 18 | +const CHECK_SCRIPT = ".github/scripts/fork-schema-check.cjs"; |
| 19 | +const CHECK_FILES = [ |
| 20 | + CHECK_SCRIPT, |
| 21 | + ".github/scripts/fork-schema-check.test.cjs", |
| 22 | + ".github/workflows/fork-checks.yml", |
| 23 | +]; |
| 24 | +const GITATTRIBUTES = /(^|\/)\.gitattributes$/; |
| 25 | + |
| 26 | +// Schema statements in shipped source (tests may build their own tables). \s spans line breaks. |
16 | 27 | const SCHEMA_STATEMENT = |
17 | 28 | /\b(CREATE\s+(UNIQUE\s+)?(TABLE|INDEX|VIEW|TRIGGER|VIRTUAL\s+TABLE)|ALTER\s+TABLE|DROP\s+(TABLE|INDEX|VIEW|TRIGGER))\b/i; |
18 | | -const SHIPPED_SOURCE = /^(apps|packages)\/.+\.(ts|tsx|js|mjs|cjs|sql)$/; |
19 | | -const TEST_SOURCE = /(\.test\.|\.spec\.|\/testkit\/|\/__tests__\/|\/test\/)/; |
| 29 | +const SHIPPED_SOURCE = /^(apps|packages)\/.+\.(ts|tsx|mts|cts|js|mjs|cjs|sql)$/; |
| 30 | +const TEST_SOURCE = /(\.test\.|\.spec\.|\.testkit\.|\/testkit\/|\/__tests__\/|\/test\/)/; |
| 31 | +const SOURCE_EXTENSIONS = ["", ".ts", ".tsx", ".mts", ".js", "/index.ts", "/index.tsx"]; |
| 32 | +const PACKAGE_MANIFEST = /^(apps|packages)\/[^/]+\/package\.json$/; |
| 33 | +const IMPORT_SPECIFIER = /\b(?:from|import|require)\s*\(?\s*["']([^"'\n]+)["']/g; |
| 34 | + |
| 35 | +const isTest = (file) => TEST_SOURCE.test(file); |
| 36 | +const isShippedSource = (file) => SHIPPED_SOURCE.test(file) && !isTest(file); |
| 37 | + |
| 38 | +/** |
| 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. |
| 41 | + * @param {{ files: string[]; has(file: string): boolean; read(file: string): string }} tree |
| 42 | + * @returns {Set<string>} |
| 43 | + */ |
| 44 | +function schemaClosure(tree) { |
| 45 | + const packages = packageDirectories(tree); |
| 46 | + const queue = tree.files.filter( |
| 47 | + (file) => SCHEMA_ROOTS.some((pattern) => pattern.test(file)) && !isTest(file), |
| 48 | + ); |
| 49 | + const closure = new Set(queue); |
| 50 | + while (queue.length > 0) { |
| 51 | + const file = queue.pop(); |
| 52 | + const text = tree.read(file); |
| 53 | + 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); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + return closure; |
| 62 | +} |
| 63 | + |
| 64 | +function packageDirectories(tree) { |
| 65 | + const packages = new Map(); |
| 66 | + for (const file of tree.files) { |
| 67 | + if (!PACKAGE_MANIFEST.test(file)) continue; |
| 68 | + try { |
| 69 | + const manifest = JSON.parse(tree.read(file)); |
| 70 | + if (typeof manifest.name === "string") { |
| 71 | + packages.set(manifest.name, { dir: path.posix.dirname(file), exports: manifest.exports }); |
| 72 | + } |
| 73 | + } catch { |
| 74 | + // A broken package.json fails the build elsewhere; it defines no schema imports here. |
| 75 | + } |
| 76 | + } |
| 77 | + return packages; |
| 78 | +} |
| 79 | + |
| 80 | +function resolveImport(tree, packages, from, specifier) { |
| 81 | + if (specifier.startsWith(".")) { |
| 82 | + return resolveFile(tree, path.posix.join(path.posix.dirname(from), specifier)); |
| 83 | + } |
| 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]}`]; |
| 88 | + const target = |
| 89 | + 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; |
| 93 | +} |
| 94 | + |
| 95 | +function resolveFile(tree, file) { |
| 96 | + const candidates = SOURCE_EXTENSIONS.map((extension) => file + extension); |
| 97 | + if (file.endsWith(".js")) candidates.unshift(file.replace(/\.js$/, ".ts")); |
| 98 | + return candidates.find((candidate) => tree.has(candidate)); |
| 99 | +} |
20 | 100 |
|
21 | 101 | /** |
22 | | - * @param {ReadonlyArray<{ path: string; addedLines: ReadonlyArray<string> }>} files |
| 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 |
23 | 105 | * @returns {string[]} one reason per refused file |
24 | 106 | */ |
25 | | -function findSchemaChanges(files) { |
| 107 | +function findSchemaChanges(changes, { closure, baseHasCheck }) { |
26 | 108 | const reasons = []; |
27 | | - for (const file of files) { |
28 | | - if (SCHEMA_PATHS.some((pattern) => pattern.test(file.path))) { |
29 | | - reasons.push(`${file.path}: changes the database schema or its migrations`); |
| 109 | + for (const change of changes) { |
| 110 | + const file = change.path; |
| 111 | + if (CHECK_FILES.includes(file)) { |
| 112 | + if (baseHasCheck) { |
| 113 | + reasons.push( |
| 114 | + `${file}: changes the schema check itself. Only the owner may do that: the check stays ` + |
| 115 | + "red, and the owner merges past it.", |
| 116 | + ); |
| 117 | + } |
30 | 118 | continue; |
31 | 119 | } |
32 | | - if (!SHIPPED_SOURCE.test(file.path) || TEST_SOURCE.test(file.path)) continue; |
33 | | - const statement = file.addedLines.find((line) => SCHEMA_STATEMENT.test(line)); |
| 120 | + if (GITATTRIBUTES.test(file)) { |
| 121 | + reasons.push( |
| 122 | + `${file}: changes how git treats files, which can hide changes from review. Only the ` + |
| 123 | + "owner may do that.", |
| 124 | + ); |
| 125 | + continue; |
| 126 | + } |
| 127 | + if (SCHEMA_ROOTS.some((pattern) => pattern.test(file)) || closure.has(file)) { |
| 128 | + reasons.push(`${file}: changes the database schema, its migrations or code they import`); |
| 129 | + continue; |
| 130 | + } |
| 131 | + if (!isShippedSource(file)) continue; |
| 132 | + const statement = [change.after, change.before] |
| 133 | + .map((text) => text?.match(SCHEMA_STATEMENT)?.[0]) |
| 134 | + .find(Boolean); |
34 | 135 | if (statement) { |
35 | | - reasons.push(`${file.path}: adds a schema statement: ${statement.trim().slice(0, 120)}`); |
| 136 | + reasons.push( |
| 137 | + `${file}: holds a schema statement (${statement.replace(/\s+/g, " ")}), so any change ` + |
| 138 | + "to it may change the schema", |
| 139 | + ); |
36 | 140 | } |
37 | 141 | } |
38 | 142 | return reasons; |
39 | 143 | } |
40 | 144 |
|
41 | | -/** Parses `git diff --unified=0` output into changed files and their added lines. */ |
42 | | -function parseDiff(diff) { |
43 | | - const files = []; |
44 | | - let oldPath = ""; |
45 | | - let current; |
46 | | - for (const line of diff.split("\n")) { |
47 | | - if (line.startsWith("diff --git ")) { |
48 | | - current = undefined; |
49 | | - oldPath = ""; |
50 | | - } else if (current === undefined && line.startsWith("--- ")) { |
51 | | - oldPath = line.slice(4).replace(/^a\//, ""); |
52 | | - } else if (current === undefined && line.startsWith("+++ ")) { |
53 | | - // A deleted file's new side is /dev/null; it keeps its old path, so deleting is refused too. |
54 | | - const target = line.slice(4); |
55 | | - current = { |
56 | | - path: target === "/dev/null" ? oldPath : target.replace(/^b\//, ""), |
57 | | - addedLines: [], |
58 | | - }; |
59 | | - files.push(current); |
60 | | - } else if (current && line.startsWith("+")) { |
61 | | - current.addedLines.push(line.slice(1)); |
| 145 | +/** |
| 146 | + * Parses `git diff --raw -z --no-abbrev` output into { path, oldBlob, newBlob } entries. A side |
| 147 | + * that is absent or a submodule (no blob) is undefined; the path is still listed. |
| 148 | + */ |
| 149 | +function parseRawDiff(raw) { |
| 150 | + const fields = raw.split("\0"); |
| 151 | + const entries = []; |
| 152 | + for (let index = 0; index + 1 < fields.length; index += 2) { |
| 153 | + const header = fields[index]; |
| 154 | + if (!header.startsWith(":")) break; |
| 155 | + const [oldMode, newMode, oldSha, newSha, status] = header.slice(1).split(" "); |
| 156 | + if (/^[RC]/.test(status)) throw new Error(`unexpected rename in diff: ${header}`); |
| 157 | + const blob = (mode, sha) => (/^10|^12/.test(mode) && !/^0+$/.test(sha) ? sha : undefined); |
| 158 | + entries.push({ |
| 159 | + path: fields[index + 1], |
| 160 | + oldBlob: blob(oldMode, oldSha), |
| 161 | + newBlob: blob(newMode, newSha), |
| 162 | + }); |
| 163 | + } |
| 164 | + return entries; |
| 165 | +} |
| 166 | + |
| 167 | +function git(args) { |
| 168 | + return execFileSync("git", ["-c", "core.quotePath=false", ...args], { |
| 169 | + encoding: "utf8", |
| 170 | + maxBuffer: 512 * 1024 * 1024, |
| 171 | + }); |
| 172 | +} |
| 173 | + |
| 174 | +/** Source files and package manifests of a commit; texts are read only when the walk asks. */ |
| 175 | +function readTree(rev) { |
| 176 | + const blobs = new Map(); |
| 177 | + for (const record of git(["ls-tree", "-r", "-z", "--full-tree", rev]).split("\0")) { |
| 178 | + const match = record.match(/^\d+ blob ([0-9a-f]+)\t(.+)$/s); |
| 179 | + if (match && (SHIPPED_SOURCE.test(match[2]) || PACKAGE_MANIFEST.test(match[2]))) { |
| 180 | + blobs.set(match[2], match[1]); |
62 | 181 | } |
63 | 182 | } |
64 | | - return files; |
| 183 | + return { |
| 184 | + files: [...blobs.keys()], |
| 185 | + has: (file) => blobs.has(file), |
| 186 | + read: (file) => (blobs.has(file) ? readBlob(blobs.get(file)) : ""), |
| 187 | + }; |
| 188 | +} |
| 189 | + |
| 190 | +const blobCache = new Map(); |
| 191 | +function readBlob(sha) { |
| 192 | + if (!blobCache.has(sha)) blobCache.set(sha, git(["cat-file", "blob", sha])); |
| 193 | + return blobCache.get(sha); |
| 194 | +} |
| 195 | + |
| 196 | +function treeHas(rev, file) { |
| 197 | + return git(["ls-tree", "--full-tree", "--name-only", rev, "--", file]).trim() === file; |
65 | 198 | } |
66 | 199 |
|
67 | 200 | function main(argv) { |
@@ -70,22 +203,35 @@ function main(argv) { |
70 | 203 | console.error("Usage: fork-schema-check.cjs <base> <head>"); |
71 | 204 | return 2; |
72 | 205 | } |
73 | | - const diff = execFileSync( |
74 | | - "git", |
75 | | - ["diff", "--unified=0", "--no-renames", "--no-color", `${base}...${head}`], |
76 | | - { encoding: "utf8", maxBuffer: 256 * 1024 * 1024 }, |
| 206 | + const mergeBase = git(["merge-base", base, head]).trim(); |
| 207 | + const entries = parseRawDiff( |
| 208 | + git(["diff", "--raw", "-z", "--no-abbrev", "--no-renames", "--no-ext-diff", mergeBase, head]), |
77 | 209 | ); |
78 | | - const reasons = findSchemaChanges(parseDiff(diff)); |
| 210 | + // Only shipped source is searched for statements, so only its texts are read. |
| 211 | + const text = (entry, sha) => (sha && isShippedSource(entry.path) ? readBlob(sha) : undefined); |
| 212 | + const changes = entries.map((entry) => ({ |
| 213 | + path: entry.path, |
| 214 | + before: text(entry, entry.oldBlob), |
| 215 | + after: text(entry, entry.newBlob), |
| 216 | + })); |
| 217 | + const closure = new Set([ |
| 218 | + ...schemaClosure(readTree(mergeBase)), |
| 219 | + ...schemaClosure(readTree(head)), |
| 220 | + ]); |
| 221 | + const reasons = findSchemaChanges(changes, { |
| 222 | + closure, |
| 223 | + baseHasCheck: treeHas(base, CHECK_SCRIPT), |
| 224 | + }); |
79 | 225 | if (reasons.length === 0) { |
80 | | - console.log("No database schema changes."); |
| 226 | + console.log(`No database schema changes (${changes.length} changed files checked).`); |
81 | 227 | return 0; |
82 | 228 | } |
83 | 229 | console.error("Fork changes must not change the database schema (no down-migrations exist):"); |
84 | 230 | for (const reason of reasons) console.error(`- ${reason}`); |
85 | 231 | return 1; |
86 | 232 | } |
87 | 233 |
|
88 | | -module.exports = { findSchemaChanges, parseDiff }; |
| 234 | +module.exports = { findSchemaChanges, parseRawDiff, schemaClosure, CHECK_FILES }; |
89 | 235 |
|
90 | 236 | if (require.main === module) { |
91 | 237 | process.exitCode = main(process.argv.slice(2)); |
|