|
| 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>). |
| 4 | + |
| 5 | +const { execFileSync } = require("node:child_process"); |
| 6 | + |
| 7 | +// Files that define or apply the schema. Any change to them is refused. |
| 8 | +const SCHEMA_PATHS = [ |
| 9 | + /^apps\/server\/src\/persistence\/Migrations(\/|\.ts$)/, |
| 10 | + /^apps\/server\/src\/persistence\/databaseMigrationHead\.ts$/, |
| 11 | + /^apps\/server\/src\/persistence\/initializeV2Database\.ts$/, |
| 12 | + /^apps\/server\/src\/persistence\/reconcileV2PreviewMigration\.ts$/, |
| 13 | +]; |
| 14 | + |
| 15 | +// Schema statements added anywhere else in shipped source (tests may build their own tables). |
| 16 | +const SCHEMA_STATEMENT = |
| 17 | + /\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\/)/; |
| 20 | + |
| 21 | +/** |
| 22 | + * @param {ReadonlyArray<{ path: string; addedLines: ReadonlyArray<string> }>} files |
| 23 | + * @returns {string[]} one reason per refused file |
| 24 | + */ |
| 25 | +function findSchemaChanges(files) { |
| 26 | + 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`); |
| 30 | + continue; |
| 31 | + } |
| 32 | + if (!SHIPPED_SOURCE.test(file.path) || TEST_SOURCE.test(file.path)) continue; |
| 33 | + const statement = file.addedLines.find((line) => SCHEMA_STATEMENT.test(line)); |
| 34 | + if (statement) { |
| 35 | + reasons.push(`${file.path}: adds a schema statement: ${statement.trim().slice(0, 120)}`); |
| 36 | + } |
| 37 | + } |
| 38 | + return reasons; |
| 39 | +} |
| 40 | + |
| 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)); |
| 62 | + } |
| 63 | + } |
| 64 | + return files; |
| 65 | +} |
| 66 | + |
| 67 | +function main(argv) { |
| 68 | + const [base, head] = argv; |
| 69 | + if (!base || !head) { |
| 70 | + console.error("Usage: fork-schema-check.cjs <base> <head>"); |
| 71 | + return 2; |
| 72 | + } |
| 73 | + const diff = execFileSync( |
| 74 | + "git", |
| 75 | + ["diff", "--unified=0", "--no-renames", "--no-color", `${base}...${head}`], |
| 76 | + { encoding: "utf8", maxBuffer: 256 * 1024 * 1024 }, |
| 77 | + ); |
| 78 | + const reasons = findSchemaChanges(parseDiff(diff)); |
| 79 | + if (reasons.length === 0) { |
| 80 | + console.log("No database schema changes."); |
| 81 | + return 0; |
| 82 | + } |
| 83 | + console.error("Fork changes must not change the database schema (no down-migrations exist):"); |
| 84 | + for (const reason of reasons) console.error(`- ${reason}`); |
| 85 | + return 1; |
| 86 | +} |
| 87 | + |
| 88 | +module.exports = { findSchemaChanges, parseDiff }; |
| 89 | + |
| 90 | +if (require.main === module) { |
| 91 | + process.exitCode = main(process.argv.slice(2)); |
| 92 | +} |
0 commit comments