41 lines
2.2 KiB
PHP
41 lines
2.2 KiB
PHP
<?php
|
|
|
|
use Illuminate\Database\Migrations\Migration;
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Schema;
|
|
|
|
return new class extends Migration
|
|
{
|
|
public function up(): void
|
|
{
|
|
Schema::create('warehouse_reservations', function (Blueprint $table) {
|
|
$table->id();
|
|
$table->foreignId('tenant_id')->constrained()->restrictOnDelete();
|
|
$table->foreignId('warehouse_request_id')->constrained()->restrictOnDelete();
|
|
$table->unsignedInteger('line_index');
|
|
$table->foreignId('stock_item_id')->constrained()->restrictOnDelete();
|
|
$table->foreignId('warehouse_id')->constrained()->restrictOnDelete();
|
|
$table->foreignId('authorization_event_id')->constrained('warehouse_request_events')->restrictOnDelete();
|
|
$table->foreignId('actor_id')->constrained('users')->restrictOnDelete();
|
|
$table->uuid('command_key');
|
|
$table->string('kind');
|
|
$table->bigInteger('quantity_milli');
|
|
$table->timestampTz('created_at');
|
|
$table->unique(['warehouse_request_id', 'line_index', 'command_key', 'kind'], 'warehouse_reservation_command_unique');
|
|
$table->index(['tenant_id', 'stock_item_id', 'warehouse_id']);
|
|
});
|
|
if (DB::getDriverName() === 'sqlite') {
|
|
DB::unprepared("CREATE TRIGGER warehouse_reservations_update BEFORE UPDATE ON warehouse_reservations BEGIN SELECT RAISE(ABORT, 'Preservar reservas'); END; CREATE TRIGGER warehouse_reservations_delete BEFORE DELETE ON warehouse_reservations BEGIN SELECT RAISE(ABORT, 'Preservar reservas'); END;");
|
|
}
|
|
if (DB::getDriverName() === 'pgsql') {
|
|
DB::unprepared("CREATE FUNCTION preserve_warehouse_reservations() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN RAISE EXCEPTION 'Preservar reservas'; END $$; CREATE TRIGGER warehouse_reservations_immutable BEFORE UPDATE OR DELETE ON warehouse_reservations FOR EACH ROW EXECUTE FUNCTION preserve_warehouse_reservations();");
|
|
}
|
|
}
|
|
|
|
public function down(): void
|
|
{
|
|
throw new LogicException('Preservar o histórico de reservas e entregas.');
|
|
}
|
|
};
|