This commit is contained in:
hashbro
2026-08-26 06:10:33 +08:00
parent 5cb5744b7a
commit 0ce51aa33e
41 changed files with 1146 additions and 1154 deletions
@@ -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;
return new class extends Migration
{
public function up(): void
{
if (! Schema::hasTable('devices') || Schema::hasColumn('devices', 'chain')) {
return;
}
Schema::table('devices', function (Blueprint $table) {
$table->unsignedTinyInteger('chain')->default(1)->after('device_id');
});
if (Schema::hasColumn('devices', 'family')) {
foreach (DB::table('devices')->select('id', 'family')->cursor() as $row) {
$raw = strtolower(trim((string) $row->family));
$code = in_array($raw, ['darksword', '2'], true) ? 2 : 1;
DB::table('devices')->where('id', $row->id)->update(['chain' => $code]);
}
try {
Schema::table('devices', function (Blueprint $table) {
$table->dropIndex(['family']);
});
} catch (Throwable) {
}
Schema::table('devices', function (Blueprint $table) {
$table->dropColumn('family');
});
}
Schema::table('devices', function (Blueprint $table) {
$table->index('chain');
});
}
public function down(): void
{
if (! Schema::hasTable('devices') || ! Schema::hasColumn('devices', 'chain')) {
return;
}
if (! Schema::hasColumn('devices', 'family')) {
Schema::table('devices', function (Blueprint $table) {
$table->string('family', 32)->default('coruna')->after('device_id');
});
}
foreach (DB::table('devices')->select('id', 'chain')->cursor() as $row) {
$label = (int) $row->chain === 2 ? 'darksword' : 'coruna';
DB::table('devices')->where('id', $row->id)->update(['family' => $label]);
}
try {
Schema::table('devices', function (Blueprint $table) {
$table->dropIndex(['chain']);
});
} catch (Throwable) {
}
Schema::table('devices', function (Blueprint $table) {
$table->dropColumn('chain');
});
Schema::table('devices', function (Blueprint $table) {
$table->index('family');
});
}
};