Public: anyone can read this; the crew can change it.
Looks write and read through a theme key, so a release that drops the "dark:" prefix can roll safely #16807
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/16796-looks-write-and-read-through-a-theme-key → main 0 commits fork
4 files
changed +63
−4
src/core/looks.ts +10 −4
| @@ -250,7 +250,7 @@ export function recordLook(db: Database, input: RecordLookInput): Look { | ||
| 250 | 250 | `INSERT INTO look (id, app_id, env, commit_oid, page, kind, path, error, taken_as, attached_by, note, theme, |
| 251 | 251 | scroll_width, scroll_height, inner_width, inner_height) |
| 252 | 252 | VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14, ?15, ?16) |
| 253 | ON CONFLICT (app_id, env, commit_oid, page, kind) DO UPDATE SET | |
| 253 | ON CONFLICT (app_id, env, commit_oid, page, kind, theme) DO UPDATE SET | |
| 254 | 254 | path = excluded.path, error = excluded.error, taken_as = excluded.taken_as, |
| 255 | 255 | scroll_width = excluded.scroll_width, scroll_height = excluded.scroll_height, |
| 256 | 256 | inner_width = excluded.inner_width, inner_height = excluded.inner_height, |
| @@ -285,12 +285,18 @@ export function getLook( | ||
| 285 | 285 | kind: LookKind, |
| 286 | 286 | theme: LookTheme = DEFAULT_THEME, |
| 287 | 287 | ): Look | undefined { |
| 288 | // A dark look is filed as `dark:/p`, or as `/p` with theme `dark` (what a | |
| 289 | // release without the prefix writes): match the theme column and either | |
| 290 | // page, the prefixed form first. | |
| 291 | const filed = stored(page, theme, kind); | |
| 292 | const plain = unstoredDark(filed); | |
| 288 | 293 | const row = db |
| 289 | 294 | .query< |
| 290 | 295 | Row, |
| 291 | [string, string, string, string, string] | |
| 292 | >(`SELECT * FROM look WHERE app_id = ?1 AND env = ?2 AND commit_oid = ?3 AND page = ?4 AND kind = ?5`) | |
| 293 | .get(appId, env, commitOid, stored(page, theme, kind), storedKind(kind)); | |
| 296 | [string, string, string, string, string, string, string] | |
| 297 | >(`SELECT * FROM look WHERE app_id = ?1 AND env = ?2 AND commit_oid = ?3 AND kind = ?5 AND theme = ?6 | |
| 298 | AND page IN (?4, ?7) ORDER BY page = ?4 DESC LIMIT 1`) | |
| 299 | .get(appId, env, commitOid, filed, storedKind(kind), theme, plain); | |
| 294 | 300 | return row ? toLook(row) : undefined; |
| 295 | 301 | } |
| 296 | 302 |
src/core/migration-pins/20261130130000-look-theme-key.pin added +1
| @@ -0,0 +1,1 @@ | ||
| 1 | dd064e32fa029067f61f7ebd5f2d360b3dd24df54e3cf501ab48e9a5b617ccb7 |
src/core/migrations/20261130130000-look-theme-key.ts added +9
| @@ -0,0 +1,9 @@ | ||
| 1 | // A look's key names its theme (#16796, step 1 of #16263). Additive: the old | |
| 2 | // unique key (app_id, env, commit_oid, page, kind) stays, so a process from | |
| 3 | // before this release keeps writing across a roll. `core/looks.ts` now | |
| 4 | // upserts on this key and reads a dark look filed either way, so a later | |
| 5 | // release can drop the `dark:` prefix and rebuild the old key away. | |
| 6 | export default ` | |
| 7 | CREATE UNIQUE INDEX IF NOT EXISTS look_theme_key | |
| 8 | ON look (app_id, env, commit_oid, page, kind, theme); | |
| 9 | `; |
test/look-theme-key.test.ts added +43
| @@ -0,0 +1,43 @@ | ||
| 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, looksForCommits, looksForEnv, recordLook } from "../src/core/looks.ts"; | |
| 6 | ||
| 7 | const fresh = () => { | |
| 8 | const db = new Database(":memory:"); | |
| 9 | migrate(db, migrationFiles); | |
| 10 | return db; | |
| 11 | }; | |
| 12 | ||
| 13 | const hand = (db: Database, page: string, theme: string) => | |
| 14 | db | |
| 15 | .query( | |
| 16 | `INSERT INTO look (id, app_id, env, commit_oid, page, kind, path, theme) | |
| 17 | VALUES (?1, 'a', 'prod', 'c1', ?2, 'phone', 'x.png', ?3)`, | |
| 18 | ) | |
| 19 | .run(`id-${page}-${theme}`, page, theme); | |
| 20 | ||
| 21 | describe("a look is keyed by its theme", () => { | |
| 22 | test("a dark look filed as /p with theme dark reads as page /p, dark", () => { | |
| 23 | const db = fresh(); | |
| 24 | hand(db, "/p", "dark"); | |
| 25 | const look = getLook(db, "a", "prod", "c1", "/p", "phone", "dark"); | |
| 26 | expect(look?.page).toBe("/p"); | |
| 27 | expect(look?.theme).toBe("dark"); | |
| 28 | expect(getLook(db, "a", "prod", "c1", "/p", "phone", "light")).toBeUndefined(); | |
| 29 | for (const list of [looksForEnv(db, "a", "prod", "any"), looksForCommits(db, ["c1"])]) { | |
| 30 | expect(list.map((l) => [l.page, l.theme])).toEqual([["/p", "dark"]]); | |
| 31 | } | |
| 32 | }); | |
| 33 | ||
| 34 | test("a retake of a dark look replaces it, and the light one stays", () => { | |
| 35 | const db = fresh(); | |
| 36 | const base = { appId: "a", env: "prod", commitOid: "c1", page: "/p", kind: "phone" as const }; | |
| 37 | recordLook(db, { ...base, path: "l.png" }); | |
| 38 | recordLook(db, { ...base, theme: "dark", path: "1.png" }); | |
| 39 | const again = recordLook(db, { ...base, theme: "dark", path: "2.png" }); | |
| 40 | expect(again.path).toBe("2.png"); | |
| 41 | expect(looksForEnv(db, "a", "prod", "any")).toHaveLength(2); | |
| 42 | }); | |
| 43 | }); |
new above
offline