Files
compras/database/migrations/2026_11_02_000049_create_warehouse_reservations.php
T
2026-11-02 08:08:05 +02:00

26 lines
2.0 KiB
PHP

<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;
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 { Schema::dropIfExists('warehouse_reservations'); }
};