make(Kernel::class)->bootstrap(); $root = dirname(__DIR__); $host = realpath($root.'/../sgf-tempo'); $config = DB::connection('pgsql')->getConfig(); if ($config['host'] !== '127.0.0.1') { throw new RuntimeException('Prova apenas local.'); } config(['database.connections.access_admin' => [...$config, 'url' => null, 'database' => 'postgres']]); $db = DB::connection('access_admin'); $suffix = bin2hex(random_bytes(6)); $role = 'ir_operational_'.$suffix; $database = 'budget_core_sgf_'.$suffix; $password = bin2hex(random_bytes(32)); $roleCreated = false; $databaseCreated = false; $restoredCreated = false; $restoredDatabase = 'sgc_operational_'.$suffix; $recoveryDump = sys_get_temp_dir().'/sgc-operational-recovery-'.$suffix.'.dump'; $code = 1; $before = file_get_contents($host.'/packages/purchasing/current.json'); $calibrate = ($argv[1] ?? '') === 'calibrate'; try { $prepare = new Process([PHP_BINARY, $root.'/scripts/prepare-sgf-operational.php', $host], $root); $prepare->mustRun(); $overlay = json_decode($prepare->getOutput(), true, 16, JSON_THROW_ON_ERROR)['directory']; $db->statement('CREATE ROLE "'.$role.'" LOGIN NOSUPERUSER NOCREATEDB NOCREATEROLE PASSWORD '.$db->getPdo()->quote($password)); $roleCreated = true; $db->statement('CREATE DATABASE "'.$database.'" OWNER "'.$role.'"'); $databaseCreated = true; $env = ['APP_ENV' => 'budget-testing', 'APP_DEBUG' => 'false', 'APP_KEY' => 'base64:'.base64_encode(random_bytes(32)), 'DB_CONNECTION' => 'pgsql', 'DB_URL' => '', 'DB_HOST' => '127.0.0.1', 'DB_PORT' => (string) $config['port'], 'DB_DATABASE' => $database, 'DB_USERNAME' => $role, 'DB_PASSWORD' => $password, 'CACHE_STORE' => 'array', 'SESSION_DRIVER' => 'array', 'IR_SGF_HOST' => $host, 'IR_CANDIDATE_ROOT' => $root, 'IR_ACCESS_OVERLAY' => $overlay]; $env['IR_CALIBRATE'] = $calibrate ? '1' : '0'; $env['IR_OPERATIONAL_BACKUP'] = $recoveryDump; $process = new Process(['powershell.exe', '-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', $host.'/scripts/sgf.ps1', 'php', $root.'/tests/Support/sgf-operational-proof.php'], $host, $env, null, 60); $process->mustRun(); $proof = json_decode($process->getOutput(), true, 32, JSON_THROW_ON_ERROR); if (! $calibrate) { $db->statement('CREATE DATABASE "'.$restoredDatabase.'" OWNER "'.$role.'"'); $restoredCreated = true; $restore = new Process(['C:/Program Files/PostgreSQL/17/bin/pg_restore.exe', '--no-owner', '--no-privileges', '--exit-on-error', '--dbname='.$restoredDatabase, $recoveryDump], null, ['PGHOST' => '127.0.0.1', 'PGPORT' => (string) $config['port'], 'PGUSER' => $role, 'PGPASSWORD' => $password], null, 60); $restore->mustRun(); $read = new Process(['powershell.exe', '-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', $host.'/scripts/sgf.ps1', 'php', $root.'/tests/Support/sgf-operational-proof.php'], $host, [...$env, 'APP_ENV' => 'production', 'DB_DATABASE' => $restoredDatabase, 'IR_RESTORE_VERIFY' => '1'], null, 60); $read->mustRun(); $proof['restored_operational_composition'] = json_decode($read->getOutput(), true, 32, JSON_THROW_ON_ERROR); $proof['recovery_dump_sha256'] = hash_file('sha256', $recoveryDump); } if (! $proof['passed'] || file_get_contents($host.'/packages/purchasing/current.json') !== $before || json_decode($before, true)['version'] !== '0.1.1') { throw new RuntimeException('Prova ou preservação do destino falhou.'); } $proof += ['overlay' => $overlay, 'checked_at' => gmdate('c'), 'destination_version' => '0.1.1', 'prior_tests_repeated' => false]; $code = 0; } catch (Throwable $error) { fwrite(STDERR, $error->getMessage()."\n"); } finally { if ($restoredCreated) { $db->select('select pg_terminate_backend(pid) from pg_stat_activity where datname=? and pid<>pg_backend_pid()', [$restoredDatabase]); $db->statement('DROP DATABASE "'.$restoredDatabase.'"'); } if ($databaseCreated) { $db->select('select pg_terminate_backend(pid) from pg_stat_activity where datname=? and pid<>pg_backend_pid()', [$database]); $db->statement('DROP DATABASE "'.$database.'"'); } if ($roleCreated) { $db->statement('DROP ROLE "'.$role.'"'); } } if ($code === 0) { if ($calibrate) { file_put_contents($root.'/integrations/sgf/operational/budget-contract.json', json_encode(['schema_signature' => $proof['schema_signature'], 'origin' => 'Existing accepted SGF common-budget host schema; no data or credentials'], JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR)); echo "Budget schema contract recorded from isolated fixture, not operational validation.\n"; exit(0); } $proof['isolated_database_removed'] = true; if (is_file($recoveryDump)) { unlink($recoveryDump); } file_put_contents($root.'/docs/proofs/sgf-operational-delivery.json', json_encode($proof, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR)); echo json_encode($proof, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR)."\n"; } exit($code);