Wrap in transaction

This commit is contained in:
Lance Pioch 2024-06-15 07:19:01 -04:00
parent 6976fa8989
commit eff8e509ef

View File

@ -2,6 +2,7 @@
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\Schema;
return new class extends Migration return new class extends Migration
@ -11,40 +12,42 @@ return new class extends Migration
*/ */
public function up(): void public function up(): void
{ {
Schema::table('server_transfers', function (Blueprint $table) { DB::transaction(function () {
$table->dropColumn(['old_allocation', 'new_allocation', 'old_additional_allocations', 'new_additional_allocations']); Schema::table('server_transfers', function (Blueprint $table) {
}); $table->dropColumn(['old_allocation', 'new_allocation', 'old_additional_allocations', 'new_additional_allocations']);
});
Schema::table('servers', function (Blueprint $table) { Schema::table('servers', function (Blueprint $table) {
$table->json('ports')->nullable(); $table->json('ports')->nullable();
}); });
DB::table('servers')->update(['ports' => '[]']); DB::table('servers')->update(['ports' => '[]']);
Schema::table('servers', function (Blueprint $table) { Schema::table('servers', function (Blueprint $table) {
$table->json('ports')->change(); $table->json('ports')->change();
}); });
$portMappings = []; $portMappings = [];
foreach (DB::table('allocations')->get() as $allocation) { foreach (DB::table('allocations')->get() as $allocation) {
$portMappings[$allocation->server_id][] = "$allocation->ip:$allocation->port"; $portMappings[$allocation->server_id][] = "$allocation->ip:$allocation->port";
} }
foreach ($portMappings as $serverId => $ports) { foreach ($portMappings as $serverId => $ports) {
DB::table('servers') DB::table('servers')
->where('id', $serverId) ->where('id', $serverId)
->update(['ports' => json_encode($ports)]); ->update(['ports' => json_encode($ports)]);
} }
Schema::table('servers', function (Blueprint $table) { Schema::table('servers', function (Blueprint $table) {
$table->dropForeign(['allocation_id']); $table->dropUnique(['allocation_id']);
$table->dropColumn(['allocation_id']); $table->dropColumn(['allocation_id']);
}); });
Schema::dropIfExists('allocations'); Schema::dropIfExists('allocations');
Schema::table('nodes', function (Blueprint $table) { Schema::table('nodes', function (Blueprint $table) {
$table->boolean('strict_ports')->default(true); $table->boolean('strict_ports')->default(true);
});
}); });
} }