Files
SGF/scripts/native-payable-page-run.mjs
2026-11-04 09:37:20 +02:00

11 lines
2.9 KiB
JavaScript

import fs from 'node:fs';import path from 'node:path';import crypto from 'node:crypto';import {spawnSync} from 'node:child_process';
const state=JSON.parse(fs.readFileSync('C:/laragon/data/sgf-tempo/run/modular-regression-current.json','utf8'));if(state.port!==55439||!/^[a-f0-9]{12}$/.test(state.id))throw Error('Wrong QA host');
const resume=process.argv[2];if(resume&&!/^[a-f0-9]{12}$/.test(resume))throw Error('Invalid owned tag');const tag=resume??crypto.randomBytes(6).toString('hex'),root=path.join(state.root,'private/payable-history-'+tag),settings=path.join(root,'settings.json');
if(!resume){
const 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('QA provisioning refused');return r.stdout.trim();};if(pg("SELECT current_setting('cluster_name')")!=='sgf_modular_regression_'+state.id)throw Error('Wrong actual cluster');
const role='payable_history_native_'+tag,database='compras_receipts_test_'+tag,password=crypto.randomBytes(32).toString('hex');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,'native');for(const d of ['framework/cache/data','framework/views','framework/sessions','logs','cache'])fs.mkdirSync(path.join(storage,d),{recursive:true});fs.writeFileSync(settings,JSON.stringify({tag,root,cluster:'sgf_modular_regression_'+state.id,source_instance:'qa-native-page-'+tag,app_key:'base64:'+crypto.randomBytes(32).toString('base64'),native:{root:path.resolve('../compras'),database,role,password,storage}},null,2));
}
const phases=process.argv[3]?process.argv[3].split(','):(resume?[]:['native','native-page-setup']);
for(const phase of phases){const expected={native:'source.json','native-page-setup':'native-page-fixture.json','native-page-post':'native-page-post.json'}[phase];if(!expected)throw Error('Unknown setup phase');const started=Date.now();const r=spawnSync('C:/laragon/tools/sgf-tempo/php-8.4.27/php.exe',['scripts/payable-history-sgf-proof.php',settings,phase],{encoding:'utf8',windowsHide:true,maxBuffer:1024*1024});fs.writeFileSync(path.join(root,phase+'-'+started+'.log'),(r.stdout||'')+(r.stderr||''));if(r.status!==0||!fs.existsSync(path.join(root,expected))||fs.statSync(path.join(root,expected)).mtimeMs<started-2000)throw Error('Private setup failed at '+root);process.stdout.write(r.stdout);}
fs.writeFileSync(path.join(state.root,'native-payable-page-latest.json'),JSON.stringify({tag,root},null,2));console.log(JSON.stringify({tag,root,owned_native_database_only:true,production_and_demos_untouched:true}));