From e6566cbd546a295e06618a96763fe27a0701c053 Mon Sep 17 00:00:00 2001 From: Eric Wang <37554696+ericwang401@users.noreply.github.com> Date: Thu, 7 Nov 2024 21:34:07 -0500 Subject: [PATCH] Update migrations to be agnostic --- ...100_move_network_column_in_nodes_table.php | 5 +- ..._column_order_in_template_groups_table.php | 6 +- ...t_column_to_behind_fqdn_in_nodes_table.php | 20 ++-- ..._032248_add_uuid_column_to_users_table.php | 9 +- ...ove_types_column_in_ip_addresses_table.php | 13 ++- ..._gateways_lowercase_in_addresses_table.php | 15 ++- ..._backups_completed_at_to_errors_column.php | 35 +++---- ...ve_timestamps_from_address_pools_table.php | 2 +- ...napshot_limit_columns_on_servers_table.php | 93 +++++++------------ ...ize_column_on_backups_snapshots_tables.php | 37 ++++---- ...date_vmid_column_type_on_servers_table.php | 23 ++--- 11 files changed, 117 insertions(+), 141 deletions(-) diff --git a/database/migrations/2022_12_06_233100_move_network_column_in_nodes_table.php b/database/migrations/2022_12_06_233100_move_network_column_in_nodes_table.php index a451df835b8..383ca59b40b 100644 --- a/database/migrations/2022_12_06_233100_move_network_column_in_nodes_table.php +++ b/database/migrations/2022_12_06_233100_move_network_column_in_nodes_table.php @@ -2,7 +2,6 @@ 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 { @@ -12,7 +11,7 @@ public function up(): void { Schema::table('nodes', function (Blueprint $table) { - DB::statement('ALTER TABLE nodes MODIFY COLUMN network varchar(255) AFTER backup_storage'); + $table->string('network')->after('backup_storage')->change(); }); } @@ -22,7 +21,7 @@ public function up(): void public function down(): void { Schema::table('nodes', function (Blueprint $table) { - DB::statement('ALTER TABLE nodes MODIFY COLUMN network varchar(255) AFTER vm_storage'); + $table->string('network')->after('vm_storage')->change(); }); } }; diff --git a/database/migrations/2022_12_25_011208_fix_node_id_column_order_in_template_groups_table.php b/database/migrations/2022_12_25_011208_fix_node_id_column_order_in_template_groups_table.php index 8d57843f019..d94fc99ba21 100644 --- a/database/migrations/2022_12_25_011208_fix_node_id_column_order_in_template_groups_table.php +++ b/database/migrations/2022_12_25_011208_fix_node_id_column_order_in_template_groups_table.php @@ -2,7 +2,6 @@ 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 { @@ -12,7 +11,8 @@ public function up(): void { Schema::table('template_groups', function (Blueprint $table) { - DB::statement('ALTER TABLE template_groups MODIFY COLUMN node_id bigint unsigned AFTER id'); + + $table->unsignedBigInteger('node_id')->after('id')->change(); }); } @@ -22,7 +22,7 @@ public function up(): void public function down(): void { Schema::table('template_groups', function (Blueprint $table) { - DB::statement('ALTER TABLE template_groups MODIFY COLUMN node_id bigint unsigned AFTER updated_at'); + $table->unsignedBigInteger('node_id')->after('updated_at')->change(); }); } }; diff --git a/database/migrations/2023_05_21_015205_move_port_column_to_behind_fqdn_in_nodes_table.php b/database/migrations/2023_05_21_015205_move_port_column_to_behind_fqdn_in_nodes_table.php index a40226bbeee..a95a0937215 100644 --- a/database/migrations/2023_05_21_015205_move_port_column_to_behind_fqdn_in_nodes_table.php +++ b/database/migrations/2023_05_21_015205_move_port_column_to_behind_fqdn_in_nodes_table.php @@ -2,23 +2,23 @@ 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 { - /** - * Run the migrations. - */ +return new class extends Migration +{ public function up(): void { - DB::statement('ALTER TABLE nodes MODIFY COLUMN port int AFTER fqdn'); + Schema::table('nodes', function (Blueprint $table) { + // Explicitly include all desired attributes for Laravel 11 + $table->integer('port')->nullable(false)->after('fqdn')->change(); + }); } - /** - * Reverse the migrations. - */ public function down(): void { - DB::statement('ALTER TABLE nodes MODIFY COLUMN port int AFTER secret'); + Schema::table('nodes', function (Blueprint $table) { + // Move the column back with all necessary attributes specified + $table->integer('port')->nullable(false)->after('secret')->change(); + }); } }; diff --git a/database/migrations/2023_05_28_032248_add_uuid_column_to_users_table.php b/database/migrations/2023_05_28_032248_add_uuid_column_to_users_table.php index 2b4a1361b24..f1e1a487d35 100644 --- a/database/migrations/2023_05_28_032248_add_uuid_column_to_users_table.php +++ b/database/migrations/2023_05_28_032248_add_uuid_column_to_users_table.php @@ -1,8 +1,8 @@ char('uuid', 36)->nullable()->after('id'); }); - DB::statement('UPDATE users SET uuid=(select UUID())'); + User::chunk(100, function ($users) { + foreach ($users as $user) { + $user->uuid = Str::uuid()->toString(); + $user->save(); + } + }); } /** diff --git a/database/migrations/2023_09_03_174812_move_types_column_in_ip_addresses_table.php b/database/migrations/2023_09_03_174812_move_types_column_in_ip_addresses_table.php index 3e60bb6542c..27d3725e7c1 100644 --- a/database/migrations/2023_09_03_174812_move_types_column_in_ip_addresses_table.php +++ b/database/migrations/2023_09_03_174812_move_types_column_in_ip_addresses_table.php @@ -4,13 +4,17 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class extends Migration { +return new class extends Migration +{ /** * Run the migrations. */ public function up(): void { - DB::statement('ALTER TABLE ip_addresses MODIFY COLUMN type varchar(255) AFTER server_id'); + Schema::table('ip_addresses', function (Blueprint $table) { + // Modify the 'type' column to be after 'server_id' with all necessary attributes + $table->string('type', 255)->after('server_id')->change(); + }); } /** @@ -18,6 +22,9 @@ public function up(): void */ public function down(): void { - DB::statement('ALTER TABLE ip_addresses MODIFY COLUMN type varchar(255) AFTER mac_address'); + Schema::table('ip_addresses', function (Blueprint $table) { + // Move the 'type' column to be after 'mac_address' with all necessary attributes + $table->string('type', 255)->after('mac_address')->change(); + }); } }; diff --git a/database/migrations/2024_01_01_191916_make_addresses_and_mac_addresses_and_gateways_lowercase_in_addresses_table.php b/database/migrations/2024_01_01_191916_make_addresses_and_mac_addresses_and_gateways_lowercase_in_addresses_table.php index 39f8c56d597..282fde12e3c 100644 --- a/database/migrations/2024_01_01_191916_make_addresses_and_mac_addresses_and_gateways_lowercase_in_addresses_table.php +++ b/database/migrations/2024_01_01_191916_make_addresses_and_mac_addresses_and_gateways_lowercase_in_addresses_table.php @@ -1,12 +1,19 @@ address = strtolower($ip->address); + $ip->gateway = strtolower($ip->gateway); + $ip->mac_address = strtolower($ip->mac_address); + $ip->save(); + } + }); } }; diff --git a/database/migrations/2024_10_03_222715_change_backups_completed_at_to_errors_column.php b/database/migrations/2024_10_03_222715_change_backups_completed_at_to_errors_column.php index e8197738d10..671e82e028a 100644 --- a/database/migrations/2024_10_03_222715_change_backups_completed_at_to_errors_column.php +++ b/database/migrations/2024_10_03_222715_change_backups_completed_at_to_errors_column.php @@ -5,16 +5,15 @@ use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Schema; -return new class extends Migration { - /** - * Run the migrations. - */ +return new class extends Migration +{ public function up(): void { - // Delete records based on conditions - DB::statement( - 'DELETE FROM backups WHERE completed_at IS NOT NULL AND is_successful = 0;', - ); + // Delete records based on conditions without raw SQL + DB::table('backups') + ->whereNotNull('completed_at') + ->where('is_successful', 0) + ->delete(); Schema::table('backups', function (Blueprint $table) { $table->string('description')->nullable()->after('name'); @@ -22,31 +21,23 @@ public function up(): void $table->dropSoftDeletes(); $table->dropColumn(['is_successful', 'updated_at']); - }); - // Modify the 'is_locked' column to be not nullable with a default of false (0) - DB::statement( - 'ALTER TABLE backups MODIFY COLUMN is_locked TINYINT(1) NOT NULL DEFAULT 0 AFTER description', - ); + // Explicitly define 'is_locked' attributes to prevent unintended behavior + $table->boolean('is_locked')->default(false)->nullable(false)->after('description')->change(); + }); } - /** - * Reverse the migrations. - */ public function down(): void { Schema::table('backups', function (Blueprint $table) { $table->dropColumn(['description', 'errors']); - $table->boolean('is_locked')->nullable()->change(); + // Re-apply all attributes to 'is_locked' in down migration + $table->boolean('is_locked')->nullable()->default(null)->after('is_successful')->change(); + $table->boolean('is_successful')->default(false)->after('server_id'); $table->timestamp('updated_at')->nullable()->after('created_at'); $table->softDeletes()->after('updated_at'); }); - - // Modify the 'is_locked' column to be nullable in the down migration - DB::statement( - 'ALTER TABLE backups MODIFY COLUMN is_locked TINYINT(1) NULL AFTER is_successful', - ); } }; diff --git a/database/migrations/2024_10_10_025946_remove_timestamps_from_address_pools_table.php b/database/migrations/2024_10_10_025946_remove_timestamps_from_address_pools_table.php index a0a6b2afb61..2eaaa8ad44d 100644 --- a/database/migrations/2024_10_10_025946_remove_timestamps_from_address_pools_table.php +++ b/database/migrations/2024_10_10_025946_remove_timestamps_from_address_pools_table.php @@ -24,6 +24,6 @@ public function down(): void $table->after('name', fn (Blueprint $table) => $table->timestamps()); }); - DB::statement('UPDATE address_pools SET created_at = NOW()'); + DB::table('address_pools')->update(['created_at' => now()]); } }; diff --git a/database/migrations/2024_10_10_033133_update_backup_snapshot_limit_columns_on_servers_table.php b/database/migrations/2024_10_10_033133_update_backup_snapshot_limit_columns_on_servers_table.php index 227d054e4dd..e4b68b3e87a 100644 --- a/database/migrations/2024_10_10_033133_update_backup_snapshot_limit_columns_on_servers_table.php +++ b/database/migrations/2024_10_10_033133_update_backup_snapshot_limit_columns_on_servers_table.php @@ -4,83 +4,54 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class extends Migration { - /** - * Run the migrations. - */ +return new class extends Migration +{ public function up(): void { - DB::statement( - 'ALTER TABLE servers MODIFY COLUMN snapshot_limit INT NULL;', - ); - DB::statement( - 'ALTER TABLE servers MODIFY COLUMN backup_limit INT NULL;', - ); - DB::statement( - 'ALTER TABLE servers MODIFY COLUMN bandwidth_limit INT NULL;', - ); - DB::statement( - 'ALTER TABLE servers MODIFY COLUMN bandwidth_usage INT UNSIGNED NOT NULL DEFAULT 0;', - ); - - DB::statement('UPDATE servers SET snapshot_limit = -1 WHERE snapshot_limit IS NULL;'); - DB::statement('UPDATE servers SET backup_limit = -1 WHERE backup_limit IS NULL;'); - DB::statement('UPDATE servers SET bandwidth_limit = -1 WHERE bandwidth_limit IS NULL;'); + Schema::table('servers', function (Blueprint $table) { + $table->integer('snapshot_limit')->nullable()->change(); + $table->integer('backup_limit')->nullable()->change(); + $table->integer('bandwidth_limit')->nullable()->change(); + $table->unsignedInteger('bandwidth_usage')->default(0)->change(); + }); - DB::statement( - 'ALTER TABLE servers CHANGE COLUMN snapshot_limit snapshot_count_limit INT NOT NULL;', - ); - DB::statement( - 'ALTER TABLE servers CHANGE COLUMN backup_limit backup_count_limit INT NOT NULL;', - ); - DB::statement( - 'ALTER TABLE servers CHANGE COLUMN bandwidth_limit bandwidth_limit INT NOT NULL;', - ); + DB::table('servers')->whereNull('snapshot_limit')->update(['snapshot_limit' => -1]); + DB::table('servers')->whereNull('backup_limit')->update(['backup_limit' => -1]); + DB::table('servers')->whereNull('bandwidth_limit')->update(['bandwidth_limit' => -1]); Schema::table('servers', function (Blueprint $table) { + $table->renameColumn('snapshot_limit', 'snapshot_count_limit'); + $table->renameColumn('backup_limit', 'backup_count_limit'); + $table->renameColumn('bandwidth_limit', 'bandwidth_limit'); $table->integer('snapshot_size_limit')->after('snapshot_count_limit'); $table->integer('backup_size_limit')->after('backup_count_limit'); }); + + Schema::table('servers', function (Blueprint $table) { + $table->integer('snapshot_count_limit')->nullable(false)->change(); + $table->integer('backup_count_limit')->nullable(false)->change(); + $table->integer('bandwidth_limit')->nullable(false)->change(); + }); } - /** - * Reverse the migrations. - */ public function down(): void { - DB::statement( - 'ALTER TABLE servers MODIFY COLUMN snapshot_count_limit INT NULL;', - ); - DB::statement( - 'ALTER TABLE servers MODIFY COLUMN backup_count_limit INT NULL;', - ); - DB::statement( - 'ALTER TABLE servers MODIFY COLUMN bandwidth_limit INT NULL;', - ); - DB::statement( - 'ALTER TABLE servers MODIFY COLUMN bandwidth_usage INT NOT NULL DEFAULT 0;', - ); + Schema::table('servers', function (Blueprint $table) { + $table->integer('snapshot_count_limit')->nullable()->change(); + $table->integer('backup_count_limit')->nullable()->change(); + $table->integer('bandwidth_limit')->nullable()->change(); + $table->integer('bandwidth_usage')->default(0)->change(); + }); - DB::statement( - 'UPDATE servers SET snapshot_count_limit = NULL WHERE snapshot_count_limit = -1;', - ); - DB::statement( - 'UPDATE servers SET backup_count_limit = NULL WHERE backup_count_limit = -1;', - ); - DB::statement('UPDATE servers SET bandwidth_limit = NULL WHERE bandwidth_limit = -1;'); + DB::table('servers')->where('snapshot_count_limit', -1)->update(['snapshot_count_limit' => null]); + DB::table('servers')->where('backup_count_limit', -1)->update(['backup_count_limit' => null]); + DB::table('servers')->where('bandwidth_limit', -1)->update(['bandwidth_limit' => null]); Schema::table('servers', function (Blueprint $table) { $table->dropColumn('snapshot_size_limit', 'backup_size_limit'); + $table->renameColumn('snapshot_count_limit', 'snapshot_limit'); + $table->renameColumn('backup_count_limit', 'backup_limit'); + $table->renameColumn('bandwidth_limit', 'bandwidth_limit'); }); - - DB::statement( - 'ALTER TABLE servers CHANGE COLUMN snapshot_count_limit snapshot_limit INT UNSIGNED NULL;', - ); - DB::statement( - 'ALTER TABLE servers CHANGE COLUMN backup_count_limit backup_limit INT UNSIGNED NULL;', - ); - DB::statement( - 'ALTER TABLE servers CHANGE COLUMN bandwidth_limit bandwidth_limit INT UNSIGNED NULL;', - ); } }; diff --git a/database/migrations/2024_10_10_192039_update_size_column_on_backups_snapshots_tables.php b/database/migrations/2024_10_10_192039_update_size_column_on_backups_snapshots_tables.php index 3d198f34c38..f93a8cb6617 100644 --- a/database/migrations/2024_10_10_192039_update_size_column_on_backups_snapshots_tables.php +++ b/database/migrations/2024_10_10_192039_update_size_column_on_backups_snapshots_tables.php @@ -1,31 +1,30 @@ unsignedInteger('size')->nullable(false)->change(); + }); + + Schema::table('snapshots', function (Blueprint $table) { + $table->unsignedInteger('size')->nullable(false)->change(); + }); } - /** - * Reverse the migrations. - */ public function down(): void { - DB::statement( - 'ALTER TABLE backups MODIFY COLUMN size BIGINT UNSIGNED NOT NULL DEFAULT 0;', - ); - DB::statement( - 'ALTER TABLE snapshots MODIFY COLUMN size BIGINT UNSIGNED NOT NULL DEFAULT 0;', - ); + Schema::table('backups', function (Blueprint $table) { + $table->unsignedBigInteger('size')->default(0)->nullable(false)->change(); + }); + + Schema::table('snapshots', function (Blueprint $table) { + $table->unsignedBigInteger('size')->default(0)->nullable(false)->change(); + }); } }; diff --git a/database/migrations/2024_10_10_194915_update_vmid_column_type_on_servers_table.php b/database/migrations/2024_10_10_194915_update_vmid_column_type_on_servers_table.php index 8d926fe07d9..c92f451cfc6 100644 --- a/database/migrations/2024_10_10_194915_update_vmid_column_type_on_servers_table.php +++ b/database/migrations/2024_10_10_194915_update_vmid_column_type_on_servers_table.php @@ -1,25 +1,22 @@ unsignedInteger('vmid')->nullable(false)->change(); + }); } - /** - * Reverse the migrations. - */ public function down(): void { - DB::statement( - 'ALTER TABLE servers MODIFY COLUMN vmid BIGINT UNSIGNED NOT NULL;', - ); + Schema::table('servers', function (Blueprint $table) { + $table->unsignedBigInteger('vmid')->nullable(false)->change(); + }); } };