Files
Investidor-V2/tools/validate-cartography-sql.php
T
nala 67dad461d9
tests / ci (push) Canceled after 0s
provincias
2026-09-29 14:14:06 +02:00

48 lines
2.3 KiB
PHP

<?php
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Support\Facades\DB;
// Read a pinned, inspected package. Execute only in the isolated QA database.
require __DIR__.'/../vendor/autoload.php';
$app = require __DIR__.'/../bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();
$db = DB::connection();
if (! $app->environment('testing') || $db->getDatabaseName() !== 'portal_services_test' || (string) $db->getConfig('port') !== '55441') {
throw new RuntimeException('Isolated QA database required.');
}
$package = json_decode(file_get_contents(storage_path('app/private/cartography-sql-package.json')), true, 512, JSON_THROW_ON_ERROR);
$source = 'C:/Mac/Home/Documents/TEMPO/Portal Tempo/Servicos Tempo/prompt/cartografia_moz.xlsx';
if (hash_file('sha256', $source) !== $package['report']['source_sha256'] || $package['report']['source_sha256'] !== '1e36c769c4767420b10f1ff5ee0c2955cdb1b03c98179c5cad549e832ffb1f15') {
throw new RuntimeException('Source checksum mismatch.');
}
$results = [];
for ($run = 1; $run <= 2; $run++) {
$results[] = $db->transaction(function () use ($db, $package, $run) {
foreach ($package['ddl'] as $sql) {
if (! str_starts_with($sql, '--')) {
$db->unprepared($sql);
}
}
foreach ($package['statements'] as $sql) {
// Laravel owns the single transaction covering DDL and all INSERTs.
if (! in_array($sql, ['BEGIN;', 'COMMIT;'], true)) {
$db->unprepared($sql);
}
}
$counts = [];
foreach ($package['report']['counts'] as $table => $expected) {
$counts[$table] = $db->table('investmoz.'.$table)->count();
if ($counts[$table] !== $expected) {
throw new RuntimeException('Reconciliation failed: '.$table);
}
}
$db->statement('SET search_path TO public');
return ['run' => $run, 'counts' => $counts, 'total' => array_sum($counts)];
});
}
$report = ['scope' => 'isolated QA only; source staging tables, not application master data', 'results' => $results];
file_put_contents(base_path('docs/investmoz/gis/sql-workbook-validation.json'), json_encode($report, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR).PHP_EOL);
echo json_encode($report, JSON_THROW_ON_ERROR).PHP_EOL;