27 lines
2.8 KiB
PHP
27 lines
2.8 KiB
PHP
<?php
|
|
ini_set('zend.exception_ignore_args','1');
|
|
set_exception_handler(function(Throwable $e){fwrite(STDERR,'L1 read-only verification 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('Destination guard');
|
|
$db=new PDO('pgsql:host='.$c['host'].';port=5432;dbname=investemoz',$c['username'],$c['password'],[PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION]);$db->exec('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY');$db->exec("SET LOCAL TIME ZONE 'UTC'");
|
|
try{
|
|
$migration='2026_10_16_140000_add_investment_zone_operational_details';
|
|
$b=json_decode(file_get_contents(__DIR__.'/FRESH-BACKUP.json'),true,512,JSON_THROW_ON_ERROR);
|
|
$p=json_decode(file_get_contents($b['private_directory'].'/backup-proof-private.json'),true,512,JSON_THROW_ON_ERROR);
|
|
$diff=[];$identical=0;
|
|
foreach($p['tables'] as $name=>$before){
|
|
$sql='SELECT row_to_json(t)::text FROM '.$name.' t ORDER BY row_to_json(t)::text';
|
|
$rows=$db->query($sql)->fetchAll(PDO::FETCH_COLUMN);$hash=hash('sha256',implode("\n",$rows));
|
|
if($hash!==$before['sha256'])$diff[]=$name;else $identical++;
|
|
}
|
|
$shape=$db->query("SELECT data_type,is_nullable FROM information_schema.columns WHERE table_schema='public' AND table_name='investment_zones' AND column_name='operational_details'")->fetch(PDO::FETCH_ASSOC);
|
|
$events=$db->query("SELECT action,actor_user_id,correlation_id,after_data->'result' AS result FROM audit_events WHERE source_system='investment-zone-details-installation' ORDER BY occurred_at,id")->fetchAll(PDO::FETCH_ASSOC);
|
|
foreach($events as &$event)$event['result']=json_decode($event['result'],true,512,JSON_THROW_ON_ERROR);unset($event);
|
|
$result=['at'=>gmdate('c'),'target'=>['database'=>'investemoz','port'=>5432],'read_only'=>true,'migration'=>$migration,'ledger'=>(int)$db->query("SELECT count(*) FROM migrations WHERE migration='$migration'")->fetchColumn(),'column'=>$shape?:null,'installation_events'=>$events,'tables_identical_to_fresh_backup'=>$identical,'tables_different_to_fresh_backup'=>$diff,'maintenance'=>is_file($root.'/storage/framework/down')];
|
|
$phase=$result['ledger']===0?'PENDING':'INSTALLED';
|
|
file_put_contents(__DIR__.'/STATUS-'.$phase.'.json',json_encode($result,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_THROW_ON_ERROR).PHP_EOL);
|
|
echo json_encode($result,JSON_UNESCAPED_UNICODE).PHP_EOL;
|
|
}finally{$db->exec('ROLLBACK');}
|