Public: anyone can read this; the crew can change it.

A migration that makes a return to the image before unproven can be named #17766

opened this

Conflicts with main in 2 files

scripts/return-check.ts · src/kernel/migrate.ts

sold/return-check → main 2 commits

Review

Sign in to review this change.
3 files changed +100 −1
scripts/return-check.ts added +53
@@ -0,0 +1,53 @@
1 #!/usr/bin/env bun
2 /**
3 * Whether the image of `<tag>` may take the forge's database back after this tree has migrated it.
4 *
5 * bun scripts/return-check.ts v0.7.719 [--allow <file>]
6 *
7 * One line for each migration since the tag that makes it unproven, and exit 1; exit 0 when there is
8 * none; 2 when it could not look. A shipped migration that was changed or removed is named too.
9 * `--allow` is a file a person writes: a migration id, a space and the reason, one a line. An id
10 * without a reason does not count. It reads words, so it does not prove the old code's writes.
11 */
12 import { existsSync, readFileSync } from "node:fs";
13 import { migrationFiles } from "../src/core/schema.ts";
14 import { unprovenReturn } from "../src/kernel/migrate.ts";
15
16 const DIR = "src/core/migrations";
17
18 const tag = process.argv[2];
19 const at = process.argv.indexOf("--allow");
20 const file = at < 0 ? undefined : process.argv[at + 1];
21 if (tag === undefined || !/^v\d+\.\d+\.\d+$/.test(tag)) {
22 console.error("usage: bun scripts/return-check.ts v<version> [--allow <file>]");
23 process.exit(2);
24 }
25 const allowed = new Map(
26 (file !== undefined && existsSync(file) ? readFileSync(file, "utf8").split("\n") : [])
27 .map((line) => /^(\S+)\s+(\S.*)$/.exec(line.trim()))
28 .flatMap((m) => (m === null ? [] : [[m[1]!, m[2]!] as const])),
29 );
30 const diff = Bun.spawnSync(["git", "diff", "--name-status", "--no-renames", tag, "HEAD", "--", DIR]);
31 if (diff.exitCode !== 0) {
32 console.error(`return-check: git could not compare with ${tag}`);
33 process.exit(2);
34 }
35 let bad = 0;
36 let seen = 0;
37 for (const line of diff.stdout.toString().split("\n").filter(Boolean)) {
38 const [state, path] = line.split("\t") as [string, string];
39 const id = path.slice(DIR.length + 1).replace(/\.ts$/, "");
40 seen++;
41 const migration = migrationFiles.find((m) => m.name === id);
42 const why = state !== "A" ? ["changed or removed after it shipped"] : migration === undefined ? ["not in the schema's list"] : unprovenReturn(migration.sql);
43 if (why.length === 0) continue;
44 const reason = allowed.get(id);
45 if (reason !== undefined) {
46 console.log(`${id}: ${why.join(", ")}; let through by a person: ${reason}`);
47 continue;
48 }
49 bad++;
50 console.log(`${id}: ${why.join(", ")}`);
51 }
52 console.log(`return-check: ${seen} migration(s) since ${tag}, ${bad} unproven`);
53 process.exit(bad === 0 ? 0 : 1);
src/kernel/migrate.ts +27
@@ -458,3 +458,30 @@ export function notAdditive(sql: string): string[] {
458458 found.push("adds a NOT NULL column without a default");
459459 return found;
460460 }
461
462 /**
463 * What in a migration makes a return to the image before it unproven: the old code must go on reading
464 * and writing a database this one changed. All `notAdditive` names, and what it lets pass: a unique
465 * index or a trigger (the old code's writes may be refused or changed), rows written to a table that
466 * was there before, and a new column with a CHECK or a reference. A table the same migration makes is
467 * unknown to the old code, so it counts for nothing. It reads words, not meaning.
468 */
469 export function unprovenReturn(sql: string): string[] {
470 const code = sql.replace(
471 /'(?:[^']|'')*'|"(?:[^"]|"")*"|\/\*[\s\S]*?(?:\*\/|$)|--[^\n]*/g,
472 (token) => (token.startsWith("'") ? "''" : token.startsWith('"') ? token.slice(1, -1).replace(/\W/g, "_") : " "),
473 );
474 const tables = (pattern: RegExp): string[] => [...code.matchAll(pattern)].map((m) => m[1]!.toLowerCase());
475 const made = new Set(tables(/\bCREATE\s+TABLE\s+(?:IF\s+NOT\s+EXISTS\s+)?[`[]?(\w+)/gi));
476 const found = notAdditive(sql);
477 const say = (what: string, pattern: RegExp): void => {
478 for (const table of new Set(tables(pattern))) if (!made.has(table)) found.push(`${what} ${table}`);
479 };
480 say("adds a unique index on", /\bCREATE\s+UNIQUE\s+INDEX\s+(?:IF\s+NOT\s+EXISTS\s+)?\S+\s+ON\s+[`[]?(\w+)/gi);
481 say("adds a trigger on", /\bCREATE\s+(?:TEMP\w*\s+)?TRIGGER\b[^;]*?\bON\s+[`[]?(\w+)/gi);
482 say("updates rows of", /\bUPDATE\s+(?:OR\s+\w+\s+)?[`[]?(\w+)[`\]]?\s+SET\b/gi);
483 say("deletes rows of", /\bDELETE\s+FROM\s+[`[]?(\w+)/gi);
484 say("inserts rows into", /\b(?:INSERT(?:\s+OR\s+\w+)?|REPLACE)\s+INTO\s+[`[]?(\w+)/gi);
485 say("adds a column with a CHECK or a reference to", /\bALTER\s+TABLE\s+[`[]?(\w+)[`\]]?\s+ADD\b[^;]*\b(?:CHECK|REFERENCES)\b/gi);
486 return found;
487 }
test/schema-additive.test.ts +20 −1
@@ -14,7 +14,7 @@ import {
1414 migrations,
1515 NUMBERED,
1616 } from "../src/core/schema.ts";
17 import { notAdditive } from "../src/kernel/migrate.ts";
17 import { notAdditive, unprovenReturn } from "../src/kernel/migrate.ts";
1818
1919 describe("migrations are additive across a roll", () => {
2020 test("the guard names what an old process could not serve under", () => {
@@ -66,6 +66,25 @@ describe("migrations are additive across a roll", () => {
6666 expect(notAdditive(sql)).toEqual([]);
6767 });
6868
69 test("what makes a return to the image before unproven is named, on tables that were there", () => {
70 for (const [sql, why] of [
71 ["CREATE UNIQUE INDEX run_x ON run(x);", "adds a unique index on run"],
72 ["CREATE TRIGGER t AFTER INSERT ON run BEGIN SELECT 1; END;", "adds a trigger on run"],
73 ["UPDATE run SET reason = 'x';", "updates rows of run"],
74 ['DELETE FROM "run" WHERE x = 1;', "deletes rows of run"],
75 ["INSERT OR IGNORE INTO run (id) VALUES ('a');", "inserts rows into run"],
76 ["ALTER TABLE run ADD COLUMN c TEXT CHECK (c <> '');", "adds a column with a CHECK or a reference to run"],
77 ["DROP TABLE run;", "drops a table"],
78 ] as const)
79 expect(unprovenReturn(sql)).toEqual([why]);
80 for (const sql of [
81 "ALTER TABLE run ADD COLUMN why TEXT; CREATE INDEX run_why ON run(why);",
82 "CREATE TABLE x (id TEXT REFERENCES run(id) ON UPDATE CASCADE ON DELETE CASCADE); CREATE UNIQUE INDEX x_id ON x(id); INSERT INTO x SELECT id FROM run;",
83 "-- UPDATE run SET x = 1\nCREATE TABLE y (id TEXT DEFAULT 'DELETE FROM run');",
84 ])
85 expect(unprovenReturn(sql)).toEqual([]);
86 });
87
6988 test("every migration not in both releases still in play is additive", () => {
7089 const inPlay = record.releases.slice(0, 2).map(shipped);
7190 expect(inPlay.length).toBeGreaterThan(0);

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs