Files
compras/app/Services/StandaloneExtensionSchema.php
T
nala 30bfb1a773
tests / ci (push) Canceled after 0s
commit compras3 2911
2026-11-29 16:03:11 +02:00

90 lines
4.9 KiB
PHP

<?php
namespace App\Services;
use Illuminate\Database\Connection;
use Tempo\Finance\GeneralLedger\LedgerSchema;
/** Explicit additive installation only; never called by application boot or a business request. */
final class StandaloneExtensionSchema
{
/** @param list<string> $expectedComponents */
public static function install(Connection $db, bool $reuseComponents = false, array $expectedComponents = []): void
{
$settings = config('standalone-runtime');
$settings['enabled'] = true;
$runtime = new StandaloneRuntime($settings, new StandaloneArtifact);
$runtime->assertInstallation($db);
$runtime->assertLoadedPackages(allowMissing: true);
$paths = $runtime->artifact()['paths'];
foreach (StandaloneArtifact::PACKAGES as $package) {
require_once $paths[$package].'/bootstrap.php';
}
$runtime->assertLoadedPackages();
$schemas = [];
foreach ([LedgerSchema::class => ['COMMON-LEDGER-HOST-SCHEMA-v1', true],
'Tempo\\Finance\\Payables\\SettlementSchema' => ['COMMON-PAYABLE-SETTLEMENT-HOST-SCHEMA-v1', false],
'Tempo\\Finance\\Payables\\ReversalSchema' => ['COMMON-PAYABLE-REVERSAL-HOST-SCHEMA-v1', false],
'Tempo\\Finance\\GeneralLedger\\CycleSchema' => ['COMMON-LEDGER-CYCLE-HOST-SCHEMA-v1', true]] as $class => [$contract, $connectionArgument]) {
if (! class_exists($class, false)) {
throw new \LogicException('COMPRAS_RUNTIME_FINANCE_SCHEMA_MISSING');
}
$schema = new \ReflectionClass($class);
if (! $schema->hasConstant('HOST_SCHEMA_CONTRACT') || $schema->getConstant('HOST_SCHEMA_CONTRACT') !== $contract
|| ! $schema->hasMethod('installForHost')) {
throw new \LogicException('COMPRAS_RUNTIME_FINANCE_SCHEMA_CONTRACT');
}
$schemas[] = [$schema, $contract, $connectionArgument];
}
$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]);
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);
}
$schema->getMethod('installForHost')->invokeArgs(null, $arguments);
}
});
}
}