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

31 lines
4.4 KiB
PHP

<?php
ini_set('zend.exception_ignore_args','1');
set_exception_handler(function(Throwable $e){fwrite(STDERR,'Preservation check stopped: '.$e::class.' 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['driver']!=='pgsql'||$c['database']!=='investemoz'||(int)$c['port']!==5432||!in_array($c['host'],['127.0.0.1','localhost'])||!empty($c['url']))throw new RuntimeException('Active identity guard');
$b=json_decode(file_get_contents(__DIR__.'/FRESH-BACKUP.json'),true);$dbName=$b['restore_target']['database'];
if(!preg_match('/^promoter_before_\d{8}_\d{6}_test$/D',$dbName))throw new RuntimeException('Restored clone guard');
$a=new PDO('pgsql:host='.$c['host'].';port=5432;dbname=investemoz',$c['username'],$c['password'],[PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION]);
$q=new PDO('pgsql:host=127.0.0.1;port=55441;dbname='.$dbName,'postgres','',[PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION]);
foreach([$a,$q]as$db){$db->exec('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY');$db->exec("SET LOCAL TIME ZONE 'UTC'");}
try{
$technical=['sessions','cache','cache_locks','jobs','failed_jobs'];$diff=[];$technicalDiff=[];$tables=[];
foreach($q->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){
$n='"'.str_replace('"','""',$t['schemaname']).'"."'.str_replace('"','""',$t['tablename']).'"';$sql='SELECT row_to_json(t)::text FROM '.$n.' t ORDER BY row_to_json(t)::text';
$x=$q->query($sql)->fetchAll(PDO::FETCH_COLUMN);$y=$a->query($sql)->fetchAll(PDO::FETCH_COLUMN);
if($x!==$y){if(in_array($t['tablename'],$technical,true))$technicalDiff[]=$t['tablename'];else $diff[]=$n;}
$tables[$n]=['rows_before'=>count($x),'rows_after'=>count($y),'identical'=>$x===$y,'before_sha256'=>hash('sha256',implode("\n",$x)),'after_sha256'=>hash('sha256',implode("\n",$y))];
}
$release=$root.'/outputs/investmoz-promotor-contexto-QA-20261015';$m=json_decode(file_get_contents($release.'/MANIFESTO-DELTA.json'),true);$base=json_decode(file_get_contents($release.'/ATIVO-ANTES.json'),true);$allowed=array_column($m['sources'],'path');$count=0;
foreach(array_merge($base['files'],$base['protected'])as$n=>$h){if(in_array($n,$allowed,true))continue;if(!is_file($root.'/'.$n)||hash_file('sha256',$root.'/'.$n)!==$h)throw new RuntimeException('Unrelated protected file changed');$count++;}
foreach($m['sources']as$s)if(hash_file('sha256',$root.'/'.$s['path'])!==$s['candidate_sha256'])throw new RuntimeException('Delivered source hash mismatch');
$private=json_decode(file_get_contents($b['private_directory'].'/backup-proof-private.json'),true);$files=0;$docs=0;
foreach($private['file_hashes']as$n=>$h){if(!str_starts_with($n,'storage/app/'))continue;if(!is_file($root.'/'.$n)||hash_file('sha256',$root.'/'.$n)!==$h)throw new RuntimeException('Managed file changed');$files++;}
$disk=config('filesystems.disks.local.root');foreach($q->query('SELECT id,storage_key FROM documents WHERE storage_key IS NOT NULL')->fetchAll(PDO::FETCH_ASSOC)as$d){$n='storage-documents/'.$d['id'].'.stored';if(!isset($private['file_hashes'][$n]))continue;if(!is_file($disk.'/'.$d['storage_key'])||hash_file('sha256',$disk.'/'.$d['storage_key'])!==$private['file_hashes'][$n])throw new RuntimeException('Original document changed');$docs++;}
$result=['at'=>gmdate('c'),'candidate'=>$m['candidate'],'read_only'=>true,'tables_compared'=>count($tables),'business_and_history_differences'=>$diff,'technical_table_differences'=>$technicalDiff,'technical_tables'=>$technical,'protected_unrelated_sources_assets_config'=>$count,'candidate_sources_verified'=>3,'managed_files_verified'=>$files,'original_documents_verified'=>$docs,'roles_and_elevations_preserved'=>!$diff,'business_records_unchanged'=>!$diff,'tables'=>$tables];
if(is_file(__DIR__.'/PRESERVACAO-FINAL.json'))throw new RuntimeException('Preserve proof');file_put_contents(__DIR__.'/PRESERVACAO-FINAL.json',json_encode($result,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE).PHP_EOL);
echo json_encode(array_diff_key($result,['tables'=>true])).PHP_EOL;if($diff)exit(2);
}finally{foreach([$a,$q]as$db)$db->exec('ROLLBACK');}