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

The look table drops its old key, and dark looks are filed under their real page #16826

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/16797-the-look-table-drops-its-old-key-and-dar → main 0 commits fork

  • Merged. scout/16797-the-look-table-drops-its-old-key-and-dar was merged into main as 7265ea4.

Review

Sign in to review this change.
5 files changed +152 −16
src/core/looks.ts +13 −16
@@ -162,11 +162,10 @@ interface Row {
162162 }
163163
164164 /**
165 * The `page` column of a dark look. The table's unique key is (app, env,
166 * commit, page, kind) and a roll never rebuilds it (migrations are
167 * additive while an older process still serves), so a dark look is filed
168 * under a page no light one can have — pages start with `/` — and this
169 * module is the only place that knows.
165 * The `page` prefix a dark look was filed under before the table's key named
166 * its theme (#16263). Nothing writes it now; it is read only, for the rows an
167 * older release still writes while a roll is in flight (the migration
168 * rewrote the rows that were there). Remove it once no such release serves.
170169 */
171170 const DARK_PREFIX = "dark:";
172171 /**
@@ -175,10 +174,8 @@ const DARK_PREFIX = "dark:";
175174 * no other can have, the same way a dark one is, and decoded here.
176175 */
177176 const NARROW_PREFIX = "narrow:";
178 const stored = (page: string, theme: LookTheme, kind: LookKind = "phone"): string => {
179 const base = kind === "narrow" ? `${NARROW_PREFIX}${page}` : page;
180 return theme === "dark" ? `${DARK_PREFIX}${base}` : base;
181 };
177 const stored = (page: string, kind: LookKind): string =>
178 kind === "narrow" ? `${NARROW_PREFIX}${page}` : page;
182179 const storedKind = (kind: LookKind): LookKind => (kind === "narrow" ? "phone" : kind);
183180 const unstoredDark = (page: string): string =>
184181 page.startsWith(DARK_PREFIX) ? page.slice(DARK_PREFIX.length) : page;
@@ -258,7 +255,7 @@ export function recordLook(db: Database, input: RecordLookInput): Look {
258255 input.appId,
259256 input.env,
260257 input.commitOid,
261 stored(page, theme, input.kind),
258 stored(page, input.kind),
262259 storedKind(input.kind),
263260 input.path,
264261 input.error ?? null,
@@ -283,18 +280,18 @@ export function getLook(
283280 kind: LookKind,
284281 theme: LookTheme = DEFAULT_THEME,
285282 ): Look | undefined {
286 // A dark look is filed as `dark:/p`, or as `/p` with theme `dark` (what a
287 // release without the prefix writes): match the theme column and either
288 // page, the prefixed form first.
289 const filed = stored(page, theme, kind);
290 const plain = unstoredDark(filed);
283 // A dark look is filed as `/p` with theme `dark`; an older release in a
284 // roll may still file `dark:/p`: match the theme column and either page,
285 // the plain form first.
286 const plain = stored(page, kind);
287 const filed = theme === "dark" ? `${DARK_PREFIX}${plain}` : plain;
291288 const row = db
292289 .query<
293290 Row,
294291 [string, string, string, string, string, string, string]
295292 >(`SELECT * FROM look WHERE app_id = ?1 AND env = ?2 AND commit_oid = ?3 AND kind = ?5 AND theme = ?6
296293 AND page IN (?4, ?7) ORDER BY page = ?4 DESC LIMIT 1`)
297 .get(appId, env, commitOid, filed, storedKind(kind), theme, plain);
294 .get(appId, env, commitOid, plain, storedKind(kind), theme, filed);
298295 return row ? toLook(row) : undefined;
299296 }
300297
src/core/migration-pins/20261130140000-look-drops-old-key.pin added +1
@@ -0,0 +1,1 @@
1 f7ece5a076005f25532900a350e3d01a3be040680e4601d6b8ac734fd69adb5c
src/core/migrations/20261130140000-look-drops-old-key.ts added +50
@@ -0,0 +1,50 @@
1 // The look table drops its old key (#16797, step 2 of #16263). A dark look was
2 // filed as `dark:/p` because (app_id, env, commit_oid, page, kind) could not tell
3 // it from the light one; step 1 (#16796, shipped in v0.7.675) keyed on theme too.
4 // Now the table is rebuilt without the old key, keeping look_theme_key, and every
5 // `dark:` row is filed under its real page with theme dark. A rebuild is the only
6 // way to lift a UNIQUE constraint. Same columns, same rows, the triggers and the
7 // index made again. Where a dark row filed both ways exists (the release that
8 // writes through the theme key and one that writes the prefix, in one roll), the
9 // newer taking wins.
10 export default `
11 CREATE TABLE look_next (
12 id TEXT PRIMARY KEY,
13 app_id TEXT NOT NULL REFERENCES app(id) ON DELETE CASCADE,
14 env TEXT NOT NULL,
15 commit_oid TEXT NOT NULL,
16 page TEXT NOT NULL DEFAULT '/',
17 kind TEXT NOT NULL CHECK (kind IN ('phone','desktop','thumb')),
18 path TEXT NOT NULL,
19 created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ','now')),
20 error TEXT,
21 taken_as TEXT NOT NULL DEFAULT 'owner' CHECK (taken_as IN ('owner','nobody')),
22 attached_by TEXT,
23 note TEXT,
24 theme TEXT NOT NULL DEFAULT 'light' CHECK (theme IN ('light','dark')),
25 scroll_width INTEGER, scroll_height INTEGER, inner_width INTEGER, inner_height INTEGER
26 );
27 CREATE UNIQUE INDEX look_next_key ON look_next (app_id, env, commit_oid, page, kind, theme);
28 INSERT OR REPLACE INTO look_next
29 (id, app_id, env, commit_oid, page, kind, path, created_at, error, taken_as,
30 attached_by, note, theme, scroll_width, scroll_height, inner_width, inner_height)
31 SELECT id, app_id, env, commit_oid,
32 CASE WHEN page LIKE 'dark:%' THEN substr(page, 6) ELSE page END,
33 kind, path, created_at, error, taken_as, attached_by, note,
34 CASE WHEN page LIKE 'dark:%' THEN 'dark' ELSE theme END,
35 scroll_width, scroll_height, inner_width, inner_height
36 FROM look ORDER BY created_at, rowid;
37 DROP TABLE look;
38 ALTER TABLE look_next RENAME TO look;
39 DROP INDEX look_next_key;
40 CREATE UNIQUE INDEX look_theme_key ON look (app_id, env, commit_oid, page, kind, theme);
41 CREATE TRIGGER front_epoch_look_insert AFTER INSERT ON look
42 WHEN NEW.env = 'production' AND NEW.kind = 'thumb'
43 BEGIN UPDATE front_epoch SET n = n + 1; END;
44 CREATE TRIGGER front_epoch_look_delete AFTER DELETE ON look
45 WHEN OLD.env = 'production' AND OLD.kind = 'thumb'
46 BEGIN UPDATE front_epoch SET n = n + 1; END;
47 CREATE TRIGGER front_epoch_look_update AFTER UPDATE OF app_id, env, commit_oid, kind, path, error, taken_as ON look
48 WHEN NEW.env = 'production' AND NEW.kind = 'thumb' OR OLD.env = 'production' AND OLD.kind = 'thumb'
49 BEGIN UPDATE front_epoch SET n = n + 1; END;
50 `;
test/look-drops-old-key.test.ts added +87
@@ -0,0 +1,87 @@
1 import { describe, expect, test } from "bun:test";
2 import { Database } from "bun:sqlite";
3 import { migrationFiles } from "../src/core/schema.ts";
4 import { migrate } from "../src/kernel/migrate.ts";
5 import { getLook, looksForEnv, recordLook } from "../src/core/looks.ts";
6
7 const NAME = "20261130140000-look-drops-old-key";
8 const before = migrationFiles.filter((m) => m.name < NAME);
9 const rest = migrationFiles.filter((m) => m.name >= NAME);
10 const row = (db: Database, id: string, page: string, theme: string, at = "2026-01-01T00:00:00.000Z") =>
11 db
12 .query(
13 `INSERT INTO look (id, app_id, env, commit_oid, page, kind, path, theme, created_at)
14 VALUES (?1, 'a', 'prod', 'c1', ?2, 'phone', ?1 || '.png', ?3, ?4)`,
15 )
16 .run(id, page, theme, at);
17 const open = () => {
18 const db = new Database(":memory:");
19 db.query("PRAGMA foreign_keys = OFF").run();
20 return db;
21 };
22
23 describe("the look table drops its old key", () => {
24 test("the migration files dark: rows under their real page, theme dark", () => {
25 const db = open();
26 migrate(db, before);
27 row(db, "l", "/p", "light");
28 row(db, "d", "dark:/p", "dark");
29 row(db, "n", "dark:narrow:/", "dark");
30 migrate(db, [...before, ...rest]);
31 const all = db.query("SELECT id, page, theme FROM look ORDER BY id").all();
32 expect(all).toEqual([
33 { id: "d", page: "/p", theme: "dark" },
34 { id: "l", page: "/p", theme: "light" },
35 { id: "n", page: "narrow:/", theme: "dark" },
36 ]);
37 expect(getLook(db, "a", "prod", "c1", "/p", "phone", "dark")?.path).toBe("d.png");
38 expect(getLook(db, "a", "prod", "c1", "/", "narrow", "dark")?.theme).toBe("dark");
39 });
40
41 test("a row filed both ways keeps the newer taking; the triggers and key are back", () => {
42 const db = open();
43 migrate(db, before);
44 row(db, "old", "dark:/p", "dark", "2026-01-01T00:00:00.000Z");
45 row(db, "new", "/p", "dark", "2026-02-01T00:00:00.000Z");
46 migrate(db, [...before, ...rest]);
47 expect(db.query("SELECT id FROM look").all()).toEqual([{ id: "new" }]);
48 const names = db.query("SELECT name FROM sqlite_master WHERE tbl_name = 'look' AND type != 'table' AND name NOT LIKE 'sqlite_%'").all();
49 expect(names.map((n: any) => n.name).sort()).toEqual([
50 "front_epoch_look_delete",
51 "front_epoch_look_insert",
52 "front_epoch_look_update",
53 "look_theme_key",
54 ]);
55 });
56
57 test("light and dark of one page coexist under one page string", () => {
58 const db = open();
59 migrate(db, migrationFiles);
60 const base = { appId: "a", env: "prod", commitOid: "c1", page: "/p", kind: "phone" as const };
61 recordLook(db, { ...base, path: "l.png" });
62 recordLook(db, { ...base, theme: "dark", path: "d.png" });
63 expect(db.query("SELECT page, theme FROM look ORDER BY theme").all()).toEqual([
64 { page: "/p", theme: "dark" },
65 { page: "/p", theme: "light" },
66 ]);
67 expect(looksForEnv(db, "a", "prod", "any")).toHaveLength(2);
68 });
69
70 test("a dark: row an older release writes later still reads as dark", () => {
71 const db = open();
72 migrate(db, migrationFiles);
73 row(db, "d", "dark:/p", "dark");
74 const look = getLook(db, "a", "prod", "c1", "/p", "phone", "dark");
75 expect(look?.page).toBe("/p");
76 expect(look?.theme).toBe("dark");
77 expect(getLook(db, "a", "prod", "c1", "/p", "phone", "light")).toBeUndefined();
78 // The old key is gone: the light and dark rows of one page are both allowed.
79 row(db, "l", "/p", "light");
80 });
81 test("recordLook writes no dark: prefix", () => {
82 const db = open();
83 migrate(db, migrationFiles);
84 recordLook(db, { appId: "a", env: "prod", commitOid: "c1", page: "/p", kind: "phone", theme: "dark", path: "d.png" });
85 expect(db.query("SELECT page FROM look").all()).toEqual([{ page: "/p" }]);
86 });
87 });
test/schema-additive.test.ts +1
@@ -163,6 +163,7 @@ const RETIRED: Record<string, { readonly drops: readonly string[]; readonly sinc
163163 * constraint in place, so a rebuild is the only way to lift one.
164164 */
165165 const REBUILT: Record<string, string> = {
166 "20261130140000-look-drops-old-key": "look loses UNIQUE(app_id, env, commit_oid, page, kind) and the dark: prefix (#16797); same columns, same rows, theme key and triggers made again; the release before (v0.7.675) writes and reads through the theme key and still reads a dark: row",
166167 "20261103120000-conversations-many": "conversation loses UNIQUE(user_id) (#8814); same columns, plus title and archived_at",
167168 "20261104100000-conversation-run-stopped-state": "conversation_run widens its state CHECK to admit stopped (#8972); same columns, same rows, the stopped column kept",
168169 "20261114000000-estimate-done": "issue_estimate widens its event CHECK to admit done (#11955); same columns, same rows, the indexes made again; the release before never writes or reads a done row",

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

Keyboard shortcuts

Docs