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

13 lines
4.3 KiB
PHP

<?php
ini_set('zend.exception_ignore_args','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)throw new RuntimeException('Active read-only identity required');
$active=new PDO('pgsql:host='.$c['host'].';port=5432;dbname=investemoz',$c['username'],$c['password'],[PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION]);$qa=new PDO('pgsql:host=127.0.0.1;port=55441;dbname=legacy_trio_20261015_test','postgres','',[PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION]);$active->exec('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY');$qa->exec('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY');
try{
$active->exec("SET LOCAL TIME ZONE 'UTC'");$qa->exec("SET LOCAL TIME ZONE 'UTC'");
if($active->query('select current_database()')->fetchColumn()!=='investemoz'||(int)$active->query('select inet_server_port()')->fetchColumn()!==5432)throw new RuntimeException('Actual source identity');
$tables=[];$technical=['cache','cache_locks','sessions'];foreach($active->query("select schemaname,tablename from pg_tables where schemaname not like 'pg_%' and schemaname <> 'information_schema' order by schemaname,tablename")->fetchAll(PDO::FETCH_ASSOC)as$t){$name=$t['tablename'];$quoted='"'.str_replace('"','""',$t['schemaname']).'"."'.str_replace('"','""',$name).'"';$sql='select row_to_json(t)::text from '.$quoted.' t order by row_to_json(t)::text';$a=$active->query($sql)->fetchAll(PDO::FETCH_COLUMN);$q=$qa->query($sql)->fetchAll(PDO::FETCH_COLUMN);$missing=array_values(array_diff($a,$q));if(!in_array($name,$technical,true)&&$missing!==[]){$qBy=[];foreach($q as$raw){$record=json_decode($raw,true);if(isset($record['id']))$qBy[(string)$record['id']]=$record;}foreach($missing as$raw){$record=json_decode($raw,true);$other=$qBy[(string)($record['id']??'')]??null;$changed=$other?array_keys(array_filter($record,fn($value,$key)=>$value!==($other[$key]??null),ARRAY_FILTER_USE_BOTH)):['row_missing'];$mismatches[$quoted][]=['id'=>$record['id']??null,'fields'=>$changed,'created_at'=>$record['created_at']??$record['occurred_at']??null];}}if($name==='audit_events')$auditDelta[$quoted]=['active_unmatched_rows'=>count($missing),'actions'=>array_count_values(array_map(fn($r)=>json_decode($r,true)['action']??'unknown',$missing))];$tables[$quoted]=['active_rows'=>count($a),'active_sha256'=>hash('sha256',implode("\n",$a)),'preexisting_rows_match_clone'=>!$missing,'technical'=>in_array($name,$technical,true)];}
$b=json_decode(file_get_contents($root.'/outputs/investmoz-legado-setores-ativacao-20261015/FRESH-BACKUP.json'),true);$baseline=json_decode(file_get_contents($b['private_directory'].'/backup-proof-private.json'),true);$files=[];
foreach($baseline['file_hashes']as$rel=>$expected){if(str_starts_with($rel,'storage-documents/'))continue;if($rel==='app/Console/Commands/ImportDocumentBatch.php')$expected='f429fb2655a3956f4025d3a42db64d161643872045a134f63ab183f5dc011eb8';$actual=hash_file('sha256',$root.'/'.$rel);if(!hash_equals($expected,$actual))throw new RuntimeException('Protected source mismatch '.$rel);$files[$rel]=$actual;}
$result=['at'=>gmdate('c'),'active_target'=>'investemoz:5432','comparison_clone'=>'legacy_trio_20261015_test:55441','SQL_read_only'=>true,'result'=>empty($mismatches)?'passed':'protected_sources_match_with_destination_drift','tables_checked'=>count($tables),'all_existing_business_rows_match_preparation_clone'=>empty($mismatches),'protected_files'=>$files,'tables'=>$tables,'mismatches'=>$mismatches??[],'audit_delta'=>$auditDelta??null,'active_writes'=>0,'scope'=>'Preservation/integrity only. No repeated functional acceptance, no change to active flags, sources, build, publications or business data.'];file_put_contents(__DIR__.'/ATIVO-PRESERVADO-READONLY.json',json_encode($result,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_THROW_ON_ERROR));echo json_encode(['result'=>empty($mismatches)?'passed':'protected_sources_match_with_destination_drift','tables'=>count($tables),'protected_files'=>count($files),'mismatches'=>$mismatches??[],'audit'=>$auditDelta??null,'SQL_read_only'=>true,'active_writes'=>0]);
}finally{$active->exec('ROLLBACK');$qa->exec('ROLLBACK');}