Files
Investidor-V2/tools/demography-registry-audit.php
T
2026-11-21 20:58:19 +02:00

23 lines
1.3 KiB
PHP

<?php
// Read only: take public territorial references from the current application, not private data.
use App\Services\InvestmentGeography;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Support\Facades\DB;
require dirname(__DIR__).'/vendor/autoload.php';
$app = require dirname(__DIR__).'/bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();
DB::beginTransaction();
try {
DB::statement('SET TRANSACTION READ ONLY');
$records = app(InvestmentGeography::class)->publicReferences()->whereIn('location_type', ['country', 'province', 'district'])
->orderByRaw("CASE location_type WHEN 'country' THEN 0 WHEN 'province' THEN 1 ELSE 2 END")
->get(['id', 'parent_id', 'name', 'location_type', 'code', 'source_code', 'source_system', 'external_id', 'data_classification', 'version_no']);
$path = storage_path('app/private/demography-registry-snapshot.json');
file_put_contents($path, json_encode(['read_only' => true, 'captured_at' => gmdate(DATE_ATOM), 'records' => $records], JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
echo json_encode(['read_only' => true, 'counts' => $records->countBy('location_type')->all(), 'snapshot_sha256' => hash_file('sha256', $path)], JSON_THROW_ON_ERROR).PHP_EOL;
} finally {
DB::rollBack();
}