64 lines
3.3 KiB
PHP
64 lines
3.3 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
|
|
{
|
|
public static function install(Connection $db): 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) {
|
|
$db->selectOne('SELECT pg_advisory_xact_lock(hashtextextended(?, 0))', [$settings['installation_id']]);
|
|
$runtime->assertInstallation($db);
|
|
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)) {
|
|
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();
|
|
BudgetOrderPartsSchema::install($db);
|
|
PurchaseOrderSourcesSchema::install($db);
|
|
BudgetLandedCostSchema::install($db);
|
|
RequisitionCycleSchema::install($db);
|
|
foreach ($schemas as [$schema, $contract, $connectionArgument]) {
|
|
$arguments = [$contract, fn () => $runtime->assertInstallation($db)];
|
|
if ($connectionArgument) {
|
|
array_unshift($arguments, $db);
|
|
}
|
|
$schema->getMethod('installForHost')->invokeArgs(null, $arguments);
|
|
}
|
|
});
|
|
}
|
|
}
|