From 24101f85a2a0d73511ed4ae88d56c3e666e6bcab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Drunen?= Date: Fri, 27 Oct 2023 00:32:37 +0200 Subject: [PATCH] Change column type to unsigned big integer for ID in users table --- ...gned_big_integer_for_id_in_users_table.php | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 database/migrations/2023_10_26_220756_change_column_type_to_unsigned_big_integer_for_id_in_users_table.php diff --git a/database/migrations/2023_10_26_220756_change_column_type_to_unsigned_big_integer_for_id_in_users_table.php b/database/migrations/2023_10_26_220756_change_column_type_to_unsigned_big_integer_for_id_in_users_table.php new file mode 100644 index 00000000..391cc76a --- /dev/null +++ b/database/migrations/2023_10_26_220756_change_column_type_to_unsigned_big_integer_for_id_in_users_table.php @@ -0,0 +1,56 @@ + $table->dropForeign(['user_id'])); + Schema::table('ideas', fn (Blueprint $table) => $table->dropForeign(['user_id'])); + Schema::table('login_attempts', fn (Blueprint $table) => $table->dropForeign(['user_id'])); + Schema::table('user_space', fn (Blueprint $table) => $table->dropForeign(['user_id'])); + Schema::table('widgets', fn (Blueprint $table) => $table->dropForeign(['user_id'])); + } + + private function reAddForeignKeys(): void + { + Schema::table('activities', fn (Blueprint $table) => $table->foreign('user_id')->references('id')->on('users')); + Schema::table('ideas', fn (Blueprint $table) => $table->foreign('user_id')->references('id')->on('users')); + Schema::table('login_attempts', fn (Blueprint $table) => $table->foreign('user_id')->references('id')->on('users')); + Schema::table('user_space', fn (Blueprint $table) => $table->foreign('user_id')->references('id')->on('users')); + Schema::table('widgets', fn (Blueprint $table) => $table->foreign('user_id')->references('id')->on('users')); + } + + public function up(): void + { + $this->dropExistingForeignKeys(); + + Schema::table('users', fn (Blueprint $table) => $table->id()->change()); + + Schema::table('activities', fn (Blueprint $table) => $table->foreignId('user_id')->change()); + Schema::table('ideas', fn (Blueprint $table) => $table->foreignId('user_id')->change()); + Schema::table('login_attempts', fn (Blueprint $table) => $table->foreignId('user_id')->change()); + Schema::table('user_space', fn (Blueprint $table) => $table->foreignId('user_id')->change()); + Schema::table('widgets', fn (Blueprint $table) => $table->foreignId('user_id')->change()); + + $this->reAddForeignKeys(); + } + + public function down(): void + { + $this->dropExistingForeignKeys(); + + Schema::table('users', fn (Blueprint $table) => $table->increments('id')->change()); + + Schema::table('activities', fn (Blueprint $table) => $table->unsignedInteger('user_id')->change()); + Schema::table('ideas', fn (Blueprint $table) => $table->unsignedInteger('user_id')->change()); + Schema::table('login_attempts', fn (Blueprint $table) => $table->unsignedInteger('user_id')->change()); + Schema::table('user_space', fn (Blueprint $table) => $table->unsignedInteger('user_id')->change()); + Schema::table('widgets', fn (Blueprint $table) => $table->unsignedInteger('user_id')->change()); + + $this->reAddForeignKeys(); + } +};