Files
SGF/scripts/purchasing-completion-preservation.mjs
2026-11-04 09:37:20 +02:00

9 lines
2.3 KiB
JavaScript

import fs from 'node:fs';import path from 'node:path';import crypto from 'node:crypto';import assert from 'node:assert/strict';import {spawnSync} from 'node:child_process';
const run=JSON.parse(fs.readFileSync('C:/laragon/data/sgf-tempo/run/modular-regression-current.json')),root=path.join(run.root,'private/receipt-composition-cc50a276c10c'),s=JSON.parse(fs.readFileSync(path.join(root,'state.json'))),out=path.join(root,'completion-preservation-result.json');assert(!fs.existsSync(out)&&s.tag==='cc50a276c10c'&&s.env.DB_PORT==='55439'&&s.env.DB_DATABASE==='budget_core_sgf_cc50a276c10c');
const checks=[],tables={},digest=s=>crypto.createHash('sha256').update(s).digest('hex');
for(const table of ['permission_grants','cb_budget_entries']){
const dump=spawnSync('C:/Program Files/PostgreSQL/17/bin/pg_restore.exe',['-a','-t',table,'-f','-',path.join(root,'completion-backup/host.dump')],{encoding:'utf8',windowsHide:true,maxBuffer:2*1024*1024});assert.equal(dump.status,0,'Private baseline extraction');const lines=dump.stdout.replaceAll('\r\n','\n').split('\n'),start=lines.findIndex(line=>line.startsWith('COPY public.'+table+' ('));assert(start>=0);const columns=/\((.+)\) FROM stdin;/.exec(lines[start])[1],end=lines.indexOf('\\.',start),before=lines.slice(start+1,end).sort();assert(end>start);
const current=spawnSync('C:/Program Files/PostgreSQL/17/bin/psql.exe',['-h','127.0.0.1','-p','55439','-U',s.env.DB_USERNAME,'-d',s.env.DB_DATABASE,'-Atq','-v','ON_ERROR_STOP=1','-c',`COPY public.${table} (${columns}) TO STDOUT`],{env:{...process.env,PGPASSWORD:s.env.DB_PASSWORD},encoding:'utf8',windowsHide:true});assert.equal(current.status,0,'Selected installation read');const after=current.stdout.replaceAll('\r\n','\n').split('\n');if(after.at(-1)==='')after.pop();after.sort();assert.deepEqual(after,before,table+' must remain unchanged');tables[table]={rows:before.length,before_sha256:digest(before.join('\n')),after_sha256:digest(after.join('\n'))};checks.push(table==='permission_grants'?'C10 nenhuma concessão alterada durante a conclusão':'C10 orçamento do host intacto sem autoridade concorrente');
}
fs.writeFileSync(out,JSON.stringify({passed:true,checks,tables,baseline:'private pre-flow backup',database_writes:false,restored_database:false,new_database:false},null,2));console.log(JSON.stringify({passed:true,checks:checks.length}));