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

Added migration to add tag_color to maintenance type, rename maintenance asset_id to item_it, add type

This commit is contained in:
snipe
2026-08-14 13:39:14 +01:00
parent 853adb8b9d
commit 6184f38811
2 changed files with 111 additions and 0 deletions

View File

@ -0,0 +1,35 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
/**
* Adds a nullable `tag_color` column to maintenance_types, matching
* the same-name column on other categorizing tables (companies,
* models, categories, locations, departments, etc). Used by the new
* asset-maintenance calendar view (blade: maintenances/calendar) to
* color-code events by maintenance type - the calendar falls back to
* a status-based palette (green/orange/blue for
* completed/active/upcoming) when no tag_color is set on the type.
*/
return new class extends Migration
{
public function up(): void
{
Schema::table('maintenance_types', function (Blueprint $table) {
if (! Schema::hasColumn('maintenance_types', 'tag_color')) {
$table->string('tag_color')->after('name')->nullable()->default(null);
}
});
}
public function down(): void
{
Schema::table('maintenance_types', function (Blueprint $table) {
if (Schema::hasColumn('maintenance_types', 'tag_color')) {
$table->dropColumn('tag_color');
}
});
}
};

View File

@ -0,0 +1,76 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
/**
* Turns maintenances into a polymorphic child so it can be attached
* to accessories (and any other checkoutable) in future, not just
* assets:
*
* asset_id (int) -> item_id (int)
* [new] item_type (string, default App\Models\Asset)
*
* Existing rows all belong to assets, so item_type is backfilled to
* the Asset FQCN in the same migration and then indexed together
* with item_id for polymorphic lookup speed.
*
* The Maintenance model keeps ->asset() and $maintenance->asset_id
* working as legacy accessors on top of the new columns so callers
* (view templates, transformers, controller filters, external API
* consumers) don't break. New callers should use ->item() / item_id
* / item_type directly.
*/
return new class extends Migration
{
public function up(): void
{
// Rename first, add the type column second, backfill third.
// Splitting rename into its own Schema::table() call avoids
// MySQL "cannot rename a column that's about to be indexed"
// combined-DDL edge cases.
Schema::table('maintenances', function (Blueprint $table) {
if (Schema::hasColumn('maintenances', 'asset_id') && ! Schema::hasColumn('maintenances', 'item_id')) {
$table->renameColumn('asset_id', 'item_id');
}
});
Schema::table('maintenances', function (Blueprint $table) {
if (! Schema::hasColumn('maintenances', 'item_type')) {
$table->string('item_type')->after('item_id')->nullable();
}
});
// Backfill: every pre-existing maintenance is against an
// asset. Do this before adding the composite index so index
// build sees the final values.
DB::table('maintenances')
->whereNull('item_type')
->update(['item_type' => \App\Models\Asset::class]);
Schema::table('maintenances', function (Blueprint $table) {
$table->index(['item_type', 'item_id'], 'maintenances_item_type_item_id_index');
});
}
public function down(): void
{
Schema::table('maintenances', function (Blueprint $table) {
$table->dropIndex('maintenances_item_type_item_id_index');
});
Schema::table('maintenances', function (Blueprint $table) {
if (Schema::hasColumn('maintenances', 'item_type')) {
$table->dropColumn('item_type');
}
});
Schema::table('maintenances', function (Blueprint $table) {
if (Schema::hasColumn('maintenances', 'item_id') && ! Schema::hasColumn('maintenances', 'asset_id')) {
$table->renameColumn('item_id', 'asset_id');
}
});
}
};