PDO::ERRMODE_EXCEPTION]); $pdo->exec('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY'); $identity = $pdo->query("SELECT current_database() AS database,inet_server_port() AS port,current_setting('transaction_read_only') AS read_only")->fetch(PDO::FETCH_ASSOC); if ($identity['database'] !== $target['database'] || (int) $identity['port'] !== 55441 || $identity['read_only'] !== 'on') { throw new RuntimeException('Target rejected.'); } $tables = $pdo->query("SELECT schemaname,tablename FROM pg_tables WHERE schemaname NOT IN ('pg_catalog','information_schema') ORDER BY schemaname,tablename")->fetchAll(PDO::FETCH_ASSOC); if (count($tables) !== count($backup['tables'])) { throw new RuntimeException('New table unexpectedly created.'); } $changed = []; foreach ($tables as $t) { $name = '"'.str_replace('"','""',$t['schemaname']).'"."'.str_replace('"','""',$t['tablename']).'"'; $where = match ($name) { '"public"."users"' => " WHERE email <> 'country-production-installer@example.invalid'", '"public"."audit_events"' => " WHERE source_system IS DISTINCT FROM 'organization-country-installation'", '"public"."migrations"' => " WHERE migration <> '2026_10_10_050000_extend_organization_country'", default => '', }; $rows = $pdo->query('SELECT row_to_json(t)::text FROM '.$name.' t'.$where.' ORDER BY row_to_json(t)::text')->fetchAll(PDO::FETCH_COLUMN); if (['count'=>count($rows),'sha256'=>hash('sha256',implode("\n",$rows))] !== $backup['tables'][$name]) { $changed[] = $name; } } if (array_diff($changed,['"public"."sessions"']) !== []) { throw new RuntimeException('Original business rows changed.'); } $columns = $pdo->query("SELECT column_name,is_nullable,data_type FROM information_schema.columns WHERE table_schema='public' AND table_name='organizations' AND column_name IN ('incorporation_country_code','incorporation_country_provenance') ORDER BY column_name")->fetchAll(PDO::FETCH_ASSOC); if (count($columns) !== 2 || array_column($columns,'is_nullable') !== ['YES','YES']) { throw new RuntimeException('Column receipt differs.'); } $stmt = $pdo->prepare("SELECT correlation_id,after_data FROM audit_events WHERE source_system='organization-country-installation'");$stmt->execute();$receipts=$stmt->fetchAll(PDO::FETCH_ASSOC); if(count($receipts)!==1 || $receipts[0]['correlation_id']!==$browser['operation_key']) { throw new RuntimeException('Repeated attempt duplicated audit or changed key.'); } $receipt = json_decode($receipts[0]['after_data'],true,512,JSON_THROW_ON_ERROR); if($receipt['code_hash']!==$backup['code_hash'] || !$receipt['result']['changed'] || $receipt['result']['new_columns']!==2 || $receipt['result']['new_tables']!==0 || $receipt['result']['flags_changed']) { throw new RuntimeException('Stored cold receipt differs.'); } $installed = $pdo->query("SELECT migration FROM migrations WHERE migration IN ('2026_10_09_030000_extend_opportunity_resources','2026_10_10_040000_create_portal_profile_authorization','2026_10_09_190000_prepare_investor_path','2026_10_10_050000_extend_organization_country') ORDER BY migration")->fetchAll(PDO::FETCH_COLUMN); if($installed!==['2026_10_10_050000_extend_organization_country']) { throw new RuntimeException('Another package installed.'); } $orgs=(int)$pdo->query('SELECT count(*) FROM organizations')->fetchColumn();$memberships=(int)$pdo->query('SELECT count(*) FROM memberships')->fetchColumn(); if($orgs!==0 || $memberships!==0) { throw new RuntimeException('Automatic master creation detected.'); } $pdo->exec('ROLLBACK'); $result=['result'=>'passed','at'=>gmdate('c'),'target'=>$identity,'code_hash'=>$backup['code_hash'],'production_manifest_sha256'=>$backup['production_manifest_sha256'],'tables'=>count($tables),'new_tables'=>0,'country_columns'=>$columns,'cold_install_receipt'=>$receipt['result'],'operation_key'=>$receipts[0]['correlation_id'],'audit_events_for_attempt'=>1,'new_organizations'=>0,'new_memberships'=>0,'other_packages_installed'=>false,'original_rows_preserved_except_qa_session'=>true,'active_writes'=>0]; file_put_contents($dir.'/country-c110-persistence-20261011.json',json_encode($result,JSON_PRETTY_PRINT|JSON_THROW_ON_ERROR));echo json_encode($result,JSON_THROW_ON_ERROR).PHP_EOL;