Skip to content

Commit 2146267

Browse files
noreplyclaude
andcommitted
fork: CI check that refuses database schema and migration changes
T3 has no down-migrations, so a fork patch that changes the schema would strand every install that later returns to upstream. fork-checks.yml runs on every pull request and fails when the change touches the migration files or adds a schema statement to shipped source. The follow workflow runs the same script over the whole patch series. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 24e7747 commit 2146267

3 files changed

Lines changed: 178 additions & 0 deletions

File tree

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const assert = require("node:assert/strict");
2+
const test = require("node:test");
3+
const { findSchemaChanges, parseDiff } = require("./fork-schema-check.cjs");
4+
5+
const file = (path, ...addedLines) => ({ path, addedLines });
6+
7+
test("refuses any change to a migration or the schema setup", () => {
8+
const reasons = findSchemaChanges([
9+
file("apps/server/src/persistence/Migrations/082_New.ts", "export default 1;"),
10+
file("apps/server/src/persistence/Migrations.ts", "import x from './x';"),
11+
file("apps/server/src/persistence/databaseMigrationHead.ts", "export const HEAD = 82;"),
12+
]);
13+
assert.equal(reasons.length, 3);
14+
});
15+
16+
test("refuses a schema statement added in shipped source", () => {
17+
const [reason] = findSchemaChanges([
18+
file(
19+
"apps/server/src/feature/Store.ts",
20+
" yield* sql`ALTER TABLE threads ADD COLUMN x TEXT`;",
21+
),
22+
]);
23+
assert.match(reason, /Store\.ts: adds a schema statement: .*ALTER TABLE/);
24+
});
25+
26+
test("allows schema statements in tests and ordinary changes elsewhere", () => {
27+
assert.deepEqual(
28+
findSchemaChanges([
29+
file("apps/server/src/feature/Store.test.ts", "CREATE TABLE scratch (id TEXT)"),
30+
file("apps/server/src/persistence/testkit/Fake.ts", "CREATE INDEX i ON t (x)"),
31+
file("apps/server/src/feature/Store.ts", "const label = 'create a table view';"),
32+
file("docs/notes.md", "ALTER TABLE in prose"),
33+
file("packages/contracts/src/settings.ts", " mcpProfile: Schema.optional(Schema.String),"),
34+
]),
35+
[],
36+
);
37+
});
38+
39+
test("parseDiff keeps added lines and the old path of a deleted file", () => {
40+
const diff = [
41+
"diff --git a/apps/x.ts b/apps/x.ts",
42+
"--- a/apps/x.ts",
43+
"+++ b/apps/x.ts",
44+
"@@ -1,0 +2 @@",
45+
"+const added = 1;",
46+
"-const removed = 1;",
47+
"diff --git a/apps/server/src/persistence/Migrations/001_A.ts b/apps/server/src/persistence/Migrations/001_A.ts",
48+
"--- a/apps/server/src/persistence/Migrations/001_A.ts",
49+
"+++ /dev/null",
50+
"@@ -1 +0,0 @@",
51+
"-export default 1;",
52+
].join("\n");
53+
assert.deepEqual(parseDiff(diff), [
54+
{ path: "apps/x.ts", addedLines: ["const added = 1;"] },
55+
{ path: "apps/server/src/persistence/Migrations/001_A.ts", addedLines: [] },
56+
]);
57+
assert.equal(findSchemaChanges(parseDiff(diff)).length, 1);
58+
});

‎.github/workflows/fork-checks.yml‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Fork checks
2+
3+
# Rules for this fork's own changes, run on every pull request. Upstream's CI needs runners a fork
4+
# does not have; fork-tests.yml runs upstream's tests on hosted runners instead.
5+
on:
6+
pull_request:
7+
8+
permissions:
9+
contents: read
10+
11+
jobs:
12+
no_schema_changes:
13+
name: No database schema changes
14+
runs-on: ubuntu-latest
15+
timeout-minutes: 5
16+
steps:
17+
- name: Checkout
18+
uses: actions/checkout@v6
19+
with:
20+
fetch-depth: 0
21+
filter: blob:none
22+
23+
- name: Test the check
24+
run: node --test .github/scripts/fork-schema-check.test.cjs
25+
26+
# T3 has no down-migrations, so a fork migration strands installs that return to upstream.
27+
- name: Refuse schema and migration changes
28+
run: node .github/scripts/fork-schema-check.cjs "${{ github.event.pull_request.base.sha }}" "${{ github.event.pull_request.head.sha }}"

0 commit comments

Comments
 (0)