Files
Investidor-V2/outputs/investmoz-referencias-economicas-mapa-20261016/inspect-QA.php
T
nala 2d23157e40
tests / ci (push) Canceled after 0s
versao nrgocios
2026-10-16 13:06:58 +02:00

29 lines
3.1 KiB
PHP

<?php
ini_set('zend.exception_ignore_args','1');
set_exception_handler(function(Throwable $e){fwrite(STDERR,'QA inventory stopped: '.$e::class.' line '.$e->getLine().PHP_EOL);exit(1);});
$targets=['restored_destination'=>'negocios_nome_before_20261016_071036_test','accepted_QA'=>'stability_consumers_20261011_084534_test'];
$result=['date'=>'2026-10-16','read_only'=>true,'active_connections'=>0,'targets'=>[]];
foreach($targets as$label=>$name){
if(!str_ends_with($name,'_test'))throw new RuntimeException('QA suffix 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');
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 QA identity required');
$tables=['sectors','reference_catalog_versions','economic_activities','economic_clusters','value_chains','value_chain_links','portfolio_entries','investment_portfolio_entries','investment_zone_relations','investment_zone_links'];
$state=['identity'=>$identity,'tables'=>[]];
foreach($tables as$t){
$q=$db->prepare('SELECT to_regclass(:n)');$q->execute(['n'=>'public.'.$t]);if(!$q->fetchColumn())continue;
$rows=$db->query('SELECT row_to_json(t)::text FROM public."'.$t.'" t ORDER BY row_to_json(t)::text')->fetchAll(PDO::FETCH_COLUMN);
$state['tables'][$t]=['count'=>count($rows),'sha256'=>hash('sha256',implode("\n",$rows)),'rows'=>array_map(fn($s)=>json_decode($s,true,512,JSON_THROW_ON_ERROR),$rows)];
}
$state['economic_schema']=$db->query("SELECT column_name,data_type,is_nullable FROM information_schema.columns WHERE table_schema='public' AND table_name='economic_activities' ORDER BY ordinal_position")->fetchAll(PDO::FETCH_ASSOC);
$state['economic_constraints']=$db->query("SELECT conname,pg_get_constraintdef(oid) AS definition FROM pg_constraint WHERE conrelid='public.economic_activities'::regclass ORDER BY conname")->fetchAll(PDO::FETCH_ASSOC);
$state['economic_indexes']=$db->query("SELECT indexname,indexdef FROM pg_indexes WHERE schemaname='public' AND tablename='economic_activities' ORDER BY indexname")->fetchAll(PDO::FETCH_ASSOC);
$state['reference_usage']=$db->query("SELECT 'opportunities' AS consumer,count(*) AS records,count(sector_id) AS sector_refs FROM opportunities UNION ALL SELECT 'existing_businesses',count(*),count(sector_id) FROM existing_businesses")->fetchAll(PDO::FETCH_ASSOC);
$result['targets'][$label]=$state;
}finally{$db->exec('ROLLBACK');}
}
$file=__DIR__.'/ESTADO-QA-READONLY.json';if(is_file($file))throw new RuntimeException('Preserve inventory');file_put_contents($file,json_encode($result,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_THROW_ON_ERROR).PHP_EOL);
echo json_encode(['read_only'=>true,'targets'=>array_map(fn($s)=>['identity'=>$s['identity'],'counts'=>array_map(fn($t)=>$t['count'],$s['tables']),'constraints'=>$s['economic_constraints']],$result['targets'])],JSON_UNESCAPED_UNICODE).PHP_EOL;