25 lines
4.4 KiB
PHP
25 lines
4.4 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('Exact active target');
|
|
$b=json_decode(file_get_contents(__DIR__.'/FRESH-BACKUP.json'),true,512,JSON_THROW_ON_ERROR);$name=$b['restore_target']['database'];if(!preg_match('/^editorial_before_\d{8}_\d{6}_test$/D',$name))throw new RuntimeException('Exact restored QA');
|
|
$db=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='.$name,'postgres','',[PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION]);
|
|
foreach([$db,$clone]as$x){$x->exec('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY');$x->exec("SET LOCAL TIME ZONE 'UTC'");}
|
|
try{
|
|
$migration='2026_10_18_160000_add_portal_editorial_content';$tables=[];
|
|
foreach(['documents','portal_resources','portal_resource_links','portal_resource_revisions','audit_events','opportunities','desk_cases','investor_journeys','investment_submissions','interest_requests','investor_navigation_preferences','economic_activities','investment_zones','migrations','portal_role_assignments','portal_superuser_pair'] as$table){
|
|
$expression=$table==='portal_resources'?"(to_jsonb(t)-'editorial_content')::text":"to_jsonb(t)::text";
|
|
$where=$table==='audit_events'?" WHERE source_system IS DISTINCT FROM 'editorial-schema-installation'":($table==='migrations'?" WHERE migration <> '".$migration."'":'');
|
|
$sql='SELECT '.$expression.' FROM "'.$table.'" t'.$where.' ORDER BY 1';$a=$db->query($sql)->fetchAll(PDO::FETCH_COLUMN);$z=$clone->query($sql)->fetchAll(PDO::FETCH_COLUMN);if($a!==$z)throw new RuntimeException('Concurrent/unexpected change: '.$table);
|
|
$tables[$table]=['rows'=>count($a),'sha256'=>hash('sha256',implode("\n",$a))];
|
|
}
|
|
$q=$db->prepare('SELECT count(*) FROM migrations WHERE migration=?');$q->execute([$migration]);if((int)$q->fetchColumn()!==1)throw new RuntimeException('Nominal ledger not unique');
|
|
$shape=$db->query("SELECT data_type,is_nullable FROM information_schema.columns WHERE table_schema='public' AND table_name='portal_resources' AND column_name='editorial_content'")->fetch(PDO::FETCH_ASSOC);if($shape!==['data_type'=>'jsonb','is_nullable'=>'YES'])throw new RuntimeException('Nominal shape');
|
|
if((int)$db->query('SELECT count(*) FROM portal_resources WHERE editorial_content IS NOT NULL')->fetchColumn()!==0)throw new RuntimeException('Unexpected editorial data created');
|
|
$events=$db->query("SELECT actor_user_id,correlation_id,after_data FROM audit_events WHERE source_system='editorial-schema-installation' ORDER BY occurred_at")->fetchAll(PDO::FETCH_ASSOC);if(count($events)!==1)throw new RuntimeException('Expected one nominal audit receipt');
|
|
$event=$events[0];$result=json_decode($event['after_data'],true)['result'];if((int)$event['actor_user_id']!==2||!$result['changed']||$result['business_records_changed']!==0||$result['code_hash']!=='07843a633f9f4cec3989f465957d4d64c7c99c45e8a6c55d08a9d59027397085')throw new RuntimeException('Receipt does not match actor/revision/scope');
|
|
$ledger=[];foreach(['2026_10_16_140000_add_investment_zone_operational_details','2026_10_16_150000_create_investor_navigation_preferences','2026_10_09_190000_prepare_investor_path',$migration] as$n){$q->execute([$n]);$ledger[$n]=(int)$q->fetchColumn();if($ledger[$n]!==1)throw new RuntimeException('Existing/nominal migration count');}
|
|
$proof=['candidate'=>$b['candidate'],'at'=>gmdate('c'),'read_only'=>true,'business_records_changed'=>0,'protected_tables'=>$tables,'allowed_changes'=>['one nullable JSONB column','one160000 ledger row','one authenticated installation audit event'],'shape'=>$shape,'ledger'=>$ledger,'actor_user_id'=>2,'receipt'=>$result,'permission_changes'=>0];
|
|
file_put_contents(__DIR__.'/PRESERVACAO-FINAL.json',json_encode($proof,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE));echo json_encode(['tables_preserved'=>count($tables),'nominal_installation_verified'=>true,'business_records_changed'=>0,'actor_user_id'=>2,'ledger'=>$ledger]).PHP_EOL;
|
|
}finally{$db->exec('ROLLBACK');$clone->exec('ROLLBACK');}
|