Files
Investidor-V2/outputs/investmoz-parques-mapa-20261016/read-restored.php
T
nala cef6e201c7
tests / ci (push) Canceled after 0s
versao nrgocios 2
2026-10-16 20:11:05 +02:00

21 lines
4.4 KiB
PHP

<?php
ini_set('zend.exception_ignore_args','1');set_exception_handler(function(Throwable $e){fwrite(STDERR,'Park inventory stopped: '.$e::class.' line '.$e->getLine().' '.$e->getMessage().PHP_EOL);exit(1);});
$root=dirname(__DIR__,2);$proof=json_decode(file_get_contents($root.'/outputs/investmoz-referencias-economicas-ativacao-20261016/FRESH-BACKUP.json'),true,512,JSON_THROW_ON_ERROR);$name=$proof['restore_target']['database'];
if(!preg_match('/^economic_refs_before_\d{8}_\d{6}_test$/D',$name)||(int)$proof['restore_target']['port']!==55441||!$proof['restore_verified'])throw new RuntimeException('Restored isolated clone required');
$db=new PDO('pgsql:host=127.0.0.1;port=55441;dbname='.$name,'postgres','',[PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION]);$db->exec('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY');$db->exec("SET LOCAL TIME ZONE 'UTC'");
try{
$actual=$db->query('SELECT current_database() AS database,inet_server_port() AS port')->fetch(PDO::FETCH_ASSOC);if($actual['database']!==$name||(int)$actual['port']!==55441)throw new RuntimeException('Actual clone mismatch');
$tables=['investment_zones','investment_zone_locations','investment_zone_links','investment_zone_relations','investment_zone_geometries','investment_zone_attachments','investment_zone_revisions','investment_legal_sources','investment_submission_zones'];$schemas=[];$counts=[];
foreach($tables as$t){$columns=$db->prepare('SELECT column_name,data_type,is_nullable,column_default FROM information_schema.columns WHERE table_schema=\'public\' AND table_name=? ORDER BY ordinal_position');$columns->execute([$t]);$schemas[$t]=$columns->fetchAll(PDO::FETCH_ASSOC);$counts[$t]=(int)$db->query('SELECT count(*) FROM "'.$t.'"')->fetchColumn();}
$zones=$db->query('SELECT id,code,category,name,description,physical_category,legal_regime,operational_status,development_status,availability_status,area_value,area_unit,area_components,source_evidence,activities,resources,aliases,historical_territory,parent_zone_id,primary_territory_id,operator_id,legal_source_id,data_as_of,sources_consulted_at,publication_status,validation_status,version_no,created_at,updated_at,deleted_at FROM investment_zones ORDER BY code')->fetchAll(PDO::FETCH_ASSOC);
foreach($zones as&$z)foreach(['area_components','source_evidence','activities','resources','aliases','historical_territory']as$f)$z[$f]=$z[$f]===null?null:json_decode($z[$f],true,512,JSON_THROW_ON_ERROR);unset($z);
$locations=$db->query('SELECT zl.id,zl.zone_id,g.id AS territory_id,g.name,g.location_type,g.parent_id,zl.scope_type,zl.source_reference,zl.validation_status,zl.version_no FROM investment_zone_locations zl JOIN geo_locations g ON g.id=zl.geo_location_id ORDER BY zl.zone_id,g.name')->fetchAll(PDO::FETCH_ASSOC);
$related=[];foreach(['investment_zone_links','investment_zone_relations','investment_zone_geometries']as$t)$related[$t]=$db->query('SELECT * FROM "'.$t.'" ORDER BY id')->fetchAll(PDO::FETCH_ASSOC);
$attachments=$db->query('SELECT a.id,a.zone_id,a.kind,a.caption,a.source_reference,a.source_date,a.rights_status,a.personal_data_status,a.review_status,a.publicly_selected,a.version_no,d.id AS document_id,d.file_name,d.sha256,d.status,d.malware_scan_status FROM investment_zone_attachments a JOIN documents d ON d.id=a.document_id ORDER BY a.zone_id,a.id')->fetchAll(PDO::FETCH_ASSOC);
$legal=$db->query('SELECT id,title,url,diploma,article,competent_entity,valid_from,valid_until,validation_status,version_no FROM investment_legal_sources ORDER BY title')->fetchAll(PDO::FETCH_ASSOC);
$ledger=$db->query("SELECT migration FROM migrations WHERE migration LIKE '%investment_zone%' OR migration LIKE '%zone_%' ORDER BY migration")->fetchAll(PDO::FETCH_COLUMN);
$result=['at'=>gmdate('c'),'target'=>$actual,'snapshot_at'=>$proof['at'],'read_only'=>true,'active_database_connections'=>0,'schemas'=>$schemas,'counts'=>$counts,'zones'=>$zones,'locations'=>$locations,'related'=>$related,'attachments'=>$attachments,'legal_sources'=>$legal,'ledger'=>$ledger];
file_put_contents(__DIR__.'/CADASTRO-RESTAURADO.json',json_encode($result,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_THROW_ON_ERROR).PHP_EOL);
echo json_encode(['target'=>$actual,'snapshot_at'=>$proof['at'],'counts'=>$counts,'zones'=>array_map(fn($z)=>array_intersect_key($z,array_flip(['id','code','name','category','version_no','publication_status','development_status','availability_status'])),$zones)],JSON_UNESCAPED_UNICODE).PHP_EOL;
}finally{$db->exec('ROLLBACK');}