make(Kernel::class)->bootstrap(); $inspect = ($argv[1] ?? '') === '--inspect-client-registry'; $reconcile = ($argv[1] ?? '') === '--record-verified-empty-registry'; $target = $reconcile ? '--apply-authorized-client-registry' : ($inspect ? ($argv[2] ?? '--apply-authorized-client-registry') : ($argv[1] ?? '')); $restore = preg_match('/^compras_client_restore_[a-f0-9]{12}$/D', $target) === 1; if (! $restore && $target !== '--apply-authorized-client-registry') { throw new LogicException('An explicit recovery copy or authorised installation is required.'); } $base = DB::connection()->getConfig(); unset($base['name']); if ($base['driver'] !== 'pgsql' || ! in_array($base['host'], ['127.0.0.1', 'localhost'], true) || ($base['database'] !== 'compras_demo_20261129' && ! preg_match('/^compras_receipts_test_[a-f0-9]{12}$/D', $base['database']))) { throw new LogicException('Refuse any other installation.'); } config(['database.connections.registry_upgrade' => [...$base, 'url' => null, 'database' => $restore ? $target : $base['database']]]); $db = DB::connection('registry_upgrade'); $identity = $db->selectOne('select current_database() as database_name'); if ($db->getName() !== 'registry_upgrade' || $identity->database_name !== ($restore ? $target : $base['database'])) { throw new LogicException('Migration connection identity mismatch; refuse all writes.'); } $exists = $db->getSchemaBuilder()->hasTable('business_clients'); if ($inspect || $reconcile) { $schema = []; if ($exists) { $schema['columns'] = $db->select("select a.attname,format_type(a.atttypid,a.atttypmod) as type,a.attnotnull,pg_get_expr(d.adbin,d.adrelid) as default_value from pg_attribute a left join pg_attrdef d on d.adrelid=a.attrelid and d.adnum=a.attnum where a.attrelid='business_clients'::regclass and a.attnum>0 and not a.attisdropped order by a.attnum"); $schema['constraints'] = $db->select("select conname,pg_get_constraintdef(oid) as definition from pg_constraint where conrelid='business_clients'::regclass order by conname"); $schema['indexes'] = $db->select("select indexname,indexdef from pg_indexes where schemaname='public' and tablename='business_clients' order by indexname"); $schema['triggers'] = $db->select("select tgname,pg_get_triggerdef(oid) as definition from pg_trigger where tgrelid='business_clients'::regclass and not tgisinternal order by tgname"); $schema['function'] = $db->select("select pg_get_functiondef('sgc_preserve_business_client()'::regprocedure) as definition"); } $schemaHash = $exists ? hash('sha256', json_encode($schema, JSON_THROW_ON_ERROR)) : null; if ($reconcile) { if (! $exists || ! hash_equals('4187827c43b41406db7b959f093cc8b2761b04406b268690718e025d3f2e1474', $schemaHash) || $identity->database_name !== 'compras_demo_20261129') { throw new LogicException('Only the exact privately recovered native schema may be reconciled.'); } $db->transaction(function () use ($db): void { $db->statement('LOCK TABLE business_clients,migrations IN SHARE ROW EXCLUSIVE MODE'); if ($db->table('business_clients')->exists() || $db->table('migrations')->where('migration', '2026_11_29_000004_create_business_clients')->exists()) { throw new LogicException('Preserve existing registry data or migration decisions.'); } $repository = new DatabaseMigrationRepository(app('db'), 'migrations'); $repository->setSource('registry_upgrade'); $repository->log('2026_11_29_000004_create_business_clients', $repository->getNextBatchNumber()); }); echo json_encode(['state' => 'passed', 'mode' => 'exact_empty_schema_reconciled', 'schema_sha256' => $schemaHash, 'existing_business_data_changed' => false, 'permissions_changed' => false], JSON_THROW_ON_ERROR).PHP_EOL; exit(0); } echo json_encode(['database' => $identity->database_name, 'connection_name' => $db->getName(), 'exists' => $exists, 'schema_sha256' => $schemaHash, 'migration_recorded' => $db->table('migrations')->where('migration', '2026_11_29_000004_create_business_clients')->exists(), 'client_count' => $exists ? $db->table('business_clients')->count() : null], JSON_THROW_ON_ERROR).PHP_EOL; exit(0); } if ($exists) { throw new LogicException('Registry already installed; inspect instead of repeating this upgrade.'); } $migration = 'database/migrations/2026_11_29_000004_create_business_clients.php'; $tables = array_column($db->select("select tablename from pg_tables where schemaname='public' order by tablename"), 'tablename'); $protected = array_values(array_diff($tables, ['migrations', 'sessions', 'cache', 'cache_locks', 'jobs', 'failed_jobs', 'job_batches'])); $fingerprint = static function () use ($db, $protected): array { $hashes = []; foreach ($protected as $table) { $quoted = '"'.str_replace('"', '""', $table).'"'; $rows = $db->select('select row_to_json(t)::text as row from '.$quoted.' t order by row_to_json(t)::text'); $hashes[$table] = hash('sha256', json_encode(array_column($rows, 'row'), JSON_THROW_ON_ERROR)); } return $hashes; }; $db->transaction(function () use ($db, $protected, $fingerprint, $migration): void { foreach ($protected as $table) { $db->statement('LOCK TABLE "'.str_replace('"', '""', $table).'" IN SHARE ROW EXCLUSIVE MODE'); } $before = $fingerprint(); if (Artisan::call('migrate', ['--database' => 'registry_upgrade', '--path' => [$migration], '--force' => true]) !== 0 || $fingerprint() !== $before || $db->table('business_clients')->count() !== 0) { throw new LogicException('Registry upgrade failed preservation checks.'); } }); echo json_encode(['state' => 'passed', 'mode' => $restore ? 'recovery_copy' : 'selected_installation', 'existing_business_tables_preserved' => count($protected), 'client_records_created' => 0, 'permissions_changed' => false, 'migration' => $migration], JSON_THROW_ON_ERROR).PHP_EOL;