getLine().PHP_EOL);exit(1);}); $root=dirname(__DIR__,2);$proof=json_decode(file_get_contents($root.'/outputs/investmoz-parques-ativacao-20261016/FRESH-BACKUP.json'),true,512,JSON_THROW_ON_ERROR);$name=$proof['restore_target']['database']; if(!preg_match('/^parks_before_\d{8}_\d{6}_test$/D',$name)||(int)$proof['restore_target']['port']!==55441||!$proof['restore_verified'])throw new RuntimeException('Exact isolated restored 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{ $identity=$db->query('SELECT current_database() AS database,inet_server_port() AS port')->fetch(PDO::FETCH_ASSOC); if($identity['database']!==$name||(int)$identity['port']!==55441)throw new RuntimeException('Actual clone guard'); $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','organizations'];$schemas=[];$counts=[]; foreach($tables as$t){$s=$db->prepare("SELECT column_name,data_type,is_nullable FROM information_schema.columns WHERE table_schema='public' AND table_name=? ORDER BY ordinal_position");$s->execute([$t]);$schemas[$t]=$s->fetchAll(PDO::FETCH_ASSOC);$counts[$t]=(int)$db->query('SELECT count(*) FROM "'.$t.'"')->fetchColumn();} $zones=$db->query("SELECT id,code,name,category,version_no,publication_status,validation_status,physical_category,legal_regime,operational_status,development_status,availability_status,operator_id,legal_source_id,area_value,area_unit,area_components,source_evidence,activities,resources,aliases,historical_territory,parent_zone_id,primary_territory_id,data_as_of,sources_consulted_at,description,description_en,name_en FROM investment_zones WHERE code LIKE 'MOZ-%' 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); $links=$db->query('SELECT zone_id,source_reference,validation_status,version_no,scope_type FROM investment_zone_locations ORDER BY zone_id,id')->fetchAll(PDO::FETCH_ASSOC); $result=['at'=>gmdate('c'),'target'=>$identity,'snapshot_at'=>$proof['at'],'read_only'=>true,'active_database_connections'=>0, 'counts'=>$counts,'schemas'=>$schemas,'zones'=>$zones,'locations'=>$links, 'snapshot_precedes_nominal_130000'=>true,'post_install_business_preservation_receipt'=>'outputs/investmoz-parques-ativacao-20261016/PRESERVACAO-FINAL.json']; file_put_contents(__DIR__.'/CADASTRO-CLONE-PRIVADO.json',json_encode($result,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_THROW_ON_ERROR).PHP_EOL); echo json_encode(['target'=>$identity,'read_only'=>true,'counts'=>$counts,'known_parks'=>array_map(fn($z)=>array_intersect_key($z,array_flip(['code','version_no','publication_status','operator_id','data_as_of'])) ,array_values(array_filter($zones,fn($z)=>$z['category']==='industrial_park')))]).PHP_EOL; }finally{$db->exec('ROLLBACK');}