19 lines
5.9 KiB
PHP
19 lines
5.9 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||!in_array($c['host'],['127.0.0.1','localhost'])||!empty($c['url']))throw new RuntimeException('Target');
|
|
$b=json_decode(file_get_contents(__DIR__.'/FRESH-BACKUP.json'),true);$active=new PDO('pgsql:host='.$c['host'].';port=5432;dbname=investemoz',$c['username'],$c['password'],[PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION]);$clone=new PDO('pgsql:host=127.0.0.1;port=55441;dbname='.$b['restore_target']['database'],'postgres','',[PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION]);
|
|
foreach([$active,$clone]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'];$tables=[];$lost=[];$new=[];
|
|
foreach($clone->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=$t['tablename'];$q='"'.str_replace('"','""',$t['schemaname']).'"."'.str_replace('"','""',$n).'"';$sql='SELECT row_to_json(t)::text FROM '.$q.' t ORDER BY row_to_json(t)::text';$before=$clone->query($sql)->fetchAll(PDO::FETCH_COLUMN);$after=$active->query($sql)->fetchAll(PDO::FETCH_COLUMN);$missing=array_values(array_diff($before,$after));$added=array_values(array_diff($after,$before));if($missing&&!in_array($n,$technical,true))$lost[$n]=count($missing);if($added)$new[$n]=count($added);$tables[$q]=['before'=>count($before),'after'=>count($after),'old_rows_preserved'=>!$missing,'technical'=>in_array($n,$technical,true),'new_rows'=>count($added),'after_sha256'=>hash('sha256',implode("\n",$after))];}
|
|
$release=$root.'/outputs/investmoz-fintech-investimento-QA-20261015';$m=json_decode(file_get_contents($release.'/MANIFESTO-ENTREGA.json'),true);$p=json_decode(file_get_contents(__DIR__.'/PREFLIGHT.json'),true);$files=[];foreach($p['protected_files']as$n=>$h){$actual=hash_file('sha256',$root.'/'.$n);if($actual!==$h)throw new RuntimeException('Protected config changed '.$n);$files[$n]=$actual;}
|
|
foreach($m['sources']as$s)if(hash_file('sha256',$root.'/'.$s['path'])!==$s['candidate_sha256'])throw new RuntimeException('Source mismatch');foreach($m['build']['files']as$f)if(hash_file('sha256',$root.'/public/build/'.$f['path'])!==$f['sha256'])throw new RuntimeException('Build mismatch');
|
|
$previous=json_decode(file_get_contents($release.'/ATIVO-PRESERVADO-READONLY.json'),true);$allowed=array_column($m['sources'],'path');foreach($m['build']['files']as$f)$allowed[]='public/build/'.$f['path'];$protected=0;foreach($previous['protected_files']as$n=>$h){if(in_array($n,$allowed,true))continue;if(!is_file($root.'/'.$n)||hash_file('sha256',$root.'/'.$n)!==$h)throw new RuntimeException('Prior protected file changed '.$n);$protected++;}
|
|
$private=json_decode(file_get_contents($b['private_directory'].'/backup-proof-private.json'),true);$documentBytes=0;foreach($clone->query('SELECT id,storage_key FROM documents WHERE storage_key IS NOT NULL')->fetchAll(PDO::FETCH_ASSOC)as$d){$disk=config('filesystems.disks.local.root');if(hash_file('sha256',$disk.'/'.$d['storage_key'])!==$private['file_hashes']['storage-documents/'.$d['id'].'.stored'])throw new RuntimeException('Original document changed');$documentBytes++;}
|
|
$migration=(int)$active->query("SELECT count(*) FROM migrations WHERE migration='2026_10_09_190000_prepare_investor_path'")->fetchColumn();$env=Dotenv\Dotenv::parse(file_get_contents($root.'/.env'));if($migration!==1||!filter_var($env['INVESTOR_PATH_ENABLED']??false,FILTER_VALIDATE_BOOL)||!filter_var($env['PORTAL_LEGACY_CONTENT_ENABLED']??false,FILTER_VALIDATE_BOOL))throw new RuntimeException('Existing flags or B changed');
|
|
$revisions=$active->query('SELECT resource_id,revision_no,change_type,actor_user_id,created_at,snapshot FROM portal_resource_revisions WHERE resource_id IN (27,28,29) ORDER BY resource_id,revision_no')->fetchAll(PDO::FETCH_ASSOC);$creation=[];foreach($revisions as&$r){$snap=json_decode($r['snapshot'],true);unset($r['snapshot']);if($r['change_type']==='created'){$rr=$snap['resource'];if($rr['published']!==false&&$rr['published']!==0)throw new RuntimeException('Initial publication');$creation[]=['resource_id'=>$r['resource_id'],'initial_published'=>false,'initial_rights'=>$rr['rights_status'],'initial_version'=>$rr['metadata_version']];}}
|
|
$unexpected=array_diff(array_keys($new),['documents','portal_resources','portal_resource_revisions','audit_events','document_malware_scans',...$technical]);
|
|
$out=['at'=>gmdate('c'),'candidate'=>$m['candidate'],'read_only'=>true,'fresh_restore_comparison'=>$b['restore_target'],'tables_compared'=>count($tables),'preexisting_business_rows_preserved'=>!$lost,'lost_or_modified_business_rows'=>$lost,'added_rows_by_table'=>$new,'unexpected_added_tables'=>array_values($unexpected),'protected_files_unchanged'=>$protected,'protected_config_hashes'=>$files,'prior_document_bytes_preserved'=>$documentBytes,'sources_verified'=>16,'build_files_verified'=>173,'B_190000_installed'=>$migration,'flags_unchanged'=>true,'article_creation_was_unpublished'=>$creation,'article_revision_audit'=>$revisions,'tables'=>$tables,'functional_acceptance_repeated'=>false];
|
|
if(is_file(__DIR__.'/PRESERVACAO-FINAL.json'))throw new RuntimeException('Preserve final receipt');file_put_contents(__DIR__.'/PRESERVACAO-FINAL.json',json_encode($out,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_THROW_ON_ERROR));echo json_encode(array_diff_key($out,array_flip(['tables','protected_config_hashes','article_revision_audit'])),JSON_UNESCAPED_UNICODE|JSON_THROW_ON_ERROR);if($lost||$unexpected)exit(2);
|
|
}finally{foreach([$active,$clone]as$db)$db->exec('ROLLBACK');}
|