unsignedBigInteger('discord_id')->nullable()->after('id'); }); // 2. Migrate Discord Users IDs DB::table('users') ->where('id', '>', 10000) ->update(['discord_id' => DB::raw('id')]); // 3. Temporary new auto increment column Schema::table('users', function (Blueprint $table) { $table->unsignedBigInteger('new_id')->first(); }); // 3.5 Manually count (cursed) $counter = 1; foreach(User::orderBy('id')->get() as $user) { $user->new_id = $counter; $user->save(); $counter++; } // 4. Drop foreign keys $this->dropForeignKeys(); // 5. Fix ID's in other tables $this->updateUserIDsInOtherTables(); // 6. Remove old ID Schema::table('users', function (Blueprint $table) { $table->bigInteger('id')->unsigned()->change(); $table->dropPrimary('id'); $table->dropColumn('id'); }); // 7. Rename new_id to id Schema::table('users', function (Blueprint $table) { $table->renameColumn('new_id', 'id'); $table->unsignedBigInteger('id')->autoIncrement()->primary()->change(); }); // 8. Recreate foreign key constraints $this->addForeignKeys(); } /** * Drop Foreign Keys referencing the user id */ private function dropForeignKeys(): void { Schema::table('markable_likes', function (Blueprint $table) { $table->dropForeign(['user_id']); }); Schema::table('watched', function (Blueprint $table) { $table->dropForeign(['user_id']); }); Schema::table('discord_access_tokens', function (Blueprint $table) { $table->dropForeign(['user_id']); }); // Our Schema does include a foreign key, for whatever reason it doesn't exist in the first palce // Schema::table('user_downloads', function (Blueprint $table) { // $table->dropForeign(['user_id']); // }); } /** * Tables to fix the IDs: * - comments ['commenter_id'] * - discord_access_tokens ['user_id'] * - markable_likes ['user_id'] * - notifications ['notifiable_id'] * - playlists ['user_id'] * - user_downloads ['user_id'] * - watched ['user_id'] */ private function updateUserIDsInOtherTables(): void { DB::table('users')->orderBy('id')->chunk(100, function (Collection $users) { foreach ($users as $user) { DB::table('comments') ->where('commenter_id', $user->id) ->update(['commenter_id' => $user->new_id]); DB::table('discord_access_tokens') ->where('user_id', $user->id) ->update(['user_id' => $user->new_id]); DB::table('markable_likes') ->where('user_id', $user->id) ->update(['user_id' => $user->new_id]); DB::table('notifications') ->where('notifiable_id', $user->id) ->update(['notifiable_id' => $user->new_id]); DB::table('playlists') ->where('user_id', $user->id) ->update(['user_id' => $user->new_id]); DB::table('user_downloads') ->where('user_id', $user->id) ->update(['user_id' => $user->new_id]); DB::table('watched') ->where('user_id', $user->id) ->update(['user_id' => $user->new_id]); } }); } /** * Re-Add Foreign Keys to tables which we dropped previously */ private function addForeignKeys(): void { Schema::table('markable_likes', function (Blueprint $table) { $table->unsignedBigInteger('user_id')->references('id')->on('users')->onDelete('cascade')->change(); }); Schema::table('watched', function (Blueprint $table) { $table->unsignedBigInteger('user_id')->references('id')->on('users')->onDelete('cascade')->change(); }); Schema::table('discord_access_tokens', function (Blueprint $table) { $table->unsignedBigInteger('user_id')->references('id')->on('users')->onDelete('cascade')->change(); }); Schema::table('user_downloads', function (Blueprint $table) { $table->unsignedBigInteger('user_id')->references('id')->on('users')->onDelete('cascade')->change(); }); } };