import {patchAdminReadHelpers} from '../../scripts/admin-read-performance-patch.mjs'; import {readFileSync} from 'node:fs'; // Replace only the two measured read helpers in an exact online baseline. export function candidateCode(baseline,{temporaryView=false}={}){ baseline=patchAdminReadHelpers(baseline); if(!temporaryView)return baseline; return withTemporaryIncomeView(baseline); } export function withTemporaryIncomeView(baseline,legacy=false){ // A transaction-local view exercises the SQL migration without changing the live view. // It is dropped before releasing the pooled connection; failures roll the DDL back. const ddl=readFileSync(legacy?'perf/admin-data-2026-10-15/legacy-income-view.sql':'scripts/sql/admin-read-performance.sql','utf8').replace('CREATE OR REPLACE VIEW','CREATE TEMP VIEW'); const proxy=`const __candidateSql=new Proxy({},{get(_,key){const fn=Psql[key];if(!['query','one','oneOrNone','any','manyOrNone'].includes(key))return typeof fn==='function'?fn.bind(Psql):fn;return function(sql,args){if(typeof sql!=='string'||!/ReviewRewardIncome|xs_payroll_cutoff_snapshot|xs_payroll_source_version/.test(sql))return fn.call(Psql,sql,args);return Psql.tx(async tx=>{await tx.none('SET LOCAL search_path=pg_temp,public');await tx.none(${JSON.stringify(ddl)});const result=await tx[key](sql,args);await tx.none('DROP VIEW pg_temp."ReviewRewardIncome"');return result;});};}});\n`; return proxy+baseline.replaceAll('Psql.','__candidateSql.'); }