*/ public function prepare(string $database, string $role, string $operator, string $reason, bool $apply = false, bool $rebind = false): array { Validator::make(compact('database', 'role', 'operator', 'reason'), [ 'database' => 'required|string|max:200', 'role' => 'required|string|max:200', 'operator' => 'required|string|min:3|max:200', 'reason' => 'required|string|min:10|max:4000', ])->validate(); if (config('standalone-runtime.enabled') === true) { throw new LogicException('Disable runtime activation before explicit installation or recovery.'); } $settings = config('standalone-runtime'); $settings['enabled'] = true; $settings['features'] = array_fill_keys(StandaloneRuntime::FEATURES, true); $runtime = new StandaloneRuntime($settings, new StandaloneArtifact); $artifact = $runtime->artifact(); $runtime->assertLoadedPackages(allowMissing: true); foreach (StandaloneArtifact::PACKAGES as $package) { require_once $artifact['paths'][$package].'/bootstrap.php'; } $runtime->assertLoadedPackages(); $db = DB::connection(); $binding = StandaloneRuntime::databaseBinding($db); $identity = $db->selectOne('SELECT current_database() AS database_name,current_user AS role_name'); if ($identity === null || $identity->database_name !== $database || $identity->role_name !== $role) { throw new LogicException('COMPRAS_RUNTIME_EXPECTED_DESTINATION'); } $plan = ['contract' => StandaloneArtifact::CONTRACT, 'installation_id' => $settings['installation_id'], 'manifest_sha256' => $settings['manifest_sha256'], 'database' => $database, 'role' => $role, 'feature_activation' => false, 'actor_grants' => false, 'rebind' => $rebind]; if (! $apply) { return $plan + ['state' => 'preview', 'written' => false]; } return $db->transaction(function () use ($db, $runtime, $settings, $binding, $operator, $reason, $rebind, $plan) { $db->selectOne('SELECT pg_advisory_xact_lock(hashtextextended(?, 0))', ['COMPRAS-STANDALONE-INSTALLATION']); $schema = $db->getSchemaBuilder(); if (! $schema->hasTable('compras_runtime_installations')) { if ($rebind) { throw new LogicException('COMPRAS_RUNTIME_RECOVERY_HISTORY_MISSING'); } (require database_path('standalone-migrations/2026_11_29_000001_create_runtime_installations.php'))->up(); } if ($db->table('compras_runtime_installations')->where('id', $settings['installation_id'])->exists()) { $runtime->assertInstallation($db); StandaloneSchema::verify($schema, $settings['features']); return $plan + ['state' => 'already_prepared', 'written' => false]; } if ($rebind) { StandaloneSchema::verify($schema, $settings['features']); } $db->table('compras_runtime_installations')->insert([ 'id' => $settings['installation_id'], 'product' => 'compras', 'contract' => StandaloneArtifact::CONTRACT, 'manifest_sha256' => $settings['manifest_sha256'], 'database_binding_sha256' => $binding, 'recorded_by' => $operator, 'reason' => $reason, 'installed_at' => now(), ]); if (! $rebind) { StandaloneExtensionSchema::install($db); } $runtime->assertInstallation($db); StandaloneSchema::verify($schema, $settings['features']); return $plan + ['state' => $rebind ? 'rebound' : 'prepared', 'written' => true]; }); } }