ultimatepos/database/migrations/2026_06_30_200003_enhance_maintenance_relations.php

82 lines
3.6 KiB
PHP

<?php
use App\Support\LegacyForeignKeys;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::table('maintenance_overhauls', function (Blueprint $table) {
if (! Schema::hasColumn('maintenance_overhauls', 'project_id')) {
$table->unsignedInteger('project_id')->nullable()->after('equipment_id');
$table->foreign('project_id')->references('id')->on('pjt_projects')->nullOnDelete();
}
});
if (! Schema::hasTable('maintenance_equipment_parts')) {
Schema::create('maintenance_equipment_parts', function (Blueprint $table) {
$table->id();
LegacyForeignKeys::business($table);
$table->foreignId('equipment_id')->constrained('maintenance_equipment')->cascadeOnDelete();
$table->foreignId('spare_part_id')->nullable()->constrained('maintenance_spare_parts')->nullOnDelete();
$table->string('part_name');
$table->string('part_code')->nullable();
$table->decimal('quantity', 12, 3)->default(1);
$table->string('unit')->nullable();
$table->boolean('is_critical')->default(false);
$table->text('notes')->nullable();
$table->timestamps();
$table->index(['equipment_id', 'spare_part_id'], 'mnt_eq_parts_eq_sp_idx');
});
}
Schema::table('maintenance_checklist_templates', function (Blueprint $table) {
if (! Schema::hasColumn('maintenance_checklist_templates', 'equipment_id')) {
$table->foreignId('equipment_id')->nullable()->after('equipment_category_id')
->constrained('maintenance_equipment')->nullOnDelete();
}
});
Schema::table('maintenance_checklist_items', function (Blueprint $table) {
if (! Schema::hasColumn('maintenance_checklist_items', 'spare_part_id')) {
$table->foreignId('spare_part_id')->nullable()->after('template_id')
->constrained('maintenance_spare_parts')->nullOnDelete();
}
if (! Schema::hasColumn('maintenance_checklist_items', 'equipment_part_id')) {
$table->foreignId('equipment_part_id')->nullable()->after('spare_part_id')
->constrained('maintenance_equipment_parts')->nullOnDelete();
}
});
}
public function down(): void
{
Schema::table('maintenance_checklist_items', function (Blueprint $table) {
if (Schema::hasColumn('maintenance_checklist_items', 'equipment_part_id')) {
$table->dropConstrainedForeignId('equipment_part_id');
}
if (Schema::hasColumn('maintenance_checklist_items', 'spare_part_id')) {
$table->dropConstrainedForeignId('spare_part_id');
}
});
Schema::table('maintenance_checklist_templates', function (Blueprint $table) {
if (Schema::hasColumn('maintenance_checklist_templates', 'equipment_id')) {
$table->dropConstrainedForeignId('equipment_id');
}
});
Schema::dropIfExists('maintenance_equipment_parts');
Schema::table('maintenance_overhauls', function (Blueprint $table) {
if (Schema::hasColumn('maintenance_overhauls', 'project_id')) {
$table->dropConstrainedForeignId('project_id');
}
});
}
};