@@ -10,15 +10,21 @@ use LogicException;
|
||||
final class StandaloneInstallation
|
||||
{
|
||||
/** @return array<string,mixed> */
|
||||
public function prepare(string $database, string $role, string $operator, string $reason, bool $apply = false, bool $rebind = false): array
|
||||
public function prepare(string $database, string $role, string $operator, string $reason, bool $apply = false, bool $rebind = false, bool $reuseComponents = 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 ($rebind && $reuseComponents) {
|
||||
throw new LogicException('Recovery binding and initial component reuse are separate operations.');
|
||||
}
|
||||
if (config('standalone-runtime.enabled') === true) {
|
||||
throw new LogicException('Disable runtime activation before explicit installation or recovery.');
|
||||
}
|
||||
if (config('standalone-runtime.prepare_only') !== true) {
|
||||
throw new LogicException('Use explicit console preparation mode to verify the pinned packages before loading executable files.');
|
||||
}
|
||||
$settings = config('standalone-runtime');
|
||||
$settings['enabled'] = true;
|
||||
$settings['features'] = array_fill_keys(StandaloneRuntime::FEATURES, true);
|
||||
@@ -35,14 +41,21 @@ final class StandaloneInstallation
|
||||
if ($identity === null || $identity->database_name !== $database || $identity->role_name !== $role) {
|
||||
throw new LogicException('COMPRAS_RUNTIME_EXPECTED_DESTINATION');
|
||||
}
|
||||
$already = $db->getSchemaBuilder()->hasTable('compras_runtime_installations')
|
||||
&& $db->table('compras_runtime_installations')->where('id', $settings['installation_id'])->exists();
|
||||
if ($already) {
|
||||
$runtime->assertInstallation($db);
|
||||
}
|
||||
$reused = $reuseComponents && ! $already ? StandaloneComponentSchema::reusable($db) : [];
|
||||
$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];
|
||||
'feature_activation' => false, 'actor_grants' => false, 'rebind' => $rebind,
|
||||
'reuse_components' => $reuseComponents, 'existing_components' => $reused];
|
||||
if (! $apply) {
|
||||
return $plan + ['state' => 'preview', 'written' => false];
|
||||
}
|
||||
|
||||
return $db->transaction(function () use ($db, $runtime, $settings, $binding, $operator, $reason, $rebind, $plan) {
|
||||
return $db->transaction(function () use ($db, $runtime, $settings, $binding, $operator, $reason, $rebind, $plan, $reuseComponents, $reused) {
|
||||
$db->selectOne('SELECT pg_advisory_xact_lock(hashtextextended(?, 0))', ['COMPRAS-STANDALONE-INSTALLATION']);
|
||||
$schema = $db->getSchemaBuilder();
|
||||
if (! $schema->hasTable('compras_runtime_installations')) {
|
||||
@@ -63,10 +76,10 @@ final class StandaloneInstallation
|
||||
$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(),
|
||||
'recorded_by' => $operator, 'reason' => $reason.($reused === [] ? '' : "\nReused canonical components: ".json_encode(array_intersect_key(StandaloneComponentSchema::SUPPORTED, array_flip($reused)), JSON_THROW_ON_ERROR)), 'installed_at' => now(),
|
||||
]);
|
||||
if (! $rebind) {
|
||||
StandaloneExtensionSchema::install($db);
|
||||
StandaloneExtensionSchema::install($db, $reuseComponents, $reused);
|
||||
}
|
||||
$runtime->assertInstallation($db);
|
||||
StandaloneSchema::verify($schema, $settings['features']);
|
||||
|
||||
Reference in New Issue
Block a user