make(Kernel::class)->bootstrap(); use App\Services\FundingInstitutionDetails; use Illuminate\Contracts\Console\Kernel; use Illuminate\Support\Facades\DB; use Illuminate\Support\Str; $i = DB::selectOne('select current_database() as name,inet_server_port() as port'); if ($i->name !== 'continuity_20261120_test' || $i->port != 55441) { throw new Exception('QA only'); } $outputDirectory = base_path('outputs/funding-b03-b06-20261120'); if (! is_dir($outputDirectory)) { mkdir($outputDirectory, 0700, true); } $package = json_decode(file_get_contents('database/data/funding-institutions-nov2026.json'), true); $snapshotPath = $argv[1] ?? throw new RuntimeException('Supply the existing read-only public geography snapshot'); $snapshot = json_decode(file_get_contents($snapshotPath), true); if (! $snapshot['read_only']) { throw new Exception('Original snapshot required'); } $result = DB::transaction(function () use ($snapshot, $package) { foreach ($snapshot['records'] as $g) { DB::table('geo_locations')->insertOrIgnore($g); } $provinceIds = collect($snapshot['records'])->where('location_type', 'province')->keyBy('name'); $actor = DB::table('users')->first()->id; $out = []; foreach (['moza', 'bci', 'standard-bank', 'ac-microbanco'] as $key) { $ref = $package['references'][$key]; $p = $ref['draft']; foreach ($ref['location_territory_names'] ?? [] as $index => $name) { $matches = $provinceIds->filter(fn ($g) => Str::lower(Str::ascii($g['name'])) === Str::lower(Str::ascii($name))); if ($matches->count() === 1) { $p['locations'][$index]['territory_id'] = $matches->first()['id']; } } $p = [...$p, ...app(FundingInstitutionDetails::class)->normalize($p, true)]; $code = 'qa-b03-b06-'.$key; $id = DB::table('funding_programmes')->where('code', $code)->value('id'); if (! $id) { $id = (string) Str::uuid(); $v = (string) Str::uuid(); DB::table('funding_programmes')->insert(['id' => $id, 'code' => $code, 'version' => 1, 'draft' => json_encode($ref['draft']), 'created_by' => $actor, 'created_at' => now(), 'updated_at' => now()]); DB::table('funding_programme_versions')->insert(['id' => $v, 'programme_id' => $id, 'version' => 1, 'payload' => json_encode($p), 'reason' => 'QA-only fixture from unchanged supplied source, no editorial or operational publication', 'actor_id' => $actor, 'published_at' => now()]); DB::table('funding_programmes')->where('id', $id)->update(['published_version_id' => $v]); } $out[$key] = ['id' => $id, 'title' => $p['title'], 'products' => array_column($p['products'], 'availability')]; } return ['source_sha256' => hash_file('sha256', 'database/data/funding-institutions-nov2026.json'), 'records' => $out, 'provinces' => $provinceIds->map(fn ($g) => $g['id'])->all()]; }); $bytes = json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR); file_put_contents(base_path('outputs/funding-b03-b06-20261120/fixture.json'), $bytes); echo $bytes;