make(Kernel::class)->bootstrap(); use Illuminate\Contracts\Console\Kernel; use Illuminate\Support\Facades\DB; use Illuminate\Support\Str; $identity = DB::selectOne('select current_database() as name, inet_server_port() as port'); throw_unless($identity->name === 'continuity_20261120_test' && (int) $identity->port === 55441, RuntimeException::class, 'Isolated QA only'); $slices = json_decode(file_get_contents(dirname(__DIR__).'/outputs/demography-d01-d06-20261120/source-slices.json'), true, 512, JSON_THROW_ON_ERROR); $result = DB::transaction(function () use ($slices) { $actor = DB::table('users')->first(); throw_unless($actor, RuntimeException::class, 'Existing synthetic investor required'); foreach (['comercio-e-servicos' => 'Comércio e serviços', 'turismo' => 'Turismo'] as $slug => $name) { DB::table('sectors')->insertOrIgnore(['slug' => $slug, 'name' => $name, 'is_active' => true, 'published_at' => now(), 'created_at' => now(), 'updated_at' => now()]); } $country = DB::table('geo_locations')->where('location_type', 'country')->value('id'); $result = []; foreach ($slices as $slice) { $provinceName = $slice['records'][0]['province']; $territoryName = $slice['records'][0]['territory']; foreach ([['province', $provinceName], ['district', $territoryName]] as [$level, $name]) { $id = (string) Str::uuid(); $external = '964a187ad32ea553b9e112cd159caa90b900050c42b7329594a75bcd798a96ff:D01-D06:'.$name; DB::table('geo_locations')->insertOrIgnore(['id' => $id, 'parent_id' => $level === 'province' ? $country : $province, 'name' => $name, 'code' => 'QA-D01-'.$name, 'location_type' => $level, 'source_system' => 'cartografia-workbook', 'external_id' => $external, 'data_classification' => 'public']); $id = DB::table('geo_locations')->where('external_id', $external)->value('id'); if ($level === 'province') { $province = $id; } } $batch = DB::table('demographic_imports')->where('sha256', $slice['sha256'])->value('id'); if (! $batch) { $batch = (string) Str::uuid(); DB::table('demographic_imports')->insert(['id' => $batch, 'sha256' => $slice['sha256'], 'file_name' => $slice['file'], 'source' => 'Instituto Nacional de Estatística (INE)', 'publication_version' => 'Exemplar INE fornecido pela Tempo; edição não indicada; SHA256 identifica a versão', 'status' => 'approved', 'report' => json_encode(['issue_counts' => [], 'qa_slice' => true]), 'preview_sha256' => hash('sha256', json_encode($slice)), 'prepared_by' => $actor->id, 'created_at' => now(), 'updated_at' => now()]); foreach ($slice['records'] as $row) { unset($row['province'], $row['territory'], $row['level']); DB::table('demographic_observations')->insert(['import_id' => $batch, 'geo_location_id' => $id, 'validation_status' => 'valid', ...$row]); } } $result[$territoryName] = ['id' => $id, 'province' => $province, 'batch' => $batch, 'records' => count($slice['records'])]; } return $result; }); echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE).PHP_EOL;