|
| 1 | +import { writeFileSync } from "node:fs"; |
| 2 | +import { Prisma } from "@trigger.dev/database"; |
| 3 | +import { expect } from "vitest"; |
| 4 | +import { postgresTest } from "./index"; |
| 5 | +import { |
| 6 | + createAdapterClient, |
| 7 | + createClientPair, |
| 8 | + createRustClient, |
| 9 | + describe, |
| 10 | + runShape, |
| 11 | + type ShapeCase, |
| 12 | + type ShapeResult, |
| 13 | +} from "./adapterGolden"; |
| 14 | + |
| 15 | +function safeWrite(path: string, contents: string) { |
| 16 | + try { |
| 17 | + writeFileSync(path, contents); |
| 18 | + } catch { |
| 19 | + void 0; |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +const shapes: ShapeCase[] = [ |
| 24 | + { |
| 25 | + id: 1, |
| 26 | + name: "text[] param with explicit cast (= ANY(${ids}::text[]))", |
| 27 | + callSite: "PostgresRunStore.ts L2159-2161 (documented binding workaround)", |
| 28 | + upstream: "#24338", |
| 29 | + setup: async (c) => { |
| 30 | + await c.$executeRawUnsafe(`CREATE TABLE g1 (id text)`); |
| 31 | + await c.$executeRawUnsafe(`INSERT INTO g1 (id) VALUES ('a'),('b'),('c')`); |
| 32 | + }, |
| 33 | + run: (c) => { |
| 34 | + const ids = ["a", "c"]; |
| 35 | + return c.$queryRaw`SELECT id FROM g1 WHERE id = ANY(${ids}::text[]) ORDER BY id`; |
| 36 | + }, |
| 37 | + }, |
| 38 | + { |
| 39 | + id: 2, |
| 40 | + name: "array results (array_agg + text[] column)", |
| 41 | + callSite: "text[] columns / array_agg readers", |
| 42 | + upstream: "#27823", |
| 43 | + setup: async (c) => { |
| 44 | + await c.$executeRawUnsafe(`CREATE TABLE g2 (id text, tags text[])`); |
| 45 | + await c.$executeRawUnsafe(`INSERT INTO g2 (id, tags) VALUES ('x', ARRAY['a','b'])`); |
| 46 | + }, |
| 47 | + run: (c) => c.$queryRaw`SELECT tags, array_agg(id ORDER BY id) AS ids FROM g2 GROUP BY tags`, |
| 48 | + }, |
| 49 | + { |
| 50 | + id: 3, |
| 51 | + name: "bigint from COUNT(*)", |
| 52 | + callSite: "PostgresRunStore.ts:2162,2171; DeploymentListPresenter.server.ts:322,168", |
| 53 | + upstream: "#23926", |
| 54 | + setup: async (c) => { |
| 55 | + await c.$executeRawUnsafe(`CREATE TABLE g3 (id int)`); |
| 56 | + await c.$executeRawUnsafe(`INSERT INTO g3 (id) VALUES (1),(2),(3)`); |
| 57 | + }, |
| 58 | + run: (c) => c.$queryRaw`SELECT COUNT(*) AS c FROM g3`, |
| 59 | + }, |
| 60 | + { |
| 61 | + id: 4, |
| 62 | + name: "bigint ns timestamp (keyset pagination)", |
| 63 | + callSite: "taskEventStore.server.ts L339-386", |
| 64 | + run: (c) => c.$queryRaw`SELECT 1723058400000000000::int8 AS t`, |
| 65 | + }, |
| 66 | + { |
| 67 | + id: 5, |
| 68 | + name: "NUMERIC -> Prisma.Decimal", |
| 69 | + callSite: "QueueListPresenter.server.ts:383", |
| 70 | + setup: async (c) => { |
| 71 | + await c.$executeRawUnsafe(`CREATE TABLE g5 (n numeric(30,4))`); |
| 72 | + await c.$executeRawUnsafe(`INSERT INTO g5 (n) VALUES ('12345.6789')`); |
| 73 | + }, |
| 74 | + run: (c) => c.$queryRaw`SELECT n FROM g5`, |
| 75 | + }, |
| 76 | + { |
| 77 | + id: 6, |
| 78 | + name: "enum cast (CAST('LOG'::text AS enum))", |
| 79 | + callSite: "taskEventStore.server.ts:196,224,274,303,327", |
| 80 | + setup: async (c) => { |
| 81 | + await c.$executeRawUnsafe(`CREATE TYPE g6_kind AS ENUM ('LOG','SPAN')`); |
| 82 | + }, |
| 83 | + run: (c) => c.$queryRaw`SELECT CAST('LOG'::text AS g6_kind) AS kind`, |
| 84 | + }, |
| 85 | + { |
| 86 | + id: 7, |
| 87 | + name: "jsonb with cast parameters (to_jsonb(${v}::text/::int))", |
| 88 | + callSite: "dashboardPreferences.server.ts:145,175", |
| 89 | + upstream: "#24338", |
| 90 | + run: (c) => { |
| 91 | + const theme = "dark"; |
| 92 | + const contrast = 1; |
| 93 | + return c.$queryRaw` |
| 94 | + SELECT jsonb_set( |
| 95 | + jsonb_set('{}'::jsonb, '{theme}', to_jsonb(${theme}::text)), |
| 96 | + '{contrast}', to_jsonb(${contrast}::int) |
| 97 | + ) AS prefs`; |
| 98 | + }, |
| 99 | + }, |
| 100 | + { |
| 101 | + id: 8, |
| 102 | + name: "timestamptz round-trip (Date param)", |
| 103 | + callSite: "Date -> timestamptz writers", |
| 104 | + upstream: "#28629", |
| 105 | + run: (c) => { |
| 106 | + const d = new Date("2026-08-07T12:34:56.789Z"); |
| 107 | + return c.$queryRaw`SELECT ${d}::timestamptz AS ts`; |
| 108 | + }, |
| 109 | + }, |
| 110 | + { |
| 111 | + id: 9, |
| 112 | + name: "IN with varying arity", |
| 113 | + callSite: "boundedIn() (TRI-4480)", |
| 114 | + upstream: "#21803", |
| 115 | + setup: async (c) => { |
| 116 | + await c.$executeRawUnsafe(`CREATE TABLE g9 (id text)`); |
| 117 | + await c.$executeRawUnsafe(`INSERT INTO g9 (id) VALUES ('a'),('b'),('c'),('d')`); |
| 118 | + }, |
| 119 | + run: async (c) => { |
| 120 | + const one = await c.$queryRaw`SELECT id FROM g9 WHERE id IN (${"a"}) ORDER BY id`; |
| 121 | + const three = |
| 122 | + await c.$queryRaw`SELECT id FROM g9 WHERE id IN (${"a"},${"c"},${"d"}) ORDER BY id`; |
| 123 | + return { arity1: one, arity3: three }; |
| 124 | + }, |
| 125 | + }, |
| 126 | + { |
| 127 | + id: 11, |
| 128 | + name: "nulls, empty result set, zero-row RETURNING", |
| 129 | + callSite: "classic divergence points", |
| 130 | + setup: async (c) => { |
| 131 | + await c.$executeRawUnsafe(`CREATE TABLE g11 (id text)`); |
| 132 | + await c.$executeRawUnsafe(`INSERT INTO g11 (id) VALUES ('a')`); |
| 133 | + }, |
| 134 | + run: async (c) => { |
| 135 | + const nulls = await c.$queryRaw`SELECT NULL::text AS a, NULL::int AS b`; |
| 136 | + const empty = await c.$queryRaw`SELECT id FROM g11 WHERE 1=0`; |
| 137 | + const returning = await c.$queryRaw`UPDATE g11 SET id = id WHERE 1=0 RETURNING id`; |
| 138 | + return { nulls, empty, returning }; |
| 139 | + }, |
| 140 | + }, |
| 141 | +]; |
| 142 | + |
| 143 | +function renderTable(results: ShapeResult[]): string { |
| 144 | + const rows = results.map((r) => { |
| 145 | + const status = r.identical ? "IDENTICAL" : r.adapterErrored ? "ADAPTER ERROR" : "DIVERGES"; |
| 146 | + return [ |
| 147 | + `### Shape ${r.id}: ${r.name}`, |
| 148 | + `- call site: ${r.callSite}${r.upstream ? ` (upstream ${r.upstream})` : ""}`, |
| 149 | + `- result: **${status}**`, |
| 150 | + `- rust: \`${r.rust}\``, |
| 151 | + `- adapter: \`${r.adapter}\``, |
| 152 | + "", |
| 153 | + ].join("\n"); |
| 154 | + }); |
| 155 | + const pass = results.filter((r) => r.identical).length; |
| 156 | + return [`## Golden matrix: ${pass}/${results.length} identical`, "", ...rows].join("\n"); |
| 157 | +} |
| 158 | + |
| 159 | +postgresTest( |
| 160 | + "Deliverable B — raw-result equivalence matrix", |
| 161 | + async ({ postgresContainer }) => { |
| 162 | + const pair = await createClientPair(postgresContainer.getConnectionUri()); |
| 163 | + const results: ShapeResult[] = []; |
| 164 | + |
| 165 | + try { |
| 166 | + for (const shape of shapes) { |
| 167 | + const result = await runShape(pair, shape); |
| 168 | + results.push(result); |
| 169 | + const tag = result.identical ? "OK " : result.adapterErrored ? "ERR " : "DIFF"; |
| 170 | + console.log(`[TRI-13039][B] ${tag} shape ${result.id}: ${result.name}`); |
| 171 | + if (!result.identical) { |
| 172 | + console.log(` rust: ${result.rust}`); |
| 173 | + console.log(` adapter: ${result.adapter}`); |
| 174 | + } |
| 175 | + } |
| 176 | + } finally { |
| 177 | + await pair.disconnect(); |
| 178 | + } |
| 179 | + |
| 180 | + const md = renderTable(results); |
| 181 | + console.log("\n" + md); |
| 182 | + safeWrite("/Users/eric/code/triggerdotdev/isolated/adapter-pg-spike-work/golden-matrix.md", md); |
| 183 | + |
| 184 | + for (const r of results) { |
| 185 | + expect(r.rust, `shape ${r.id} (${r.name}) must be byte-identical`).toBe(r.adapter); |
| 186 | + } |
| 187 | + }, |
| 188 | + 180000 |
| 189 | +); |
| 190 | + |
| 191 | +postgresTest( |
| 192 | + "Deliverable B shape 10 — unqualified SQL relying on search_path / {schema} (#28128)", |
| 193 | + async ({ postgresContainer }) => { |
| 194 | + const baseUri = postgresContainer.getConnectionUri(); |
| 195 | + |
| 196 | + const admin = createRustClient(baseUri); |
| 197 | + await admin.$executeRawUnsafe(`CREATE SCHEMA s1`); |
| 198 | + await admin.$executeRawUnsafe(`CREATE TABLE s1.t (id text)`); |
| 199 | + await admin.$executeRawUnsafe(`INSERT INTO s1.t (id) VALUES ('inschema')`); |
| 200 | + await admin.$executeRawUnsafe(`CREATE TABLE public.pt (id text)`); |
| 201 | + await admin.$executeRawUnsafe(`INSERT INTO public.pt (id) VALUES ('inpublic')`); |
| 202 | + await admin.$disconnect(); |
| 203 | + |
| 204 | + const capture = async (fn: () => Promise<unknown>) => { |
| 205 | + try { |
| 206 | + return { ok: true as const, desc: describe(await fn()) }; |
| 207 | + } catch (err: any) { |
| 208 | + return { |
| 209 | + ok: false as const, |
| 210 | + desc: `${err?.code}: ${String(err?.message).split("\n").pop()}`, |
| 211 | + }; |
| 212 | + } |
| 213 | + }; |
| 214 | + |
| 215 | + const nonPublicAdapter = createAdapterClient(baseUri, "s1"); |
| 216 | + const publicAdapter = createAdapterClient(baseUri, "public"); |
| 217 | + try { |
| 218 | + const nonPublic = await capture(() => |
| 219 | + nonPublicAdapter.$queryRaw(Prisma.sql([`SELECT id FROM t ORDER BY id`])) |
| 220 | + ); |
| 221 | + const publicSchema = await capture(() => |
| 222 | + publicAdapter.$queryRaw(Prisma.sql([`SELECT id FROM pt ORDER BY id`])) |
| 223 | + ); |
| 224 | + |
| 225 | + console.log(`[TRI-13039][B] shape 10 adapter {schema:s1} unqualified: ${nonPublic.desc}`); |
| 226 | + console.log( |
| 227 | + `[TRI-13039][B] shape 10 adapter {schema:public} unqualified: ${publicSchema.desc}` |
| 228 | + ); |
| 229 | + safeWrite( |
| 230 | + "/Users/eric/code/triggerdotdev/isolated/adapter-pg-spike-work/golden-shape10.md", |
| 231 | + [ |
| 232 | + "## Shape 10: unqualified SQL relying on search_path (#28128)", |
| 233 | + "", |
| 234 | + "The adapter's `{schema}` option does NOT set a session search_path for raw SQL.", |
| 235 | + `- {schema:s1} + unqualified \`t\` (non-public): ${nonPublic.ok ? "OK" : "FAILS"} -> \`${nonPublic.desc}\``, |
| 236 | + `- {schema:public} + unqualified \`pt\` (public): ${publicSchema.ok ? "OK" : "FAILS"} -> \`${publicSchema.desc}\``, |
| 237 | + "", |
| 238 | + "Prod impact: both DATABASE_URL and RUN_OPS_DATABASE_URL use ?schema=public, so unqualified", |
| 239 | + "raw SQL resolves fine under the adapter. Only a non-public schema would break.", |
| 240 | + ].join("\n") |
| 241 | + ); |
| 242 | + |
| 243 | + expect(nonPublic.ok, "non-public schema breaks unqualified raw SQL (confirms #28128)").toBe( |
| 244 | + false |
| 245 | + ); |
| 246 | + expect(publicSchema.ok, "public schema resolves unqualified raw SQL under the adapter").toBe( |
| 247 | + true |
| 248 | + ); |
| 249 | + expect(publicSchema.desc).toBe(`[{id: "inpublic"}]`); |
| 250 | + } finally { |
| 251 | + await Promise.allSettled([nonPublicAdapter.$disconnect(), publicAdapter.$disconnect()]); |
| 252 | + } |
| 253 | + }, |
| 254 | + 180000 |
| 255 | +); |
0 commit comments