oportunidades 7
tests / ci (push) Waiting to run

This commit is contained in:
2026-10-13 11:40:29 +02:00
parent 20971d523f
commit 287e991486
2902 changed files with 1085456 additions and 25957 deletions
+36 -11
View File
@@ -1,11 +1,14 @@
<?php
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Support\Facades\DB;
// Logical recovery into a newly initialized, private QA cluster. Never repair shared catalogs in place.
require dirname(__DIR__).'/vendor/autoload.php';
require __DIR__.'/portal-qa-environment.php';
$target = portalQaEnvironment();
$app = require dirname(__DIR__).'/bootstrap/app.php';
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
$app->make(Kernel::class)->bootstrap();
set_exception_handler(static function (Throwable $error): void {
$diagnostic = json_encode(['at' => gmdate('c'), 'mode' => $GLOBALS['argv'][1] ?? 'capture', 'class' => get_class($error), 'message' => $error->getMessage()], JSON_PRETTY_PRINT);
$private = dirname(__DIR__).'/storage/app/private/investmoz-v2/portal-audit/';
@@ -14,7 +17,10 @@ set_exception_handler(static function (Throwable $error): void {
fwrite(STDERR, 'QA recovery stopped; details private, active environment untouched.'.PHP_EOL);
exit(1);
});
function recoveryQuote(string $name): string { return '"'.str_replace('"', '""', $name).'"'; }
function recoveryQuote(string $name): string
{
return '"'.str_replace('"', '""', $name).'"';
}
function recoverySchema(PDO $pdo): array
{
$filter = "n.nspname NOT LIKE 'pg_%' AND n.nspname <> 'information_schema'";
@@ -31,6 +37,7 @@ function recoverySchema(PDO $pdo): array
'sequences' => "SELECT n.nspname,c.relname,format_type(s.seqtypid,NULL) AS type,s.seqstart,s.seqincrement,s.seqmax,s.seqmin,s.seqcache,s.seqcycle FROM pg_sequence s JOIN pg_class c ON c.oid=s.seqrelid JOIN pg_namespace n ON n.oid=c.relnamespace WHERE $filter ORDER BY 1,2",
'extensions' => 'SELECT extname,extversion FROM pg_extension ORDER BY 1',
];
return array_map(fn ($sql) => $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC), $queries);
}
function recoveryCompare(PDO $pdo, string $directory, array $manifest): array
@@ -40,7 +47,9 @@ function recoveryCompare(PDO $pdo, string $directory, array $manifest): array
$schema = recoverySchema($pdo);
$canonical = static function (array $value): array {
// Dropped column slots are physical storage history, not column ordering/contracts.
foreach ($value['columns'] as &$column) { unset($column['attnum']); }
foreach ($value['columns'] as &$column) {
unset($column['attnum']);
}
unset($column);
foreach ($value['constraints'] as &$constraint) {
if (in_array($constraint['conname'], ['portal_request_status', 'portal_role_name'], true)
@@ -50,6 +59,7 @@ function recoveryCompare(PDO $pdo, string $directory, array $manifest): array
}
}
unset($constraint);
return $value;
};
if ($canonical($schema) !== $canonical($manifest['schema'])) {
@@ -61,7 +71,9 @@ function recoveryCompare(PDO $pdo, string $directory, array $manifest): array
$expected = file_get_contents($directory.'/'.$table['file']);
throw_unless(hash('sha256', $expected) === $table['sha256'], RuntimeException::class, 'Preservation bytes changed.');
$actual = implode("\n", $pdo->query('SELECT row_to_json(t)::text FROM '.$name.' t ORDER BY row_to_json(t)::text')->fetchAll(PDO::FETCH_COLUMN));
if ($table['rows'] > 0) { $actual .= "\n"; }
if ($table['rows'] > 0) {
$actual .= "\n";
}
throw_unless($actual === $expected, RuntimeException::class, 'Recovered table bytes differ: '.$name);
$total += $table['rows'];
}
@@ -69,14 +81,17 @@ function recoveryCompare(PDO $pdo, string $directory, array $manifest): array
throw_unless($pdo->query('SELECT last_value,is_called FROM '.$name)->fetch(PDO::FETCH_ASSOC) === $expected,
RuntimeException::class, 'Sequence differs.');
}
return ['tables' => count($manifest['tables']), 'rows' => $total, 'sequences' => count($manifest['sequences']), 'schema_equal' => true, 'all_rows_equal' => true];
} finally { $pdo->exec('ROLLBACK'); }
} finally {
$pdo->exec('ROLLBACK');
}
}
$mode = $argv[1] ?? 'capture';
$base = storage_path('app/private/investmoz-v2/portal-qa-recovery');
if ($mode === 'capture') {
portalQaAssertTarget($target);
$source = Illuminate\Support\Facades\DB::connection()->getPdo();
$source = DB::connection()->getPdo();
$directory = $base.'/'.gmdate('Ymd_His');
mkdir($directory, 0700, true);
$source->exec('BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY');
@@ -88,7 +103,9 @@ if ($mode === 'capture') {
$file = sprintf('%04d.ndjson', count($manifest['tables']) + 1);
$bytes = implode("\n", $source->query('SELECT row_to_json(t)::text FROM '.$name.' t ORDER BY row_to_json(t)::text')->fetchAll(PDO::FETCH_COLUMN));
$count = $bytes === '' ? 0 : substr_count($bytes, "\n") + 1;
if ($count > 0) { $bytes .= "\n"; }
if ($count > 0) {
$bytes .= "\n";
}
file_put_contents($directory.'/'.$file, $bytes);
$manifest['tables'][$name] = ['schema' => $table['schemaname'], 'name' => $table['tablename'], 'file' => $file, 'rows' => $count, 'sha256' => hash('sha256', $bytes)];
}
@@ -116,9 +133,13 @@ if ($mode === 'roles') {
$saved = json_decode(file_get_contents($directory.'/roles-private.json'), true, 512, JSON_THROW_ON_ERROR);
$known = $maintenance->query('SELECT rolname FROM pg_authid')->fetchAll(PDO::FETCH_COLUMN);
foreach ($saved['roles'] as $role) {
if (str_starts_with($role['rolname'], 'pg_')) { continue; }
if (str_starts_with($role['rolname'], 'pg_')) {
continue;
}
$name = recoveryQuote($role['rolname']);
if (! in_array($role['rolname'], $known, true)) { $maintenance->exec('CREATE ROLE '.$name); }
if (! in_array($role['rolname'], $known, true)) {
$maintenance->exec('CREATE ROLE '.$name);
}
$options = [];
foreach (['rolsuper' => 'SUPERUSER', 'rolinherit' => 'INHERIT', 'rolcreaterole' => 'CREATEROLE', 'rolcreatedb' => 'CREATEDB', 'rolcanlogin' => 'LOGIN', 'rolreplication' => 'REPLICATION', 'rolbypassrls' => 'BYPASSRLS'] as $key => $option) {
$options[] = ($role[$key] ? '' : 'NO').$option;
@@ -159,7 +180,9 @@ if ($mode === 'load') {
$bytes = file_get_contents($directory.'/'.$table['file']);
throw_unless(hash('sha256', $bytes) === $table['sha256'], RuntimeException::class, 'Recovery bytes changed.');
foreach (explode("\n", rtrim($bytes, "\n")) as $line) {
if ($line !== '') { $query->execute([$line]); }
if ($line !== '') {
$query->execute([$line]);
}
}
}
foreach ($manifest['sequences'] as $name => $sequence) {
@@ -176,4 +199,6 @@ if ($mode === 'load') {
$proof = recoveryCompare($pdo, $directory, $manifest) + ['verified_at' => gmdate('c'), 'target' => $identity, 'active_writes' => 0, 'key_rotated' => false];
file_put_contents($directory.'/'.$mode.'-proof.json', json_encode($proof, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR));
echo json_encode($proof).PHP_EOL;
} else { throw new RuntimeException('Unknown recovery operation.'); }
} else {
throw new RuntimeException('Unknown recovery operation.');
}