getLine().PHP_EOL);exit(1);}); putenv('STABILITY_QA_MODE=consumers'); $root=dirname(__DIR__,2);$qa=$root.'/storage/app/private/investmoz-v2/portal-audit/portal-stability-source-20261011_083742'; $app=require $qa.'/tools/portal-stability-runtime.php'; $db=Illuminate\Support\Facades\DB::connection();$c=config('database.connections.'.config('database.default')); $target=(array)$db->selectOne('SELECT current_database() AS database,inet_server_port() AS port'); if($target!==['database'=>'stability_consumers_20261011_084534_test','port'=>55441])throw new RuntimeException('QA source required'); $dir='C:/Users/macbook/.config/investmoz/B-final-recovery-'.gmdate('Ymd_His'); if(is_dir($dir)||!mkdir($dir,0700,true))throw new RuntimeException('New private recovery directory required'); $restore='b_final_restore_'.gmdate('Ymd_His').'_test'; if(!preg_match('/^b_final_restore_\d{8}_\d{6}_test$/D',$restore))throw new RuntimeException('Isolated restore required'); $input=json_decode(file_get_contents(__DIR__.'/B-final-inputs-20261015.json'),true,512,JSON_THROW_ON_ERROR); $run=function(array $args)use($c){$p=new Symfony\Component\Process\Process($args,null,['PGPASSWORD'=>$c['password']]);$p->setTimeout(240);$p->run();if(!$p->isSuccessful())throw new RuntimeException('Recovery subprocess failed');}; $source=$db->getPdo();$source->exec('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY'); try { $snapshot=$source->query('SELECT pg_export_snapshot()')->fetchColumn();$dump=$dir.'/database-QA.dump';$bin='C:/Program Files/PostgreSQL/17/bin/'; $run([$bin.'pg_dump.exe','-h','127.0.0.1','-p','55441','-U',$c['username'],'-d',$target['database'],'-Fc','--snapshot='.$snapshot,'-f',$dump]); $admin=new PDO('pgsql:host=127.0.0.1;port=55441;dbname=postgres',$c['username'],$c['password'],[PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION]); if((int)$admin->query('SELECT inet_server_port()')->fetchColumn()!==55441)throw new RuntimeException('Restore port guard'); $admin->exec('CREATE DATABASE "'.$restore.'"'); $run([$bin.'pg_restore.exe','-h','127.0.0.1','-p','55441','-U',$c['username'],'-d',$restore,'--exit-on-error','--no-owner','--no-privileges',$dump]); $clone=new PDO('pgsql:host=127.0.0.1;port=55441;dbname='.$restore,$c['username'],$c['password'],[PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION]);$clone->exec('BEGIN READ ONLY'); $tables=[]; foreach($source->query("SELECT schemaname,tablename FROM pg_tables WHERE schemaname NOT IN ('pg_catalog','information_schema') ORDER BY schemaname,tablename")->fetchAll(PDO::FETCH_ASSOC)as$t){ $name='"'.str_replace('"','""',$t['schemaname']).'"."'.str_replace('"','""',$t['tablename']).'"';$sql='SELECT row_to_json(t)::text FROM '.$name.' t ORDER BY row_to_json(t)::text'; $a=$source->query($sql)->fetchAll(PDO::FETCH_COLUMN);$b=$clone->query($sql)->fetchAll(PDO::FETCH_COLUMN);if($a!==$b)throw new RuntimeException('Restored table differs'); $tables[$name]=['rows'=>count($a),'sha256'=>hash('sha256',implode("\n",$a))]; } $sql="SELECT schemaname,sequencename,data_type,start_value,min_value,max_value,increment_by,cycle,cache_size,last_value FROM pg_sequences WHERE schemaname NOT IN ('pg_catalog','information_schema') ORDER BY schemaname,sequencename"; $seq=$source->query($sql)->fetchAll(PDO::FETCH_ASSOC);if($seq!==$clone->query($sql)->fetchAll(PDO::FETCH_ASSOC))throw new RuntimeException('Sequence mismatch'); $copy=function($from,$relative)use($dir){$to=$dir.'/files/'.$relative;$restored=$dir.'/file-restore/'.$relative;if(!is_file($from))throw new RuntimeException('Recovery file missing');foreach([$to,$restored]as$p){if(!is_dir(dirname($p)))mkdir(dirname($p),0700,true);if(!copy($from,$p)||hash_file('sha256',$from)!==hash_file('sha256',$p))throw new RuntimeException('File recovery mismatch');}return hash_file('sha256',$from);}; $files=[];foreach($input['source_files']as$n=>$h){if(hash_file('sha256',$qa.'/'.$n)!==$h)throw new RuntimeException('Source changed');$files['candidate/'.$n]=$copy($qa.'/'.$n,'candidate/'.$n);} foreach($input['build_files']as$n=>$h){if(hash_file('sha256',$qa.'/public/build/'.$n)!==$h)throw new RuntimeException('Bundle changed');$files['candidate/public/build/'.$n]=$copy($qa.'/public/build/'.$n,'candidate/public/build/'.$n);} $copy($qa.'/.env.stability-consumers','private-runtime/.env.stability-consumers'); $disk=Illuminate\Support\Facades\Storage::disk('local');$missing=[];$documents=0; foreach($source->query("SELECT id,storage_key FROM documents WHERE storage_key IS NOT NULL ORDER BY id")->fetchAll(PDO::FETCH_ASSOC)as$d){ $key=$d['storage_key'];if(!$disk->exists($key)){$missing[]=$d['id'];continue;} $files['document/'.$d['id']]=$copy($disk->path($key),'documents/'.$d['id'].'.stored');$documents++; } $clone->exec('ROLLBACK'); $private=['at'=>gmdate('c'),'source'=>$target,'restore'=>['database'=>$restore,'port'=>55441],'dump_sha256'=>hash_file('sha256',$dump),'tables'=>$tables,'sequence_count'=>count($seq),'files'=>$files,'missing_document_ids'=>$missing,'restore_verified'=>true,'activation_authorized'=>false]; file_put_contents($dir.'/proof-private.json',json_encode($private,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_THROW_ON_ERROR)); $safe=['at'=>$private['at'],'source'=>$target,'restore'=>$private['restore'],'database_restore_verified'=>true,'all_table_rows_hashes_equal'=>true,'table_count'=>count($tables),'sequence_count'=>count($seq),'sequences_equal'=>true,'candidate_source_and_build_files_restored'=>count($input['source_files'])+count($input['build_files']),'file_restore_hashes_equal'=>true,'existing_document_files_copied_and_restored'=>$documents,'preexisting_unavailable_document_count'=>count($missing),'private_dump_sha256'=>$private['dump_sha256'],'private_recovery_directory'=>$dir,'dump_credentials_runtime_config_not_in_review_ZIP'=>true,'application_boot_in_restored_database_executed'=>false,'functional_tests_repeated'=>false,'QA_source_writes'=>0,'active_changes'=>0,'activation_authorized'=>false]; file_put_contents(__DIR__.'/B-final-backup-restoration-20261015.json',json_encode($safe,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_THROW_ON_ERROR).PHP_EOL);echo json_encode($safe,JSON_UNESCAPED_UNICODE).PHP_EOL; }finally{$source->exec('ROLLBACK');}