This commit is contained in:
baodev0
2026-06-22 08:43:32 +07:00
parent 3243473843
commit 0c436153c6
22 changed files with 1389 additions and 95 deletions
@@ -0,0 +1,104 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
/**
* Adds `status` (tinyint, 0=inactive / 1=active) to module_host and module_network,
* then backfills 1-active-per-parent across the full provisioning chain:
*
* module_vcenter (1 active globally)
* module_datacenter (1 active per vCenter)
* module_host (1 active per datacenter)
* module_datastore (1 active per host)
* module_network (1 active per host)
*
* Picks the smallest-id row per parent as the active one when none exists yet,
* matching the deterministic "first row wins" rule confirmed with the product owner.
*/
return new class extends Migration {
public function up(): void
{
if (!Schema::hasColumn('module_host', 'status')) {
Schema::table('module_host', function (Blueprint $table) {
$table->tinyInteger('status')->default(0)->after('iso_path');
});
}
if (!Schema::hasColumn('module_network', 'status')) {
Schema::table('module_network', function (Blueprint $table) {
$table->tinyInteger('status')->default(0)->after('module_host_id');
});
}
$this->backfillOneActiveGlobal('module_vcenter');
$this->backfillOneActivePerParent('module_datacenter', 'vcenter_id');
$this->backfillOneActivePerParent('module_host', 'module_datacenter_id');
$this->backfillOneActivePerParent('module_datastore', 'module_host_id');
$this->backfillOneActivePerParent('module_network', 'module_host_id');
}
public function down(): void
{
if (Schema::hasColumn('module_network', 'status')) {
Schema::table('module_network', function (Blueprint $table) {
$table->dropColumn('status');
});
}
if (Schema::hasColumn('module_host', 'status')) {
Schema::table('module_host', function (Blueprint $table) {
$table->dropColumn('status');
});
}
}
/** Keep one global active row (smallest id wins), zero out the rest. */
private function backfillOneActiveGlobal(string $table): void
{
$activeId = DB::table($table)->where('status', 1)->orderBy('id')->value('id')
?? DB::table($table)->orderBy('id')->value('id');
if (!$activeId) {
return;
}
DB::table($table)->where('id', '!=', $activeId)->update(['status' => 0]);
DB::table($table)->where('id', $activeId)->update(['status' => 1]);
}
/** For each parent value, keep one child active (smallest id wins). */
private function backfillOneActivePerParent(string $table, string $parentColumn): void
{
$parents = DB::table($table)->select($parentColumn)->distinct()->pluck($parentColumn);
foreach ($parents as $parentId) {
if ($parentId === null) {
continue;
}
$activeId = DB::table($table)
->where($parentColumn, $parentId)
->where('status', 1)
->orderBy('id')
->value('id');
$activeId = $activeId ?? DB::table($table)
->where($parentColumn, $parentId)
->orderBy('id')
->value('id');
if (!$activeId) {
continue;
}
DB::table($table)
->where($parentColumn, $parentId)
->where('id', '!=', $activeId)
->update(['status' => 0]);
DB::table($table)->where('id', $activeId)->update(['status' => 1]);
}
}
};