@@ -8,7 +8,8 @@ use Tempo\Finance\GeneralLedger\LedgerSchema;
|
||||
/** Explicit additive installation only; never called by application boot or a business request. */
|
||||
final class StandaloneExtensionSchema
|
||||
{
|
||||
public static function install(Connection $db): void
|
||||
/** @param list<string> $expectedComponents */
|
||||
public static function install(Connection $db, bool $reuseComponents = false, array $expectedComponents = []): void
|
||||
{
|
||||
$settings = config('standalone-runtime');
|
||||
$settings['enabled'] = true;
|
||||
@@ -35,23 +36,48 @@ final class StandaloneExtensionSchema
|
||||
}
|
||||
$schemas[] = [$schema, $contract, $connectionArgument];
|
||||
}
|
||||
$db->transaction(function () use ($db, $runtime, $settings, $schemas) {
|
||||
$db->transaction(function () use ($db, $runtime, $settings, $schemas, $reuseComponents, $expectedComponents) {
|
||||
$db->selectOne('SELECT pg_advisory_xact_lock(hashtextextended(?, 0))', [$settings['installation_id']]);
|
||||
$runtime->assertInstallation($db);
|
||||
$reused = [];
|
||||
if ($reuseComponents) {
|
||||
// Same company-first ordering as business writers; no schema adoption in a request.
|
||||
$db->statement('LOCK TABLE tenants,requisitions,approval_steps,workflow_notices,budget_entries IN SHARE ROW EXCLUSIVE MODE');
|
||||
foreach (StandaloneComponentSchema::GROUPS as $tables) {
|
||||
foreach ($tables as $table) {
|
||||
if ($db->getSchemaBuilder()->hasTable($table)) {
|
||||
$db->statement('LOCK TABLE '.$table.' IN SHARE ROW EXCLUSIVE MODE');
|
||||
}
|
||||
}
|
||||
}
|
||||
$reused = StandaloneComponentSchema::reusable($db);
|
||||
if ($reused !== $expectedComponents) {
|
||||
throw new \LogicException('COMPRAS_RUNTIME_COMPONENT_PLAN_CHANGED');
|
||||
}
|
||||
}
|
||||
foreach (['budget_distributions', 'budget_order_commands', 'purchase_order_sources', 'budget_landed_commands',
|
||||
'budget_requisition_cycles', 'gl_accounts', 'ps_payments', 'ps_reversals', 'gl_corrections'] as $table) {
|
||||
if ($db->getSchemaBuilder()->hasTable($table)) {
|
||||
if (($table === 'budget_distributions' && in_array('budget-distributions-v1', $reused, true))
|
||||
|| ($table === 'ps_payments' && in_array('supplier-settlements-v1', $reused, true))) {
|
||||
continue;
|
||||
}
|
||||
throw new \LogicException('COMPRAS_RUNTIME_EXTENSION_SCHEMA_EXISTS');
|
||||
}
|
||||
}
|
||||
// The normal application migrations already install the common payables schema.
|
||||
StandaloneSchema::verify($db->getSchemaBuilder(), ['supplier-payables' => true]);
|
||||
BudgetDistributionSchema::install();
|
||||
if (! in_array('budget-distributions-v1', $reused, true)) {
|
||||
BudgetDistributionSchema::install();
|
||||
}
|
||||
BudgetOrderPartsSchema::install($db);
|
||||
PurchaseOrderSourcesSchema::install($db);
|
||||
BudgetLandedCostSchema::install($db);
|
||||
RequisitionCycleSchema::install($db);
|
||||
foreach ($schemas as [$schema, $contract, $connectionArgument]) {
|
||||
if ($contract === 'COMMON-PAYABLE-SETTLEMENT-HOST-SCHEMA-v1' && in_array('supplier-settlements-v1', $reused, true)) {
|
||||
continue;
|
||||
}
|
||||
$arguments = [$contract, fn () => $runtime->assertInstallation($db)];
|
||||
if ($connectionArgument) {
|
||||
array_unshift($arguments, $db);
|
||||
|
||||
Reference in New Issue
Block a user