CasperSecurity
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;
class LaratrustSetupTeams extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
// Create teams table
Schema::create('teams', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->string('display_name')->nullable();
$table->string('description')->nullable();
$table->tinyInteger('status')->default(1);
$table->timestamps();
});
// Update role_user table
Schema::table('role_user', function (Blueprint $table) {
// Drop existing constraints safely
try {
$table->dropForeign(['role_id']);
} catch (\Exception $e) {}
try {
$table->dropPrimary(['user_id', 'role_id', 'user_type']);
} catch (\Exception $e) {}
// Add team_id if not exists
if (!Schema::hasColumn('role_user', 'team_id')) {
$table->unsignedInteger('team_id')->nullable();
}
// Recreate foreign keys
$table->foreign('role_id')
->references('id')->on('roles')
->onUpdate('cascade')->onDelete('cascade');
$table->foreign('team_id')
->references('id')->on('teams')
->onUpdate('cascade')->onDelete('cascade');
// Unique constraint
$table->unique(['user_id', 'role_id', 'user_type', 'team_id'], 'role_user_unique_team');
});
// Update permission_user table
Schema::table('permission_user', function (Blueprint $table) {
// Drop existing constraints safely
try {
$table->dropForeign(['permission_id']);
} catch (\Exception $e) {}
try {
$table->dropPrimary(['permission_id', 'user_id', 'user_type']);
} catch (\Exception $e) {}
// Add team_id if not exists
if (!Schema::hasColumn('permission_user', 'team_id')) {
$table->unsignedInteger('team_id')->nullable();
}
// Recreate foreign keys
$table->foreign('permission_id')
->references('id')->on('permissions')
->onUpdate('cascade')->onDelete('cascade');
$table->foreign('team_id')
->references('id')->on('teams')
->onUpdate('cascade')->onDelete('cascade');
// Unique constraint
$table->unique(['user_id', 'permission_id', 'user_type', 'team_id'], 'permission_user_unique_team');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
// Disable foreign key checks
DB::statement('SET FOREIGN_KEY_CHECKS=0;');
// Remove team_id from role_user
if (Schema::hasTable('role_user')) {
Schema::table('role_user', function (Blueprint $table) {
if (Schema::hasColumn('role_user', 'team_id')) {
$table->dropColumn('team_id');
}
});
}
// Remove team_id from permission_user
if (Schema::hasTable('permission_user')) {
Schema::table('permission_user', function (Blueprint $table) {
if (Schema::hasColumn('permission_user', 'team_id')) {
$table->dropColumn('team_id');
}
});
}
// Drop teams table
Schema::dropIfExists('teams');
// Enable foreign key checks
DB::statement('SET FOREIGN_KEY_CHECKS=1;');
}
}