9 lines
3.1 KiB
JavaScript
9 lines
3.1 KiB
JavaScript
import fs from 'node:fs';import path from 'node:path';import crypto from 'node:crypto';import {spawnSync} from 'node:child_process';
|
|
const read=p=>JSON.parse(fs.readFileSync(p,'utf8')),state=read('C:/laragon/data/sgf-tempo/run/modular-regression-current.json');if(state.port!==55439)throw Error('Owned QA cluster required');const oldRoot=path.join(state.root,'private/payable-history-03e16d9fb37d'),old=read(path.join(oldRoot,'settings.json'));if(old.cluster!=='sgf_modular_regression_'+state.id)throw Error('Source cluster mismatch');const tag=crypto.randomBytes(6).toString('hex'),root=path.join(state.root,'private/report-profile-'+tag),role='payable_history_sgf_'+tag,database='payable_sgf_'+tag,password=crypto.randomBytes(32).toString('hex');fs.mkdirSync(root);const bin='C:/Program Files/PostgreSQL/17/bin/',admin=fs.readFileSync(path.join(state.root,'private/admin-password.txt'),'utf8').trim();
|
|
const run=(exe,args,pass,input)=>{const r=spawnSync(bin+exe,args,{input,encoding:'utf8',windowsHide:true,env:{...process.env,PGPASSWORD:pass},maxBuffer:1024*1024});if(r.status!==0){fs.appendFileSync(path.join(root,'private-diagnostics.log'),r.stderr??'');throw Error('Isolated clone operation failed; diagnostics remain private');}return r.stdout.trim();};const base=['-h','127.0.0.1','-p','55439'];if(run('psql.exe',[...base,'-U','sgf_regression_owner','-d','postgres','-Atq'],admin,"SELECT current_setting('cluster_name')")!==old.cluster)throw Error('Effective QA cluster mismatch');
|
|
run('pg_dump.exe',[...base,'-U',old.sgf.role,'-d',old.sgf.database,'--no-owner','--no-acl','-Fc','-f',path.join(root,'qa.dump')],old.sgf.password);
|
|
run('psql.exe',[...base,'-U','sgf_regression_owner','-d','postgres','-v','ON_ERROR_STOP=1','-q'],admin,`CREATE ROLE ${role} LOGIN PASSWORD '${password}' NOSUPERUSER NOCREATEDB NOCREATEROLE NOREPLICATION NOBYPASSRLS; CREATE DATABASE ${database} OWNER ${role}; REVOKE ALL ON DATABASE ${database} FROM PUBLIC;`);
|
|
run('pg_restore.exe',[...base,'-U',role,'-d',database,'--no-owner','--no-acl','--exit-on-error',path.join(root,'qa.dump')],password);
|
|
const storage=path.join(root,'storage');for(const d of ['framework/cache/data','framework/views','framework/sessions','logs','cache'])fs.mkdirSync(path.join(storage,d),{recursive:true});const settings={...old,tag,root,sgf:{...old.sgf,database,role,password,storage},profile_clone_of:'03e16d9fb37d'};fs.writeFileSync(path.join(root,'settings.json'),JSON.stringify(settings,null,2));for(const f of ['fixture.json','source.json'])fs.copyFileSync(path.join(oldRoot,f),path.join(root,f));
|
|
let source=fs.readFileSync('app/Modules/Purchasing/SupplierPayables.php','utf8').replace('final class SupplierPayables','final class ProfileBaselineSupplierPayables');fs.writeFileSync(path.join(root,'baseline.php'),source);const archived=crypto.createHash('sha256').update(source).digest('hex');fs.writeFileSync(path.join(root,'clone.json'),JSON.stringify({passed:true,tag,source_tag:'03e16d9fb37d',original_database_not_modified:true,baseline_sha256:archived,new_owned_database:true,synthetic_only:true},null,2));console.log(JSON.stringify({tag,root,synthetic_clone_only:true}));
|