From fbe3acf3a6a3ce5ad8ef8d0345e34d902c1a70d9 Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 11 Aug 2026 17:05:05 +0100 Subject: [PATCH] Wrap migration within if (!Schema::hasTable) --- ..._maintenance_types_and_tracking_fields.php | 74 ++++++++++--------- 1 file changed, 40 insertions(+), 34 deletions(-) diff --git a/database/migrations/2026_05_18_094309_add_maintenance_types_and_tracking_fields.php b/database/migrations/2026_05_18_094309_add_maintenance_types_and_tracking_fields.php index 25201826ce..6837380c11 100644 --- a/database/migrations/2026_05_18_094309_add_maintenance_types_and_tracking_fields.php +++ b/database/migrations/2026_05_18_094309_add_maintenance_types_and_tracking_fields.php @@ -9,48 +9,54 @@ return new class extends Migration { public function up(): void { - // Create maintenance_types lookup table - Schema::create('maintenance_types', function (Blueprint $table) { - $table->id(); - $table->string('name', 100); - $table->unsignedBigInteger('created_by')->nullable(); - $table->softDeletes(); - $table->timestamps(); - }); + + if (!Schema::hasTable('maintenance_types')) { + Schema::create('maintenance_types', function (Blueprint $table) { + $table->id(); + $table->string('name', 100); + $table->unsignedBigInteger('created_by')->nullable(); + $table->softDeletes(); + $table->timestamps(); + }); - // Seed with the 8 built-in types - $now = now(); - $types = [ - 'Maintenance', - 'Repair', - 'Upgrade', - 'PAT Test', - 'Calibration', - 'Software Support', - 'Hardware Support', - 'Configuration Change', - ]; + // Seed with the 8 built-in types. Only runs on the fresh-create + // path so we don't duplicate rows when the table already existed. + $now = now(); + $types = [ + 'Maintenance', + 'Repair', + 'Upgrade', + 'PAT Test', + 'Calibration', + 'Software Support', + 'Hardware Support', + 'Configuration Change', + ]; - foreach ($types as $name) { - DB::table('maintenance_types')->insert([ - 'name' => $name, - 'created_at' => $now, - 'updated_at' => $now, - ]); + foreach ($types as $name) { + DB::table('maintenance_types')->insert([ + 'name' => $name, + 'created_at' => $now, + 'updated_at' => $now, + ]); + } } // Add new tracking columns and the maintenance_type FK to maintenances - Schema::table('maintenances', function (Blueprint $table) { - $table->unsignedBigInteger('maintenance_type_id')->nullable(); - $table->unsignedBigInteger('checked_out_to_id')->nullable(); - $table->string('checked_out_to_type')->nullable(); - $table->unsignedBigInteger('responsible_party_id')->nullable(); - $table->timestamp('completed_at')->nullable(); - $table->unsignedBigInteger('completed_by')->nullable(); - }); + if (!Schema::hasColumn('maintenances', 'maintenance_type_id')) { + Schema::table('maintenances', function (Blueprint $table) { + $table->unsignedBigInteger('maintenance_type_id')->nullable(); + $table->unsignedBigInteger('checked_out_to_id')->nullable(); + $table->string('checked_out_to_type')->nullable(); + $table->unsignedBigInteger('responsible_party_id')->nullable(); + $table->timestamp('completed_at')->nullable(); + $table->unsignedBigInteger('completed_by')->nullable(); + }); + } // Map existing asset_maintenance_type strings to the new FK. // The stored values are the same strings we just seeded into maintenance_types. + // `whereNull('maintenance_type_id')` keeps this safe to re-run. $types = DB::table('maintenance_types')->pluck('id', 'name'); foreach ($types as $name => $id) {