gmdate('c'),'database_writes'=>0]; try { $root=dirname(__DIR__,2);$mode=$argv[1]??'qa'; if($mode==='qa') { $plan=json_decode(file_get_contents($root.'/storage/app/private/investmoz-v2/portal-audit/portal-stability-latest.json'),true,512,JSON_THROW_ON_ERROR); putenv('STABILITY_QA_MODE=consumers'); $app=(static fn($p)=>require $p)($plan['source'].'/tools/portal-stability-runtime.php'); $expected=['database'=>'stability_consumers_20261011_084534_test','port'=>55441]; } elseif($mode==='active') { 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); $expected=['database'=>'investemoz','port'=>5432]; } else {throw new RuntimeException('Mode rejected');} $c=config('database.connections.'.config('database.default')); if($c['driver']!=='pgsql'||!empty($c['url'])||$c['database']!==$expected['database']||(int)$c['port']!==$expected['port'])throw new RuntimeException('Configured target rejected'); $app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap(); $db=Illuminate\Support\Facades\DB::connection();$db->beginTransaction();$db->statement('SET TRANSACTION READ ONLY'); $identity=$db->selectOne('SELECT current_database() AS database,inet_server_port() AS port'); if($identity->database!==$expected['database']||(int)$identity->port!==$expected['port'])throw new RuntimeException('Actual target rejected'); $proof['target']=$identity; $proof['geometries']=$db->select("SELECT g.source_system,g.geometry_type,g.data_classification,l.location_type,count(*) AS count,sum(CASE WHEN g.deleted_at IS NULL THEN 1 ELSE 0 END) AS available FROM geo_geometries g JOIN geo_locations l ON l.id=g.geo_location_id GROUP BY 1,2,3,4 ORDER BY 1,2,3,4"); $proof['territories']=$db->select("SELECT source_system,location_type,data_classification,count(*) AS count FROM geo_locations WHERE deleted_at IS NULL GROUP BY 1,2,3 ORDER BY 1,2,3"); $proof['organizations']=$db->select("SELECT id,name,incorporation_country_code,version_no FROM organizations WHERE deleted_at IS NULL ORDER BY id"); $proof['businesses']=$db->select("SELECT id,status,modality,public_slug,published_at IS NOT NULL AS has_publication,deleted_at IS NOT NULL AS deleted FROM existing_businesses ORDER BY id"); $proof['opportunities']=$db->select("SELECT id,status,opportunity_category,owner_user_id,management_version,deleted_at IS NOT NULL AS deleted FROM opportunities ORDER BY id"); $proof['document_groups']=$db->select("SELECT source_system,media_type,data_classification,malware_scan_status,count(*) AS count FROM documents GROUP BY 1,2,3,4 ORDER BY 1,2,3,4"); $proof['origins']=$db->select("SELECT metadata->>'declared_origin_country_code' AS origin,country,count(*) AS count FROM interest_requests GROUP BY 1,2 ORDER BY 1,2"); $proof['result']='read_only';$db->rollBack(); } catch(Throwable $e) { if(isset($db)&&$db->transactionLevel())$db->rollBack(); $proof+=['result'=>'stopped','type'=>$e::class,'file'=>basename($e->getFile()),'line'=>$e->getLine()]; } file_put_contents(__DIR__.'/consumer-context-'.$mode.'.json',json_encode($proof,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_THROW_ON_ERROR)); echo json_encode(['mode'=>$mode,'result'=>$proof['result'],'target'=>$proof['target']??null,'database_writes'=>0,'geometry_groups'=>count($proof['geometries']??[]),'organizations'=>count($proof['organizations']??[])]); exit($proof['result']==='read_only'?0:1);