Rhys Sullivanfmtc92c57d6 months agoHistory
67 lines · 2.9 KB · TypeScript
RawDownload
// Regression: every cloud drizzle instance must be constructed with the// merge of cloud + executor schemas. If ANY call site builds its own// `{ schema }` without spreading both, `db._.fullSchema` comes back// missing tables and the drizzle adapter throws// `unknown model "source"` at the first request that touches scoped data.//// We hit this in prod (MCP endpoint) when `mcp-session.ts` built// `combinedSchema = { ...cloudSchema }` and forgot to spread the executor// schema — web-app requests worked because they went through// `DbService.Live`, but the MCP Durable Object constructed its own.//// This test asserts the only sanctioned constant (`combinedSchema` from// `./db`) actually contains every executor-schema export AND that drizzle// surfaces them all under `db._.fullSchema`. import { describe, expect, it } from "@effect/vitest";import { drizzle } from "drizzle-orm/postgres-js";import { Effect } from "effect";import postgres from "postgres"; import * as cloudSchema from "./schema";import * as executorSchema from "./executor-schema";import { combinedSchema } from "./db"; describe("combinedSchema", () => {  it("spreads every cloud + executor schema export", () => {    const expected = new Set([...Object.keys(cloudSchema), ...Object.keys(executorSchema)]);    for (const key of expected) {      expect(combinedSchema, `combinedSchema missing "${key}"`).toHaveProperty(key);    }  });   // Executor-schema drives the scope-sharded tables (source/tool/etc). If  // any of these go missing the drizzle adapter's `getTable` lookup throws.  it("includes scope-sharded executor tables", () => {    for (const key of Object.keys(executorSchema)) {      expect(combinedSchema, `combinedSchema missing "${key}"`).toHaveProperty(key);    }  });   // The prod bug was actually at the drizzle layer: spread + __exportAll  // getters could theoretically drop tables if evaluated before their  // declarations. Construct a drizzle instance and walk its fullSchema  // to catch that class of bug too.  it.effect("drizzle(combinedSchema) exposes every table under _.fullSchema", () =>    // postgres() lazily connects — safe to build with a dummy url, we    // never .query() so no socket is opened.    Effect.acquireRelease(      Effect.sync(() => postgres("postgres://u:p@127.0.0.1:1/x", { max: 1 })),      (sql) => Effect.promise(() => sql.end({ timeout: 0 })),    ).pipe(      Effect.flatMap((sql) =>        Effect.sync(() => {          const db = drizzle(sql, { schema: combinedSchema });          const drizzleInternals = (            value: unknown,          ): { _: { fullSchema: Record<string, unknown> } } =>            value as { _: { fullSchema: Record<string, unknown> } };          const fullSchema = drizzleInternals(db)._.fullSchema;          for (const key of Object.keys(executorSchema)) {            expect(fullSchema, `fullSchema missing "${key}"`).toHaveProperty(key);          }        }),      ),    ),  );});