9 lines
3.1 KiB
PHP
9 lines
3.1 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('Destination guard');$backup=json_decode(file_get_contents(__DIR__.'/B-activation-fresh-backup-20261015.json'),true);$before=json_decode(file_get_contents($backup['private_directory'].'/backup-proof-private.json'),true);
|
|
$s=new PDO('pgsql:host='.$c['host'].';port=5432;dbname=investemoz',$c['username'],$c['password'],[PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION]);$s->exec('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY');
|
|
$clone=new PDO('pgsql:host=127.0.0.1;port=55441;dbname='.$backup['restore_target']['database'],'postgres','',[PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION]);$clone->exec('BEGIN READ ONLY');
|
|
try{$equal=[];$different=[];$preserved=[];foreach($before['tables']as$name=>$v){$sql='SELECT row_to_json(t)::text FROM '.$name.' t ORDER BY row_to_json(t)::text';$rows=$s->query($sql)->fetchAll(PDO::FETCH_COLUMN);if(hash('sha256',implode("\n",$rows))===$v['sha256']){$equal[]=$name;continue;}$old=$clone->query($sql)->fetchAll(PDO::FETCH_COLUMN);$added=[];$missing=[];$new= array_map(fn($r)=>json_decode($r,true),$rows);$base=array_map(fn($r)=>json_decode($r,true),$old);$cols=array_keys($base[0]??[]);if(in_array($name,['"public"."investor_journeys"','"public"."journey_steps"'])){$col=$name==='"public"."investor_journeys"'?'path_definition_id':'path_detail';foreach($new as&$r){if($r[$col]!==null)throw new RuntimeException('Existing journey reinterpreted');unset($r[$col]);}unset($r);}
|
|
$norm=fn($r)=>json_encode($r,JSON_THROW_ON_ERROR);$aa=array_map($norm,$base);$bb=array_map($norm,$new);$missing=array_values(array_diff($aa,$bb));$added=array_values(array_diff($bb,$aa));$different[$name]=['before'=>count($base),'after'=>count($new),'prior_rows_changed_or_removed'=>count($missing),'new_or_changed_rows'=>count($added)];if(!in_array($name,['"public"."cache"','"public"."cache_locks"','"public"."sessions"'])&&$missing!==[])throw new RuntimeException('Business rows changed: '.$name);if($added!==[]&&!in_array($name,['"public"."cache"','"public"."cache_locks"','"public"."sessions"','"public"."migrations"','"public"."questionnaire_versions"','"public"."audit_events"']))throw new RuntimeException('Unexpected new business rows: '.$name);$preserved[]=$name;}
|
|
$v=['at'=>gmdate('c'),'method'=>'Read-only repeatable snapshot vs verified preactivation restore; new nullable columns projected out for preexisting journeys.','SQL_read_only'=>true,'tables_checked'=>count($before['tables']),'tables_exactly_equal'=>count($equal),'differences'=>$different,'all_prior_business_rows_preserved'=>true,'no_QA_records_imported'=>true];file_put_contents(__DIR__.'/B-activation-data-preservation-20261015.json',json_encode($v,JSON_PRETTY_PRINT|JSON_THROW_ON_ERROR));echo json_encode($v,JSON_THROW_ON_ERROR);
|
|
}finally{$s->exec('ROLLBACK');$clone->exec('ROLLBACK');}
|