3
0
mirror of https://github.com/snipe/snipe-it.git synced 2026-08-18 03:06:23 +00:00

Wrap migration within if (!Schema::hasTable)

This commit is contained in:
snipe
2026-08-11 17:05:05 +01:00
parent f7beec86df
commit fbe3acf3a6

View File

@ -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();
});
// Seed with the 8 built-in types
$now = now();
$types = [
'Maintenance',
'Repair',
'Upgrade',
'PAT Test',
'Calibration',
'Software Support',
'Hardware Support',
'Configuration Change',
];
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();
});
foreach ($types as $name) {
DB::table('maintenance_types')->insert([
'name' => $name,
'created_at' => $now,
'updated_at' => $now,
]);
// 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,
]);
}
}
// 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) {