#!/usr/bin/env node /** * BI-P061: is "only one published release per dashboard" actually enforced? * * The application checks for a live release before publishing, and that check * is a read-then-write race. This script measures BOTH directions against a * real Postgres and writes evidence/bi-p061/dashboard-release-race.json: * * guard_absent the partial unique index dropped -> both concurrent * publishes commit, and the dashboard has TWO live versions. * guard_present the index in place -> the loser fails with 23505. * * Without the first run, the second proves nothing: a race that never * interleaved would also show one published release. * * ANALYTICS_TEST_DATABASE_URL=postgres://postgres:test@127.0.0.1:25441/opt \ * node scripts/bi-p061-release-race.mjs * * It CREATEs and DROPs its own throwaway schemas and touches nothing else. */ import fs from 'node:fs'; import path from 'node:path'; import postgres from 'postgres'; const URL_ = process.env.ANALYTICS_TEST_DATABASE_URL; if (!URL_) { console.error('ANALYTICS_TEST_DATABASE_URL is required. Refusing to invent numbers.'); process.exit(2); } const STAMP = Date.now().toString(36); const SCHEMAS = { guard_present: `bi_p061_ev_on_${STAMP}`, guard_absent: `bi_p061_ev_off_${STAMP}` }; const MIGRATIONS = ['sql/008_analytics_domain_model.sql', 'sql/010_analytics_dashboard_releases.sql']; const HASH = (c) => c.repeat(64); const connect = (schema, max = 1) => postgres(URL_, { max, onnotice: () => undefined, connection: { search_path: schema } }); async function setup(schema, { dropIndex }) { const admin = postgres(URL_, { max: 1, onnotice: () => undefined }); await admin.unsafe(`CREATE SCHEMA IF NOT EXISTS ${schema}`); await admin.end({ timeout: 5 }); const c = connect(schema, 2); for (const f of MIGRATIONS) await c.unsafe(fs.readFileSync(path.join(process.cwd(), f), 'utf8')); if (dropIndex) await c.unsafe('DROP INDEX opt_analytics_dashboard_release_one_published'); await c.unsafe(` INSERT INTO opt_analytics_dashboard_definition (dashboard_id, tenant_id, project_id, name, current_version, owner_id) VALUES ('dash_race', 't_alpha', 'p_main', 'Race', 1, 'u_1'); INSERT INTO opt_analytics_dashboard_version (dashboard_version_id, dashboard_id, tenant_id, project_id, version, layout_json, layout_hash) VALUES ('dv_1', 'dash_race', 't_alpha', 'p_main', 1, '{}', '${HASH('a')}'), ('dv_2', 'dash_race', 't_alpha', 'p_main', 2, '{}', '${HASH('b')}');`); return c; } /** Both sessions read "nothing is live", then both write. */ async function race(schema) { const conns = [connect(schema), connect(schema)]; const publish = (conn, n) => conn.begin(async (tx) => { await tx.unsafe( `SELECT release_id FROM opt_analytics_dashboard_release WHERE dashboard_id='dash_race' AND state='published'`, ); await new Promise((r) => setTimeout(r, 60)); await tx.unsafe(` INSERT INTO opt_analytics_dashboard_release (release_id, dashboard_id, dashboard_version_id, tenant_id, project_id, version, state, document_hash, published_at) VALUES ('rel_${n}', 'dash_race', 'dv_${n}', 't_alpha', 'p_main', ${n}, 'published', '${HASH(n === 1 ? 'a' : 'b')}', now())`); }); const results = await Promise.allSettled([publish(conns[0], 1), publish(conns[1], 2)]); for (const c of conns) await c.end({ timeout: 5 }); return { committed: results.filter((r) => r.status === 'fulfilled').length, rejected: results .filter((r) => r.status === 'rejected') .map((r) => ({ code: r.reason?.code ?? null, constraint: r.reason?.constraint_name ?? null })), }; } async function publishedRows(conn) { const rows = await conn.unsafe( `SELECT release_id, version FROM opt_analytics_dashboard_release WHERE dashboard_id='dash_race' AND state='published' ORDER BY version`, ); return rows.map((r) => ({ release_id: r.release_id, version: r.version })); } const out = { recorded_at: new Date().toISOString() }; const cleanup = []; try { for (const [label, schema] of Object.entries(SCHEMAS)) { const conn = await setup(schema, { dropIndex: label === 'guard_absent' }); cleanup.push([schema, conn]); if (!out.postgres_version) { const v = await conn.unsafe('SHOW server_version'); out.postgres_version = v[0].server_version; } const result = await race(schema); out[label] = { partial_unique_index_present: label === 'guard_present', transactions_committed: result.committed, rejections: result.rejected, published_releases_after: await publishedRows(conn), }; } out.verdict = { counterfactual_produced_two_live_dashboards: out.guard_absent.published_releases_after.length === 2, guard_left_exactly_one: out.guard_present.published_releases_after.length === 1, loser_error_code: out.guard_present.rejections[0]?.code ?? null, application_check_alone_is_sufficient: false, note: 'DashboardReleaseService.publish() reads for a live release before writing. Both sessions in ' + 'guard_absent passed that check and both committed. The partial unique index is what holds.', }; const dir = path.join(process.cwd(), 'evidence/bi-p061'); fs.mkdirSync(dir, { recursive: true }); const file = path.join(dir, 'dashboard-release-race.json'); fs.writeFileSync(file, `${JSON.stringify(out, null, 2)}\n`); console.log(`wrote ${file}`); console.log(JSON.stringify(out.verdict, null, 2)); } finally { for (const [schema, conn] of cleanup) { await conn.unsafe(`DROP SCHEMA IF EXISTS ${schema} CASCADE`); await conn.end({ timeout: 5 }); } }