@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Illuminate\Database\Connection;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use LogicException;
|
||||
|
||||
/** Single additive schema definition shared by explicit installation and guarded QA. */
|
||||
final class BudgetOrderPartsSchema
|
||||
{
|
||||
public static function install(Connection $db): void
|
||||
{
|
||||
$schema = $db->getSchemaBuilder();
|
||||
$tables = ['budget_order_commands', 'budget_order_bindings', 'budget_order_allocations', 'budget_order_consumptions'];
|
||||
$found = count(array_filter($tables, fn ($table) => $schema->hasTable($table)));
|
||||
if ($found !== 0) {
|
||||
throw new LogicException('Order-parts schema must be installed once into an empty isolated composition.');
|
||||
}
|
||||
if (! $schema->hasTable('budget_distribution_entries')) {
|
||||
throw new LogicException('Install the explicit V1 distribution schema first.');
|
||||
}
|
||||
$db->transaction(function () use ($schema, $db, $tables): void {
|
||||
$schema->create('budget_order_commands', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->foreignId('tenant_id')->constrained()->restrictOnDelete();
|
||||
$table->string('kind', 20);
|
||||
$table->string('document_key', 60);
|
||||
$table->uuid('operation_key');
|
||||
$table->string('payload_hash', 64);
|
||||
$table->json('result');
|
||||
$table->json('snapshot');
|
||||
$table->foreignId('created_by')->constrained('users')->restrictOnDelete();
|
||||
$table->timestamp('created_at');
|
||||
$table->unique(['tenant_id', 'operation_key']);
|
||||
$table->unique(['tenant_id', 'kind', 'document_key']);
|
||||
});
|
||||
$schema->create('budget_order_bindings', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->foreignId('tenant_id')->constrained()->restrictOnDelete();
|
||||
$table->foreignId('command_id')->constrained('budget_order_commands')->restrictOnDelete();
|
||||
$table->foreignId('distribution_id')->constrained('budget_distributions')->restrictOnDelete();
|
||||
$table->unsignedInteger('source_version');
|
||||
$table->unique(['distribution_id', 'source_version']);
|
||||
$table->unique(['command_id', 'distribution_id']);
|
||||
});
|
||||
foreach (['budget_order_allocations', 'budget_order_consumptions'] as $name) {
|
||||
$schema->create($name, function (Blueprint $table) use ($name): void {
|
||||
$table->id();
|
||||
$table->foreignId('tenant_id')->constrained()->restrictOnDelete();
|
||||
$table->foreignId('command_id')->constrained('budget_order_commands')->restrictOnDelete();
|
||||
$table->foreignId('distribution_id')->constrained('budget_distributions')->restrictOnDelete();
|
||||
$table->uuid('order_key');
|
||||
$table->string('allocation_key', 120);
|
||||
$table->foreignId('budget_id')->constrained('budgets')->restrictOnDelete();
|
||||
$table->bigInteger('subtotal_minor');
|
||||
$table->bigInteger('total_minor');
|
||||
$table->bigInteger('amount_minor');
|
||||
if ($name === 'budget_order_consumptions') {
|
||||
$table->foreignId('invoice_id')->constrained()->restrictOnDelete();
|
||||
} else {
|
||||
$table->string('kind', 20);
|
||||
}
|
||||
$table->unique(['command_id', 'distribution_id', 'order_key', 'allocation_key'], $name === 'budget_order_allocations' ? 'order_allocation_binding' : 'order_consumption_binding');
|
||||
});
|
||||
}
|
||||
if ($db->getDriverName() === 'pgsql') {
|
||||
// Existing function always raises; no mutation or deletion of accepted history.
|
||||
foreach ($tables as $table) {
|
||||
$db->statement('CREATE TRIGGER distribution_history_guard BEFORE UPDATE OR DELETE ON '.$table.' FOR EACH ROW EXECUTE FUNCTION sgc_distribution_history_guard()');
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user