Confirm active opportunity classifications with read-only audit

This commit is contained in:
2026-10-09 07:38:01 +02:00
parent ae789ce024
commit ebfc188240
3 changed files with 34 additions and 1 deletions
@@ -13,6 +13,23 @@ foreach ($records as &$record) {
}
unset($record);
$checks = [];
$restored = $backup['restored_target'];
if (! str_ends_with($restored['database'], '_test') || (int) $restored['port'] !== 55441) {
throw new RuntimeException('Invalid isolated comparison target');
}
$baseline = new PDO('pgsql:host=127.0.0.1;port=55441;dbname='.$restored['database'], $c['username'], $c['password'], [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$baseline->exec('BEGIN TRANSACTION READ ONLY');
$oldEvents = $baseline->query("SELECT id,row_to_json(t)::text AS row FROM audit_events t WHERE source_system IS DISTINCT FROM 'opportunity-origin-review' ORDER BY id")->fetchAll(PDO::FETCH_KEY_PAIR);
$currentEvents = $pdo->query('SELECT id,row_to_json(t)::text AS row FROM audit_events t ORDER BY id')->fetchAll(PDO::FETCH_KEY_PAIR);
$missing = $changed = 0;
foreach ($oldEvents as $id => $row) {
if (! isset($currentEvents[$id])) {
$missing++;
} elseif ($currentEvents[$id] !== $row) {
$changed++;
}
}
$baseline->exec('ROLLBACK');
foreach (['opportunities','opportunity_projects','opportunity_clusters','opportunity_favourites','interest_requests','interest_submission_receipts','investment_messages','tasks','project_locations','cluster_projects'] as $table) {
$key = '"public"."'.$table.'"';
$rows = $pdo->query('SELECT row_to_json(t)::text AS row FROM '.$key.' t ORDER BY row_to_json(t)::text')->fetchAll(PDO::FETCH_COLUMN);
@@ -20,6 +37,6 @@ foreach (['opportunities','opportunity_projects','opportunity_clusters','opportu
$checks[$table] = ['matches_backup' => $hash === ($backup['table_hashes'][$key] ?? null), 'hash' => $hash, 'rows' => count($rows)];
}
$pdo->exec('ROLLBACK');
$proof = ['captured_at' => gmdate('c'), 'identity' => $identity, 'records' => $records, 'audit' => $events, 'preservation' => $checks, 'active_writes' => 0];
$proof = ['captured_at' => gmdate('c'), 'identity' => $identity, 'records' => $records, 'audit' => $events, 'preservation' => $checks, 'historical_audit' => ['baseline_events' => count($oldEvents), 'missing' => $missing, 'changed' => $changed, 'additional_events' => count($currentEvents) - count($oldEvents)], 'active_writes' => 0];
file_put_contents($directory.'/opportunity-origin-active-confirmation.json', json_encode($proof, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR));
echo json_encode($proof, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR).PHP_EOL;