41 lines
1.8 KiB
PHP
41 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace Tempo\Finance\Payables;
|
|
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Schema;
|
|
|
|
final class PaymentDecisionSchema
|
|
{
|
|
public static function install(): void
|
|
{
|
|
$driver = DB::connection()->getDriverName();
|
|
if (! in_array($driver, ['pgsql', 'sqlite'], true) || ! Schema::hasTable('pa_imported_payments') || Schema::hasTable('pa_payment_decisions')) {
|
|
throw new \LogicException('Histórico anterior obrigatório; não adoptar tabela de decisões existente.');
|
|
}
|
|
Schema::create('pa_payment_decisions', function (Blueprint $table) {
|
|
$table->id();
|
|
foreach (['tenant_id', 'company_id', 'unit_id'] as $key) {
|
|
$table->unsignedBigInteger($key);
|
|
}
|
|
$table->foreignId('obligation_id')->constrained('pa_obligations');
|
|
$table->foreignId('imported_payment_id')->unique()->constrained('pa_imported_payments');
|
|
$table->unsignedInteger('sequence');
|
|
$table->string('previous_hash', 64);
|
|
$table->string('history_hash', 64);
|
|
$table->jsonb('payment_snapshot');
|
|
$table->unsignedBigInteger('actor_id');
|
|
$table->timestamp('created_at');
|
|
$table->unique(['obligation_id', 'sequence']);
|
|
});
|
|
if ($driver === 'pgsql') {
|
|
DB::unprepared('CREATE TRIGGER pa_decision_immutable BEFORE UPDATE OR DELETE ON pa_payment_decisions FOR EACH ROW EXECUTE FUNCTION pa_append_only()');
|
|
} else {
|
|
foreach (['UPDATE', 'DELETE'] as $operation) {
|
|
DB::unprepared('CREATE TRIGGER pa_decision_'.strtolower($operation)." BEFORE {$operation} ON pa_payment_decisions BEGIN SELECT RAISE(ABORT, 'PA_APPEND_ONLY'); END");
|
|
}
|
|
}
|
|
}
|
|
}
|