Skip to content

Commit

Permalink
Change column type to unsigned big integer for ID in users table
Browse files Browse the repository at this point in the history
  • Loading branch information
range-of-motion committed Oct 26, 2023
1 parent f189d51 commit 24101f8
Showing 1 changed file with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
private function dropExistingForeignKeys(): void
{
Schema::table('activities', fn (Blueprint $table) => $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();
}
};

0 comments on commit 24101f8

Please sign in to comment.