candidate-code.mjs 1.4 KB

123456789101112131415
  1. import {patchAdminReadHelpers} from '../../scripts/admin-read-performance-patch.mjs';
  2. import {readFileSync} from 'node:fs';
  3. // Replace only the two measured read helpers in an exact online baseline.
  4. export function candidateCode(baseline,{temporaryView=false}={}){
  5. baseline=patchAdminReadHelpers(baseline);
  6. if(!temporaryView)return baseline;
  7. return withTemporaryIncomeView(baseline);
  8. }
  9. export function withTemporaryIncomeView(baseline,legacy=false){
  10. // A transaction-local view exercises the SQL migration without changing the live view.
  11. // It is dropped before releasing the pooled connection; failures roll the DDL back.
  12. 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');
  13. 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`;
  14. return proxy+baseline.replaceAll('Psql.','__candidateSql.');
  15. }