Replace Auth System #3
@@ -5,6 +5,7 @@ use App\Models\Downloads;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
|
||||
154
database/migrations/2025_12_03_193018_fix_user_ids.php
Normal file
154
database/migrations/2025_12_03_193018_fix_user_ids.php
Normal file
@@ -0,0 +1,154 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
use App\Models\User;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
// 1. Create new column discord_id
|
||||
Schema::table('users', function (Blueprint $table) {
|
||||
$table->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();
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
// Remove tables from larascord
|
||||
Schema::dropIfExists('discord_access_tokens');
|
||||
Schema::dropIfExists('personal_access_tokens');
|
||||
|
||||
// Drop columns from larascord
|
||||
Schema::table('users', function (Blueprint $table) {
|
||||
$table->dropColumn('discriminator');
|
||||
$table->dropColumn('remember_token');
|
||||
$table->dropColumn('banner');
|
||||
$table->dropColumn('banner_color');
|
||||
$table->dropColumn('accent_color');
|
||||
$table->dropColumn('premium_type');
|
||||
$table->dropColumn('public_flags');
|
||||
$table->dropColumn('verified');
|
||||
$table->dropColumn('mfa_enabled');
|
||||
});
|
||||
|
||||
// Change & Add Columns
|
||||
Schema::table('users', function (Blueprint $table) {
|
||||
// Rename
|
||||
$table->renameColumn('username', 'name');
|
||||
$table->renameColumn('global_name', 'discord_name');
|
||||
$table->renameColumn('avatar', 'discord_avatar');
|
||||
|
||||
// Re-Add Email verification
|
||||
$table->timestamp('email_verified_at')->nullable()->after('email');
|
||||
|
||||
// Re-Add Password Auth
|
||||
$table->string('password')->nullable()->after('email_verified_at');
|
||||
$table->rememberToken()->after('password');
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user