Files
Investidor-V2/outputs/investmoz-legado-trio-QA-20261015/prepare-isolated-QA.php
T
nala 6a76aac80f
tests / ci (push) Canceled after 0s
versao compilada 1
2026-10-16 09:17:28 +02:00

22 lines
3.2 KiB
PHP

<?php
ini_set('zend.exception_ignore_args', '1');
set_exception_handler(function(Throwable $e) { fwrite(STDERR, $e::class.': preparation stopped at line '.$e->getLine().PHP_EOL); exit(1); });
$root=dirname(__DIR__,2);require $root.'/vendor/autoload.php';$app=require $root.'/bootstrap/app.php';
(new Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables)->bootstrap($app);(new Illuminate\Foundation\Bootstrap\LoadConfiguration)->bootstrap($app);
$c=config('database.connections.'.config('database.default'));
if($c['database']!=='investemoz'||(int)$c['port']!==5432||!in_array($c['host'],['127.0.0.1','localhost'])||!empty($c['url']))throw new RuntimeException('Source identity');
$work='C:/Users/macbook/.config/investmoz/legacy-trio-QA-20261015';if(is_dir($work))throw new RuntimeException('Preserve QA run');mkdir($work,0700,true);
$target='legacy_trio_20261015_test';$source=new PDO('pgsql:host='.$c['host'].';port=5432;dbname=investemoz',$c['username'],$c['password'],[PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION]);$source->exec('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY');
$run=function(array $args,string $password){$p=new Symfony\Component\Process\Process($args,null,['PGPASSWORD'=>$password]);$p->setTimeout(240);$p->mustRun();};
try{
$id=$source->query('select current_database() as database,inet_server_port() as port')->fetch(PDO::FETCH_ASSOC);if($id['database']!=='investemoz'||(int)$id['port']!==5432)throw new RuntimeException('Actual source mismatch');
$snapshot=$source->query('select pg_export_snapshot()')->fetchColumn();$dump=$work.'/qa-source.dump';$bin='C:/Program Files/PostgreSQL/17/bin/';
$run([$bin.'pg_dump.exe','-h',$c['host'],'-p','5432','-U',$c['username'],'-d','investemoz','-Fc','--snapshot='.$snapshot,'-f',$dump],$c['password']);
$admin=new PDO('pgsql:host=127.0.0.1;port=55441;dbname=postgres','postgres','',[PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION]);if((int)$admin->query('select inet_server_port()')->fetchColumn()!==55441)throw new RuntimeException('QA port guard');
$q=$admin->prepare('select 1 from pg_database where datname=?');$q->execute([$target]);if($q->fetchColumn())throw new RuntimeException('QA clone already exists');
$admin->exec('CREATE DATABASE "'.$target.'"');$run([$bin.'pg_restore.exe','-h','127.0.0.1','-p','55441','-U','postgres','-d',$target,'--exit-on-error','--no-owner','--no-privileges',$dump],'');
$files=0;foreach($source->query('select storage_key,sha256 from documents where deleted_at is null')->fetchAll(PDO::FETCH_ASSOC) as $d){$from=config('filesystems.disks.local.root').'/'.$d['storage_key'];$to=$work.'/storage/app/private/'.$d['storage_key'];if(!is_dir(dirname($to)))mkdir(dirname($to),0700,true);if(!copy($from,$to)||hash_file('sha256',$to)!==trim($d['sha256']))throw new RuntimeException('QA original bytes mismatch');$files++;}
$v=['at'=>gmdate('c'),'source_read_only'=>$id,'target'=>['database'=>$target,'port'=>55441],'private_work'=>$work,'copied_originals_hash_verified'=>$files,'dump_sha256'=>hash_file('sha256',$dump),'purpose'=>'Isolated QA preparation; not a fresh operational activation backup','active_writes'=>0];
file_put_contents(__DIR__.'/BASELINE-QA.json',json_encode($v,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_THROW_ON_ERROR));echo json_encode($v);
}finally{$source->exec('ROLLBACK');}