Skip to content

Commit 04431e8

Browse files
dmealingclaude
andcommitted
ci(db): add a live D1 schema-drift gate + reconcile the schema
Wires `meta verify --dialect d1` into CI (local, hermetic — a fresh miniflare D1, no Cloudflare auth) and predeploy (remote), closing the gap where the hand-written D1 migrations and the metadata were two unchecked sources of truth. The flagship drift-detection reference impl now gates its own DB schema the way it already gates codegen. Requires @metaobjectsdev 0.20.2 (the D1-verify false-drift fix). With that engine, the gate surfaced real, legitimate drift — reconciled here: - 6 columns carried a NOT NULL DEFAULT the metadata didn't declare (migrations 0002/0004). Declared the 4 meaningful ones (round/stance/confidence/keyClaims); the 2 vestigial empty-string defaults (take_markdown/one_line_summary) are dropped by the rebuild rather than restated as cruft. - councils.status / council_turns.kind / .confidence were modeled as enum/numeric but the DB enforced no CHECK — prod accepted values the metadata forbids. - the council_turns->councils FK lacked ON UPDATE CASCADE (the metamodel default for a relationship.composition). Migration 0006 (generated by `meta migrate --from-db`) is a recreate-and-copy that adds the three named CHECKs, gives the FK ON UPDATE CASCADE, and drops the two empty-string defaults — data preserved (INSERT … SELECT). Applies to prod at the next `db:migrate:remote`. Verified: a FRESH `wrangler d1 migrations apply --local` of 0001–0006 -> `meta verify --dialect d1` reports zero drift. Full gate green (drift / verify:db / tsc / build / 204). Upgrades @metaobjectsdev/* 0.20.1 -> 0.20.2. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014xy8powhHYJ6gfFt9Ut8dL
1 parent 02769fe commit 04431e8

7 files changed

Lines changed: 177 additions & 73 deletions

File tree

‎.github/workflows/meta-verify.yml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ jobs:
3838
cache: npm
3939
- run: npm ci
4040
- run: npm run demo:drift # metadata / prompt / codegen drift gate
41+
- run: npm run verify:db # D1 SCHEMA drift gate — apply migrations to a fresh local
42+
# D1 (miniflare; no Cloudflare auth) then `meta verify --dialect d1`. Catches a
43+
# migration that drifts from the metadata (a missing CHECK, a stale column) the
44+
# way demo:drift catches codegen drift. predeploy adds the --remote counterpart.
4145
- run: npx tsc -b # typecheck generated + hand-written code
4246
- run: npm run build:web # production build compiles
4347
- run: npm test # full vitest suite (no secrets required)

