14 lines
2.4 KiB
PHP
14 lines
2.4 KiB
PHP
<?php
|
|
ini_set('memory_limit','512M');
|
|
require __DIR__.'/boot-QA.php';
|
|
$db=Illuminate\Support\Facades\DB::connection();$db->beginTransaction();$db->statement('SET TRANSACTION ISOLATION LEVEL REPEATABLE READ, READ ONLY');$db->statement("SET LOCAL TIME ZONE 'UTC'");
|
|
try{
|
|
$identity=(array)$db->selectOne('SELECT current_database() AS database,inet_server_port() AS port');if($identity['database']!=='parks_integral_20261018_test'||(int)$identity['port']!==55441)throw new RuntimeException('QA only');
|
|
$snapshot=$db->selectOne('SELECT pg_export_snapshot() AS id')->id;$restore='parks_integral_restore_20261018_v2_test';$dir='C:/Users/macbook/.config/investmoz/parques-integral-QA-20261018';$dump=$dir.'/final-QA-v2.dump';$bin='C:/Program Files/PostgreSQL/17/bin/';$args=['-h','127.0.0.1','-p','55441','-U','postgres'];
|
|
$run=function($args){$p=new Symfony\Component\Process\Process($args);$p->setTimeout(180);$p->mustRun();};
|
|
$run([$bin.'pg_dump.exe',...$args,'-d',$identity['database'],'-Fc','--snapshot='.$snapshot,'-f',$dump]);$run([$bin.'createdb.exe',...$args,$restore]);$run([$bin.'pg_restore.exe',...$args,'-d',$restore,'--exit-on-error','--no-owner','--no-privileges',$dump]);
|
|
$clone=new PDO('pgsql:host=127.0.0.1;port=55441;dbname='.$restore,'postgres','',[PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION]);$clone->exec('BEGIN READ ONLY');$clone->exec("SET LOCAL TIME ZONE 'UTC'");$tables=[];
|
|
foreach($db->select("SELECT schemaname,tablename FROM pg_tables WHERE schemaname NOT LIKE 'pg_%' AND schemaname<>'information_schema' ORDER BY schemaname,tablename")as$t){$name='"'.$t->schemaname.'"."'.$t->tablename.'"';$sql='SELECT row_to_json(t)::text AS row FROM '.$name.' t ORDER BY row_to_json(t)::text';$a=array_map(fn($r)=>$r->row,$db->select($sql));$b=$clone->query($sql)->fetchAll(PDO::FETCH_COLUMN);if($a!==$b)throw new RuntimeException('Restored rows differ');$tables[$name]=['rows'=>count($a),'sha256'=>hash('sha256',implode("\n",$a))];}
|
|
$clone->exec('ROLLBACK');$proof=['source'=>$identity,'restored_database'=>$restore,'port'=>55441,'dump_sha256'=>hash_file('sha256',$dump),'all_tables_equal'=>true,'table_count'=>count($tables),'tables'=>$tables,'active_changes'=>0];file_put_contents(__DIR__.'/RESTORE-QA.json',json_encode($proof,JSON_PRETTY_PRINT|JSON_THROW_ON_ERROR));echo json_encode(['QA_restore_verified'=>true,'tables'=>count($tables),'active_changes'=>0]).PHP_EOL;
|
|
}finally{$db->rollBack();}
|