update
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
<?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
|
||||
{
|
||||
Schema::table('module_vmware', function (Blueprint $table) {
|
||||
if (!Schema::hasColumn('module_vmware', 'cpu')) {
|
||||
$table->integer('cpu')->nullable()->after('hostname');
|
||||
}
|
||||
if (!Schema::hasColumn('module_vmware', 'ram_mb')) {
|
||||
$table->integer('ram_mb')->nullable()->after('cpu');
|
||||
}
|
||||
if (!Schema::hasColumn('module_vmware', 'disk_gb')) {
|
||||
$table->integer('disk_gb')->nullable()->after('ram_mb');
|
||||
}
|
||||
});
|
||||
|
||||
// Backfill vcenter_id, cpu, ram_mb from virtual_machines for rows that are
|
||||
// linked via virtual_machine_id but missing those fields.
|
||||
if (
|
||||
Schema::hasTable('module_vmware')
|
||||
&& Schema::hasTable('virtual_machines')
|
||||
&& Schema::hasTable('module_datacenter')
|
||||
&& Schema::hasColumn('module_vmware', 'virtual_machine_id')
|
||||
) {
|
||||
DB::statement('
|
||||
UPDATE module_vmware mv
|
||||
INNER JOIN virtual_machines vm ON vm.id = mv.virtual_machine_id
|
||||
LEFT JOIN module_datacenter dc ON dc.id = vm.datacenter_id
|
||||
SET
|
||||
mv.vcenter_id = COALESCE(mv.vcenter_id, dc.vcenter_id),
|
||||
mv.cpu = COALESCE(mv.cpu, vm.cpu),
|
||||
mv.ram_mb = COALESCE(mv.ram_mb, vm.ram_mb)
|
||||
WHERE mv.virtual_machine_id IS NOT NULL
|
||||
');
|
||||
}
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('module_vmware', function (Blueprint $table) {
|
||||
foreach (['cpu', 'ram_mb', 'disk_gb'] as $col) {
|
||||
if (Schema::hasColumn('module_vmware', $col)) {
|
||||
$table->dropColumn($col);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user