‎metaobjects/meta-council-turn.yaml‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,16 @@ metadata:
2020
# append-only and dropping it would force a table rewrite; every turn
2121
# now records round 0. Marked deprecated so tooling (not just a comment)
2222
# sees it as dead.
23+
# `default:` on the columns below mirrors the DB defaults added by
24+
# migration 0002 (structured outputs) / 0004 — the NOT NULL + DEFAULT
25+
# contract that let those columns be ALTER-ADDed onto existing rows. The
26+
# app always supplies a value; declaring the defaults here keeps the
27+
# metadata honest against the live D1 schema (meta verify --dialect d1).
2328
- field.int:
2429
name: round
2530
deprecated: true
2631
required: true
32+
default: 0
2733
- field.string:
2834
name: wizardId
2935
extends: Wizard.id
@@ -38,9 +44,15 @@ metadata:
3844
name: stance
3945
extends: wizardStance
4046
required: true
47+
default: abstains
4148
# The take-core is the persisted form of WizardOutput — single-source the
4249
# shape from the owner; keyClaims/citations keep the entity's physical
4350
# column names + jsonb storage as overrides on top of the inherited shape.
51+
# takeMarkdown / oneLineSummary carry NO `default:` — the migrations gave them
52+
# a vestigial `DEFAULT ''` (an empty-string default is meaningless for prose;
53+
# the app always supplies a value), so the rebuild migration drops it rather
54+
# than the metadata declaring cruft. (The non-empty defaults above/below are
55+
# kept — they mirror a real NOT NULL + DEFAULT contract.)
4456
- field.string:
4557
name: takeMarkdown
4658
extends: WizardOutput.takeMarkdown
@@ -50,10 +62,12 @@ metadata:
5062
- field.float:
5163
name: confidence
5264
extends: WizardOutput.confidence
65+
default: 0
5366
- field.string:
5467
name: keyClaims
5568
extends: WizardOutput.keyClaims
5669
column: key_claims_json
70+
default: "[]"
5771
- field.object:
5872
name: citations
5973
extends: WizardOutput.citations
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
PRAGMA foreign_keys = OFF;
2+
3+
CREATE TABLE "__new_council_turns" (
4+
"council_id" VARCHAR(16) NOT NULL,
5+
"ordinal" INTEGER NOT NULL,
6+
"round" INTEGER NOT NULL DEFAULT 0,
7+
"wizard_id" VARCHAR(32) NOT NULL,
8+
"wizard_name" VARCHAR(128) NOT NULL,
9+
"kind" TEXT NOT NULL,
10+
"stance" TEXT NOT NULL DEFAULT 'abstains',
11+
"take_markdown" TEXT NOT NULL,
12+
"one_line_summary" VARCHAR(140) NOT NULL,
13+
"confidence" REAL NOT NULL DEFAULT 0,
14+
"key_claims_json" TEXT NOT NULL DEFAULT '[]',
15+
"citations_json" TEXT,
16+
PRIMARY KEY ("council_id", "ordinal"),
17+
FOREIGN KEY ("council_id") REFERENCES "councils" ("id") ON DELETE CASCADE ON UPDATE CASCADE,
18+
CONSTRAINT "council_turns_kind_chk" CHECK ("kind" IN ('wizard', 'error')),
19+
CONSTRAINT "council_turns_stance_chk" CHECK ("stance" IN ('supports', 'opposes', 'complicates', 'reframes', 'abstains')),
20+
CONSTRAINT "council_turns_confidence_numeric_chk" CHECK ("confidence" >= 0 AND "confidence" <= 1)
21+
);
22+
23+
INSERT INTO "__new_council_turns" ("council_id", "ordinal", "round", "wizard_id", "wizard_name", "kind", "stance", "take_markdown", "one_line_summary", "confidence", "key_claims_json", "citations_json") SELECT "council_id", "ordinal", "round", "wizard_id", "wizard_name", "kind", "stance", "take_markdown", "one_line_summary", "confidence", "key_claims_json", "citations_json" FROM "council_turns";
24+
25+
DROP TABLE "council_turns";
26+
27+
ALTER TABLE "__new_council_turns" RENAME TO "council_turns";
28+
29+
PRAGMA foreign_keys = ON;
30+
31+
PRAGMA foreign_key_check;
32+
33+
PRAGMA foreign_keys = OFF;
34+
35+
CREATE TABLE "__new_councils" (
36+
"id" VARCHAR(8) PRIMARY KEY NOT NULL,
37+
"visitor_id" VARCHAR(64) NOT NULL,
38+
"question" TEXT NOT NULL,
39+
"status" TEXT NOT NULL,
40+
"verdict_stance" TEXT,
41+
"verdict_confidence" REAL,
42+
"verdict_markdown" TEXT,
43+
"verdict_evidence_quality" TEXT,
44+
"dissents_json" TEXT,
45+
"verdict_agreements_json" TEXT,
46+
"verdict_splits_json" TEXT,
47+
"verdict_verify_note" TEXT,
48+
"created_at" INTEGER NOT NULL,
49+
"completed_at" INTEGER,
50+
CONSTRAINT "councils_status_chk" CHECK ("status" IN ('pending', 'partial', 'complete', 'error')),
51+
CONSTRAINT "councils_verdict_stance_chk" CHECK ("verdict_stance" IN ('yes', 'no', 'it_depends', 'unanswerable')),
52+
CONSTRAINT "councils_verdict_evidence_quality_chk" CHECK ("verdict_evidence_quality" IN ('strong', 'mixed', 'thin', 'none'))
53+
);
54+
55+
INSERT INTO "__new_councils" ("id", "visitor_id", "question", "status", "verdict_stance", "verdict_confidence", "verdict_markdown", "verdict_evidence_quality", "dissents_json", "verdict_agreements_json", "verdict_splits_json", "verdict_verify_note", "created_at", "completed_at") SELECT "id", "visitor_id", "question", "status", "verdict_stance", "verdict_confidence", "verdict_markdown", "verdict_evidence_quality", "dissents_json", "verdict_agreements_json", "verdict_splits_json", "verdict_verify_note", "created_at", "completed_at" FROM "councils";
56+
57+
DROP TABLE "councils";
58+
59+
ALTER TABLE "__new_councils" RENAME TO "councils";
60+
61+
PRAGMA foreign_keys = ON;
62+
63+
PRAGMA foreign_key_check;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-- Reverse of 0006_reconcile-schema.sql.
2+
--
3+
-- 0006 is a SQLite recreate-and-copy: it adds the named CHECK constraints that the
4+
-- metadata's field.enum / validator.numeric imply (council_turns_kind_chk,
5+
-- council_turns_confidence_numeric_chk, councils_status_chk — plus named forms of the
6+
-- stance/verdict checks that migrations 0002/0004 created anonymously), gives the
7+
-- council_turns→councils FK `ON UPDATE CASCADE` (the metamodel default for a
8+
-- relationship.composition), and drops the vestigial `DEFAULT ''` on take_markdown /
9+
-- one_line_summary. It preserves ALL row data (INSERT … SELECT).
10+
--
11+
-- A faithful reverse is itself a full recreate-and-copy back to the pre-0006 shape
12+
-- (the accreted 0001+0002+0003+0004 form: anonymous stance/verdict checks, no
13+
-- kind/status/confidence check, FK ON DELETE only, the empty-string defaults restored).
14+
-- Because the forward migration drops no data and D1 migrations are applied append-only
15+
-- in practice, that reverse is left as a documented MANUAL recreate rather than
16+
-- auto-generated best-effort SQL — matching how `meta migrate` emits SQLite down
17+
-- migrations. Reconstruct the pre-0006 CREATE TABLEs from migrations 0001–0004 if a
18+
-- rollback is ever genuinely required.

