Files
compras/scripts/sgf-access-proof.php
T
2026-11-02 08:08:05 +02:00

60 lines
3.1 KiB
PHP

<?php
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Support\Facades\DB;
use Symfony\Component\Process\Process;
require dirname(__DIR__).'/vendor/autoload.php';
$app = require dirname(__DIR__).'/bootstrap/app.php';
$app->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_access_'.$suffix;
$database = 'budget_core_sgf_'.$suffix;
$password = bin2hex(random_bytes(32));
$roleCreated = false;
$databaseCreated = false;
$code = 1;
$before = file_get_contents($host.'/packages/purchasing/current.json');
try {
$prepare = new Process([PHP_BINARY, $root.'/scripts/prepare-sgf-access.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];
$process = new Process(['powershell.exe', '-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', $host.'/scripts/sgf.ps1', 'php', $root.'/tests/Support/sgf-access-proof.php'], $host, $env, null, 60);
$process->mustRun();
$proof = json_decode($process->getOutput(), true, 32, JSON_THROW_ON_ERROR);
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 ($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) {
$proof['isolated_database_removed'] = true;
file_put_contents($root.'/docs/proofs/sgf-access-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);