@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
use App\Services\BudgetOrderPartsSchema;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
// Optional contract structure. Deliberately outside normal and deployment migrations.
|
||||
@@ -15,66 +15,7 @@ return new class extends Migration
|
||||
if (! app()->environment('testing') || ! $isolated) {
|
||||
throw new LogicException('Order parts require the guarded disposable QA database.');
|
||||
}
|
||||
$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()');
|
||||
}
|
||||
}
|
||||
});
|
||||
BudgetOrderPartsSchema::install($db);
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
use App\Services\PurchaseOrderSourcesSchema;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
// Approved multi-order host adaptation, exclusively on disposable QA.
|
||||
@@ -15,54 +15,7 @@ return new class extends Migration
|
||||
if (! app()->environment('testing') || ! $isolated) {
|
||||
throw new LogicException('Order-source schema requires disposable QA.');
|
||||
}
|
||||
$schema = $db->getSchemaBuilder();
|
||||
if ($schema->hasTable('purchase_order_sources') || $schema->hasColumn('purchase_orders', 'order_key') || ! $schema->hasTable('budget_order_commands')) {
|
||||
throw new LogicException('Install the optional financial schema first; do not reinstall or repair a partial host schema automatically.');
|
||||
}
|
||||
$db->transaction(function () use ($db, $schema): void {
|
||||
$schema->table('purchase_orders', function (Blueprint $table): void {
|
||||
$table->dropUnique(['requisition_id']);
|
||||
$table->dropUnique(['purchase_award_id']);
|
||||
$table->uuid('order_key')->nullable();
|
||||
$table->string('order_payload_hash', 64)->nullable();
|
||||
$table->unsignedInteger('order_source_count')->nullable();
|
||||
$table->unique(['tenant_id', 'order_key']);
|
||||
});
|
||||
$schema->table('purchase_order_items', function (Blueprint $table): void {
|
||||
$table->foreignId('quotation_item_id')->nullable()->constrained()->restrictOnDelete();
|
||||
$table->unsignedBigInteger('quantity_offset_milli')->nullable();
|
||||
});
|
||||
$schema->create('purchase_order_sources', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->foreignId('tenant_id')->constrained()->restrictOnDelete();
|
||||
$table->foreignId('purchase_order_id')->constrained()->restrictOnDelete();
|
||||
$table->foreignId('requisition_id')->constrained()->restrictOnDelete();
|
||||
$table->foreignId('purchase_award_id')->constrained()->restrictOnDelete();
|
||||
$table->foreignId('quotation_id')->constrained()->restrictOnDelete();
|
||||
$table->json('snapshot');
|
||||
$table->unique(['purchase_order_id', 'requisition_id']);
|
||||
});
|
||||
if ($db->getDriverName() === 'pgsql') {
|
||||
$db->statement('CREATE TRIGGER distribution_history_guard BEFORE UPDATE OR DELETE ON purchase_order_sources FOR EACH ROW EXECUTE FUNCTION sgc_distribution_history_guard()');
|
||||
$db->unprepared(<<<'SQL'
|
||||
CREATE FUNCTION sgc_order_parts_header_guard() RETURNS trigger LANGUAGE plpgsql AS $$
|
||||
BEGIN
|
||||
IF OLD.order_key IS NOT NULL THEN
|
||||
IF TG_OP = 'DELETE' THEN RAISE EXCEPTION 'Preserve partial order history'; END IF;
|
||||
IF (to_jsonb(NEW) - 'number' - 'status') IS DISTINCT FROM (to_jsonb(OLD) - 'number' - 'status') THEN
|
||||
RAISE EXCEPTION 'Preserve the frozen partial order';
|
||||
END IF;
|
||||
ELSIF TG_OP = 'UPDATE' AND NEW.order_key IS NOT NULL THEN
|
||||
RAISE EXCEPTION 'Do not relabel an issued legacy order';
|
||||
END IF;
|
||||
IF TG_OP = 'DELETE' THEN RETURN OLD; END IF;
|
||||
RETURN NEW;
|
||||
END $$;
|
||||
CREATE TRIGGER order_parts_header_guard BEFORE UPDATE OR DELETE ON purchase_orders FOR EACH ROW EXECUTE FUNCTION sgc_order_parts_header_guard();
|
||||
CREATE TRIGGER order_parts_line_guard BEFORE UPDATE OR DELETE ON purchase_order_items FOR EACH ROW WHEN (OLD.quotation_item_id IS NOT NULL) EXECUTE FUNCTION sgc_distribution_history_guard();
|
||||
SQL);
|
||||
}
|
||||
});
|
||||
PurchaseOrderSourcesSchema::install($db);
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
use App\Services\BudgetLandedCostSchema;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
// Explicit isolated QA only; not a default or deployment migration.
|
||||
@@ -15,54 +15,7 @@ return new class extends Migration
|
||||
if (! app()->environment('testing') || ! $isolated) {
|
||||
throw new LogicException('Landed cost effects require a guarded disposable QA database.');
|
||||
}
|
||||
$schema = $db->getSchemaBuilder();
|
||||
$tables = ['budget_landed_commands', 'budget_landed_entries'];
|
||||
if ($schema->hasTable($tables[0]) || $schema->hasTable($tables[1])) {
|
||||
throw new LogicException('Never reinstall or repair landed-cost financial history implicitly.');
|
||||
}
|
||||
$db->transaction(function () use ($schema, $db, $tables): void {
|
||||
$schema->create('budget_landed_commands', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->foreignId('tenant_id')->constrained()->restrictOnDelete();
|
||||
$table->foreignId('created_by')->constrained('users')->restrictOnDelete();
|
||||
$table->uuid('operation_key');
|
||||
$table->uuid('plan_key');
|
||||
$table->unsignedInteger('plan_version');
|
||||
$table->string('plan_hash', 64);
|
||||
$table->string('kind', 20);
|
||||
$table->string('payload_hash', 64);
|
||||
$table->json('snapshot');
|
||||
$table->timestamp('created_at');
|
||||
$table->unique(['tenant_id', 'operation_key']);
|
||||
$table->index(['tenant_id', 'plan_key', 'id']);
|
||||
});
|
||||
$schema->create('budget_landed_entries', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->foreignId('tenant_id')->constrained()->restrictOnDelete();
|
||||
$table->foreignId('command_id')->constrained('budget_landed_commands')->restrictOnDelete();
|
||||
$table->foreignId('budget_id')->constrained('budgets')->restrictOnDelete();
|
||||
$table->string('source_key', 120);
|
||||
$table->string('source_kind', 30);
|
||||
$table->unsignedBigInteger('source_id');
|
||||
$table->unsignedInteger('source_version');
|
||||
$table->string('source_revision', 64);
|
||||
$table->json('source_snapshot');
|
||||
$table->bigInteger('reserved_minor');
|
||||
$table->bigInteger('committed_minor');
|
||||
$table->bigInteger('consumed_minor');
|
||||
$table->unique(['command_id', 'source_key']);
|
||||
$table->index(['tenant_id', 'source_key']);
|
||||
});
|
||||
foreach ($tables as $table) {
|
||||
if ($db->getDriverName() === 'pgsql') {
|
||||
$db->unprepared('CREATE FUNCTION preserve_'.$table.'() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN RAISE EXCEPTION \'Preserve landed cost history\'; END $$;');
|
||||
$db->statement('CREATE TRIGGER immutable_landed_history BEFORE UPDATE OR DELETE ON '.$table.' FOR EACH ROW EXECUTE FUNCTION preserve_'.$table.'()');
|
||||
} else {
|
||||
$db->unprepared('CREATE TRIGGER '.$table.'_update BEFORE UPDATE ON '.$table." BEGIN SELECT RAISE(ABORT, 'Preserve landed cost history'); END;");
|
||||
$db->unprepared('CREATE TRIGGER '.$table.'_delete BEFORE DELETE ON '.$table." BEGIN SELECT RAISE(ABORT, 'Preserve landed cost history'); END;");
|
||||
}
|
||||
}
|
||||
});
|
||||
BudgetLandedCostSchema::install($db);
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
use App\Services\RequisitionCycleSchema;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
// Explicit cycle-contract QA only. Never discovered by operational migrate.
|
||||
@@ -15,50 +15,7 @@ return new class extends Migration
|
||||
if (! app()->environment('testing') || ! $isolated) {
|
||||
throw new LogicException('Approval and budget cycle schema requires isolated QA.');
|
||||
}
|
||||
$schema = $db->getSchemaBuilder();
|
||||
if ($schema->hasTable('budget_requisition_cycles') || $schema->hasColumn('requisitions', 'approval_cycle')
|
||||
|| $schema->hasColumn('budget_entries', 'requisition_cycle') || $schema->hasColumn('workflow_notices', 'event_kind')) {
|
||||
throw new LogicException('Do not reinstall or repair partial cycle structures automatically.');
|
||||
}
|
||||
$schema->table('requisitions', fn (Blueprint $table) => $table->unsignedInteger('approval_cycle')->default(1));
|
||||
$schema->table('approval_steps', function (Blueprint $table) {
|
||||
$table->unsignedInteger('approval_cycle')->default(1);
|
||||
$table->dropUnique(['requisition_id', 'position']);
|
||||
$table->unique(['requisition_id', 'approval_cycle', 'position'], 'approval_step_cycle_position');
|
||||
});
|
||||
$schema->table('budget_entries', function (Blueprint $table) {
|
||||
$table->unsignedInteger('requisition_cycle')->default(1);
|
||||
$table->dropUnique(['requisition_id', 'kind']);
|
||||
$table->unique(['requisition_id', 'requisition_cycle', 'kind'], 'budget_entry_cycle_kind');
|
||||
});
|
||||
$schema->table('workflow_notices', function (Blueprint $table) {
|
||||
$table->string('event_kind', 20)->default('task');
|
||||
$table->dropUnique(['approval_step_id', 'recipient_id']);
|
||||
$table->unique(['approval_step_id', 'recipient_id', 'event_kind'], 'workflow_notice_event_recipient');
|
||||
});
|
||||
if ($schema->hasTable('budget_distributions')) {
|
||||
$schema->table('budget_distributions', function (Blueprint $table) {
|
||||
$table->unsignedInteger('requisition_cycle')->default(1);
|
||||
$table->dropUnique(['requisition_id']);
|
||||
$table->unique(['requisition_id', 'requisition_cycle'], 'budget_distribution_cycle');
|
||||
});
|
||||
}
|
||||
$schema->create('budget_requisition_cycles', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('tenant_id')->constrained()->restrictOnDelete();
|
||||
$table->foreignId('requisition_id')->constrained()->restrictOnDelete();
|
||||
$table->unsignedInteger('cycle');
|
||||
$table->string('state', 20);
|
||||
$table->uuid('open_operation');
|
||||
$table->foreignId('opened_by')->constrained('users')->restrictOnDelete();
|
||||
$table->timestamp('opened_at');
|
||||
$table->uuid('close_operation')->nullable();
|
||||
$table->foreignId('closed_by')->nullable()->constrained('users')->restrictOnDelete();
|
||||
$table->timestamp('closed_at')->nullable();
|
||||
$table->unique(['tenant_id', 'requisition_id', 'cycle'], 'budget_requisition_cycle');
|
||||
$table->unique(['tenant_id', 'open_operation']);
|
||||
$table->unique(['tenant_id', 'close_operation']);
|
||||
});
|
||||
RequisitionCycleSchema::install($db);
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
// Explicit installation path; never discovered by the application's normal migrate command.
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
$db = DB::connection();
|
||||
if ($db->getDriverName() !== 'pgsql') {
|
||||
throw new LogicException('COMPRAS_RUNTIME_DATABASE_DRIVER');
|
||||
}
|
||||
$db->getSchemaBuilder()->create('compras_runtime_installations', function (Blueprint $table) {
|
||||
$table->uuid('id')->primary();
|
||||
$table->string('product', 20);
|
||||
$table->string('contract', 60);
|
||||
$table->string('manifest_sha256', 64);
|
||||
$table->string('database_binding_sha256', 64);
|
||||
$table->string('recorded_by', 200);
|
||||
$table->text('reason');
|
||||
$table->timestampTz('installed_at');
|
||||
});
|
||||
$db->unprepared("CREATE FUNCTION compras_runtime_preserve() RETURNS trigger LANGUAGE plpgsql AS \$\$
|
||||
BEGIN RAISE EXCEPTION 'Preserve standalone installation history'; END \$\$;
|
||||
CREATE TRIGGER compras_runtime_immutable BEFORE UPDATE OR DELETE ON compras_runtime_installations
|
||||
FOR EACH ROW EXECUTE FUNCTION compras_runtime_preserve();
|
||||
CREATE TRIGGER compras_runtime_no_truncate BEFORE TRUNCATE ON compras_runtime_installations
|
||||
FOR EACH STATEMENT EXECUTE FUNCTION compras_runtime_preserve();");
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
throw new LogicException('Preserve standalone installation history; use an explicitly reviewed recovery procedure.');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
use App\Services\StandaloneExtensionSchema;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
// Requires a separately recorded explicit installation; no activation or actor grants.
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
StandaloneExtensionSchema::install(DB::connection());
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
throw new LogicException('Use a verified compatible recovery; never delete financial or approval history.');
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user