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')); const state=read('C:/laragon/data/sgf-tempo/run/modular-regression-current.json'); if(state.port!==55439||!/^[a-f0-9]{12}$/.test(state.id))throw Error('Wrong QA host'); const sourceTag=process.argv[2];if(!/^[a-f0-9]{12}$/.test(sourceTag||''))throw Error('Original historical QA source tag required'); const original=path.join(state.root,'private/payable-history-'+sourceTag),bundle=read(path.join(original,'source.json')); const tag=crypto.randomBytes(6).toString('hex'),root=path.join(state.root,'private/payable-history-'+tag),role='payable_history_sgf_'+tag,database='payable_sgf_'+tag; const password=crypto.randomBytes(32).toString('hex'),admin=fs.readFileSync(path.join(state.root,'private/admin-password.txt'),'utf8').trim(); const pg=input=>{const r=spawnSync('C:/Program Files/PostgreSQL/17/bin/psql.exe',['-h','127.0.0.1','-p','55439','-U','sgf_regression_owner','-d','postgres','-Atq','-v','ON_ERROR_STOP=1'],{input,encoding:'utf8',windowsHide:true,env:{...process.env,PGPASSWORD:admin}});if(r.status!==0)throw Error('Private QA provisioning refused');return r.stdout.trim();}; if(pg("SELECT current_setting('cluster_name')")!=='sgf_modular_regression_'+state.id)throw Error('Wrong effective cluster'); pg(`CREATE ROLE ${role} LOGIN PASSWORD '${password}' NOSUPERUSER NOCREATEDB NOCREATEROLE NOREPLICATION NOBYPASSRLS; CREATE DATABASE ${database} OWNER ${role}; REVOKE ALL ON DATABASE ${database} FROM PUBLIC;`); const storage=path.join(root,'sgf');for(const d of ['framework/cache/data','framework/views','framework/sessions','logs','cache'])fs.mkdirSync(path.join(storage,d),{recursive:true}); const config={tag,root,cluster:'sgf_modular_regression_'+state.id,source_instance:bundle.source.instance,original_source_tag:sourceTag,sgf:{root:process.cwd(),database,role,password,storage}}; fs.writeFileSync(path.join(root,'settings.json'),JSON.stringify(config,null,2));fs.copyFileSync(path.join(original,'source.json'),path.join(root,'source.json')); for(const phase of ['sgf','regression']){const r=spawnSync('C:/laragon/tools/sgf-tempo/php-8.4.27/php.exe',['scripts/payable-history-sgf-proof.php',path.join(root,'settings.json'),phase],{encoding:'utf8',windowsHide:true});fs.writeFileSync(path.join(root,phase+'-initial-stdout.txt'),r.stdout||'');fs.writeFileSync(path.join(root,phase+'-initial-stderr.txt'),r.stderr||'');if(r.status!==0||!fs.existsSync(path.join(root,phase==='sgf'?'fixture.json':'regression.json')))throw Error('Private '+phase+' failed at '+root);process.stdout.write(r.stdout);} fs.writeFileSync(path.join(state.root,'payable-page-latest.json'),JSON.stringify({tag,root},null,2));console.log(JSON.stringify({tag,root,source_qa_preserved:true,new_sgf_database_only:true}));