‎package-lock.json‎

Lines changed: 57 additions & 57 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,24 @@
1313
"gen:db": "meta gen",
1414
"verify:portraits": "tsx scripts/verify-portraits.ts",
1515
"verify": "npm run verify:portraits",
16-
"check": "npm run gen:db && npm run verify && tsc -b && vitest run",
16+
"check": "npm run gen:db && npm run verify && npm run verify:db && tsc -b && vitest run",
1717
"build:web": "vite build",
18-
"predeploy": "npm run check && npm run build:web",
18+
"predeploy": "npm run check && npm run verify:db:remote && npm run build:web",
1919
"db:migrate:local": "wrangler d1 migrations apply wizardsofodd --local",
2020
"db:migrate:remote": "wrangler d1 migrations apply wizardsofodd --remote",
21+
"verify:db": "npm run db:migrate:local && meta verify --dialect d1 --d1 DB",
22+
"verify:db:remote": "meta verify --dialect d1 --d1 DB --remote",
2123
"gen:assets": "tsx scripts/generate-assets.ts",
2224
"gen:favicons": "tsx scripts/generate-favicons.ts",
2325
"postinstall": "wrangler types",
2426
"demo:drift": "meta verify --templates --codegen --prompts data/templates"
2527
},
2628
"dependencies": {
27-
"@metaobjectsdev/cli": "^0.20.1",
28-
"@metaobjectsdev/codegen-ts": "^0.20.1",
29-
"@metaobjectsdev/render": "^0.20.1",
30-
"@metaobjectsdev/runtime-ts": "^0.20.1",
31-
"@metaobjectsdev/sdk": "^0.20.1",
29+
"@metaobjectsdev/cli": "^0.20.2",
30+
"@metaobjectsdev/codegen-ts": "^0.20.2",
31+
"@metaobjectsdev/render": "^0.20.2",
32+
"@metaobjectsdev/runtime-ts": "^0.20.2",
33+
"@metaobjectsdev/sdk": "^0.20.2",
3234
"drizzle-orm": "^0.45.2",
3335
"drizzle-zod": "^0.5.1",
3436
"hono": "^4.12.29",
@@ -41,7 +43,7 @@
4143
"devDependencies": {
4244
"@cloudflare/vitest-pool-workers": "^0.16.20",
4345
"@cloudflare/workers-types": "^4.20241127.0",
44-
"@metaobjectsdev/metadata": "^0.20.1",
46+
"@metaobjectsdev/metadata": "^0.20.2",
4547
"@testing-library/react": "^16.3.2",
4648
"@types/node": "^26.1.1",
4749
"@types/react": "^18.3.29",

0 commit comments

Comments
 (0)