Skip to content

Instantly share code, notes, and snippets.

@fabricecw
Last active August 29, 2025 09:26
Show Gist options
  • Select an option

  • Save fabricecw/58ee93dd4f99e78724d8acbb851658a4 to your computer and use it in GitHub Desktop.

Select an option

Save fabricecw/58ee93dd4f99e78724d8acbb851658a4 to your computer and use it in GitHub Desktop.

Revisions

  1. fabricecw revised this gist May 4, 2020. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions migration_permission_update_v1_to_v2.php
    Original file line number Diff line number Diff line change
    @@ -88,19 +88,19 @@ public function up()

    // Migrate V1 tables to V2 tables
    $roles = collect(DB::table('roles_v1')->select()->get())->map(function($x) { return (array) $x + ['guard_name' => config('auth.defaults.guard')]; })->toArray();
    DB::table('roles')->insert($roles);
    DB::table($tableNames['roles'])->insert($roles);

    $permissions = collect(DB::table('permissions_v1')->select()->get())->map(function($x) { return (array) $x + ['guard_name' => config('auth.defaults.guard')]; })->toArray();
    DB::table('permissions')->insert($permissions);
    DB::table($tableNames['permissions'])->insert($permissions);

    $model_has_permissions = collect(DB::table('user_has_permissions')->select(['user_id AS model_id', 'permission_id'])->get())->map(function($x) use ($userModelNamespace) { return (array) $x + ['model_type' => $userModelNamespace]; })->toArray();
    DB::table('model_has_permissions')->insert($model_has_permissions);
    DB::table($tableNames['model_has_permissions'])->insert($model_has_permissions);

    $model_has_roles = collect(DB::table('user_has_roles')->select(['user_id AS model_id', 'role_id'])->get())->map(function($x) use ($userModelNamespace) { return (array) $x + ['model_type' => $userModelNamespace]; })->toArray();
    DB::table('model_has_roles')->insert($model_has_roles);
    DB::table($tableNames['model_has_roles'])->insert($model_has_roles);

    $role_has_permissions = collect(DB::table('role_has_permissions_v1')->select()->get())->map(function($x) { return (array) $x; })->toArray();
    DB::table('role_has_permissions')->insert($role_has_permissions);
    DB::table($tableNames['role_has_permissions'])->insert($role_has_permissions);

    // Drop V1 tables
    // Remove this lines if you want to keep the renamed V1 tables
  2. fabricecw revised this gist Sep 9, 2019. 1 changed file with 7 additions and 4 deletions.
    11 changes: 7 additions & 4 deletions migration_permission_update_v1_to_v2.php
    Original file line number Diff line number Diff line change
    @@ -46,7 +46,7 @@ public function up()
    $table->unsignedInteger('permission_id');
    $table->string('model_type');
    $table->unsignedBigInteger($columnNames['model_morph_key']);
    $table->index([$columnNames['model_morph_key'], 'model_type', ]);
    $table->index([$columnNames['model_morph_key'], 'model_type', ], 'model_has_permissions_model_id_model_type_index');
    $table->foreign('permission_id')
    ->references('id')
    ->on($tableNames['permissions'])
    @@ -59,7 +59,7 @@ public function up()
    $table->unsignedInteger('role_id');
    $table->string('model_type');
    $table->unsignedBigInteger($columnNames['model_morph_key']);
    $table->index([$columnNames['model_morph_key'], 'model_type', ]);
    $table->index([$columnNames['model_morph_key'], 'model_type', ], 'model_has_roles_model_id_model_type_index');
    $table->foreign('role_id')
    ->references('id')
    ->on($tableNames['roles'])
    @@ -79,8 +79,11 @@ public function up()
    ->references('id')
    ->on($tableNames['roles'])
    ->onDelete('cascade');
    $table->primary(['permission_id', 'role_id']);
    app('cache')->forget('spatie.permission.cache');
    $table->primary(['permission_id', 'role_id'], 'role_has_permissions_permission_id_role_id_primary');

    app('cache')
    ->store(config('permission.cache.store') != 'default' ? config('permission.cache.store') : null)
    ->forget(config('permission.cache.key'));
    });

    // Migrate V1 tables to V2 tables
  3. fabricecw revised this gist Jun 4, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion migration_permission_update_v1_to_v2.php
    Original file line number Diff line number Diff line change
    @@ -90,7 +90,7 @@ public function up()
    $permissions = collect(DB::table('permissions_v1')->select()->get())->map(function($x) { return (array) $x + ['guard_name' => config('auth.defaults.guard')]; })->toArray();
    DB::table('permissions')->insert($permissions);

    $model_has_permissions = collect(DB::table('user_has_permissions')->select('user_id AS model_id')->get())->map(function($x) use ($userModelNamespace) { return (array) $x + ['model_type' => $userModelNamespace]; })->toArray();
    $model_has_permissions = collect(DB::table('user_has_permissions')->select(['user_id AS model_id', 'permission_id'])->get())->map(function($x) use ($userModelNamespace) { return (array) $x + ['model_type' => $userModelNamespace]; })->toArray();
    DB::table('model_has_permissions')->insert($model_has_permissions);

    $model_has_roles = collect(DB::table('user_has_roles')->select(['user_id AS model_id', 'role_id'])->get())->map(function($x) use ($userModelNamespace) { return (array) $x + ['model_type' => $userModelNamespace]; })->toArray();
  4. fabricecw revised this gist Dec 2, 2018. 1 changed file with 12 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions migration_permission_update_v1_to_v2.php
    Original file line number Diff line number Diff line change
    @@ -20,24 +20,28 @@ public function up()
    Schema::rename('permissions', 'permissions_v1');
    Schema::rename('roles', 'roles_v1');
    Schema::rename('role_has_permissions', 'role_has_permissions_v1');

    // Drop V1 foreign key constraints
    Schema::table('role_has_permissions_v1', function ($table) {
    $table->dropForeign('role_has_permissions_permission_id_foreign');
    $table->dropForeign('role_has_permissions_role_id_foreign');
    });

    // Create V2.28 tables
    Schema::create($tableNames['permissions'], function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('guard_name');
    $table->timestamps();
    });

    Schema::create($tableNames['roles'], function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('guard_name');
    $table->timestamps();
    });

    Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames, $columnNames) {
    $table->unsignedInteger('permission_id');
    $table->string('model_type');
    @@ -50,6 +54,7 @@ public function up()
    $table->primary(['permission_id', $columnNames['model_morph_key'], 'model_type'],
    'model_has_permissions_permission_model_type_primary');
    });

    Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames) {
    $table->unsignedInteger('role_id');
    $table->string('model_type');
    @@ -62,6 +67,7 @@ public function up()
    $table->primary(['role_id', $columnNames['model_morph_key'], 'model_type'],
    'model_has_roles_role_model_type_primary');
    });

    Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames) {
    $table->unsignedInteger('permission_id');
    $table->unsignedInteger('role_id');
    @@ -76,17 +82,23 @@ public function up()
    $table->primary(['permission_id', 'role_id']);
    app('cache')->forget('spatie.permission.cache');
    });

    // Migrate V1 tables to V2 tables
    $roles = collect(DB::table('roles_v1')->select()->get())->map(function($x) { return (array) $x + ['guard_name' => config('auth.defaults.guard')]; })->toArray();
    DB::table('roles')->insert($roles);

    $permissions = collect(DB::table('permissions_v1')->select()->get())->map(function($x) { return (array) $x + ['guard_name' => config('auth.defaults.guard')]; })->toArray();
    DB::table('permissions')->insert($permissions);

    $model_has_permissions = collect(DB::table('user_has_permissions')->select('user_id AS model_id')->get())->map(function($x) use ($userModelNamespace) { return (array) $x + ['model_type' => $userModelNamespace]; })->toArray();
    DB::table('model_has_permissions')->insert($model_has_permissions);

    $model_has_roles = collect(DB::table('user_has_roles')->select(['user_id AS model_id', 'role_id'])->get())->map(function($x) use ($userModelNamespace) { return (array) $x + ['model_type' => $userModelNamespace]; })->toArray();
    DB::table('model_has_roles')->insert($model_has_roles);

    $role_has_permissions = collect(DB::table('role_has_permissions_v1')->select()->get())->map(function($x) { return (array) $x; })->toArray();
    DB::table('role_has_permissions')->insert($role_has_permissions);

    // Drop V1 tables
    // Remove this lines if you want to keep the renamed V1 tables
    Schema::drop('role_has_permissions_v1');
  5. fabricecw revised this gist Dec 2, 2018. 1 changed file with 2 additions and 29 deletions.
    31 changes: 2 additions & 29 deletions migration_permission_update_v1_to_v2.php
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,8 @@
    <?php

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

    class PermissionUpdateV1ToV2 extends Migration
    {
    /**
    @@ -18,100 +16,77 @@ public function up()
    $columnNames = config('permission.column_names');
    $userModelNamespace = 'App\User'; // Change this value if you didn't use default User namespace in V1


    // Rename V1 tables
    Schema::rename('permissions', 'permissions_v1');
    Schema::rename('roles', 'roles_v1');
    Schema::rename('role_has_permissions', 'role_has_permissions_v1');

    // Drop V1 foreign key constraints
    Schema::table('role_has_permissions_v1', function ($table) {
    $table->dropForeign('role_has_permissions_permission_id_foreign');
    $table->dropForeign('role_has_permissions_role_id_foreign');
    });

    // Create V2.28 tables
    Schema::create($tableNames['permissions'], function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('guard_name');
    $table->timestamps();
    });

    Schema::create($tableNames['roles'], function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('guard_name');
    $table->timestamps();
    });

    Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames, $columnNames) {
    $table->unsignedInteger('permission_id');

    $table->string('model_type');
    $table->unsignedBigInteger($columnNames['model_morph_key']);
    $table->index([$columnNames['model_morph_key'], 'model_type', ]);

    $table->foreign('permission_id')
    ->references('id')
    ->on($tableNames['permissions'])
    ->onDelete('cascade');

    $table->primary(['permission_id', $columnNames['model_morph_key'], 'model_type'],
    'model_has_permissions_permission_model_type_primary');
    });

    Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames) {
    $table->unsignedInteger('role_id');

    $table->string('model_type');
    $table->unsignedBigInteger($columnNames['model_morph_key']);
    $table->index([$columnNames['model_morph_key'], 'model_type', ]);

    $table->foreign('role_id')
    ->references('id')
    ->on($tableNames['roles'])
    ->onDelete('cascade');

    $table->primary(['role_id', $columnNames['model_morph_key'], 'model_type'],
    'model_has_roles_role_model_type_primary');
    });

    Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames) {
    $table->unsignedInteger('permission_id');
    $table->unsignedInteger('role_id');

    $table->foreign('permission_id')
    ->references('id')
    ->on($tableNames['permissions'])
    ->onDelete('cascade');

    $table->foreign('role_id')
    ->references('id')
    ->on($tableNames['roles'])
    ->onDelete('cascade');

    $table->primary(['permission_id', 'role_id']);

    app('cache')->forget('spatie.permission.cache');
    });

    // Migrate V1 tables to V2 tables
    $roles = collect(DB::table('roles_v1')->select()->get())->map(function($x) { return (array) $x + ['guard_name' => config('auth.defaults.guard')]; })->toArray();
    DB::table('roles')->insert($roles);

    $permissions = collect(DB::table('permissions_v1')->select()->get())->map(function($x) { return (array) $x + ['guard_name' => config('auth.defaults.guard')]; })->toArray();
    DB::table('permissions')->insert($permissions);

    $model_has_permissions = collect(DB::table('user_has_permissions')->select('user_id AS model_id')->get())->map(function($x) { return (array) $x + ['model_type' => $userModelNamespace]; })->toArray();
    $model_has_permissions = collect(DB::table('user_has_permissions')->select('user_id AS model_id')->get())->map(function($x) use ($userModelNamespace) { return (array) $x + ['model_type' => $userModelNamespace]; })->toArray();
    DB::table('model_has_permissions')->insert($model_has_permissions);

    $model_has_roles = collect(DB::table('user_has_roles')->select(['user_id AS model_id', 'role_id'])->get())->map(function($x) { return (array) $x + ['model_type' => $userModelNamespace]; })->toArray();
    $model_has_roles = collect(DB::table('user_has_roles')->select(['user_id AS model_id', 'role_id'])->get())->map(function($x) use ($userModelNamespace) { return (array) $x + ['model_type' => $userModelNamespace]; })->toArray();
    DB::table('model_has_roles')->insert($model_has_roles);

    $role_has_permissions = collect(DB::table('role_has_permissions_v1')->select()->get())->map(function($x) { return (array) $x; })->toArray();
    DB::table('role_has_permissions')->insert($role_has_permissions);

    // Drop V1 tables
    // Remove this lines if you want to keep the renamed V1 tables
    Schema::drop('role_has_permissions_v1');
    @@ -120,7 +95,6 @@ public function up()
    Schema::drop('roles_v1');
    Schema::drop('permissions_v1');
    }

    /**
    * Reverse the migrations.
    *
    @@ -131,7 +105,6 @@ public function down()
    // WARNING: You can't rollback to V1 tables with this script!

    $tableNames = config('permission.table_names');

    Schema::drop($tableNames['role_has_permissions']);
    Schema::drop($tableNames['model_has_roles']);
    Schema::drop($tableNames['model_has_permissions']);
  6. fabricecw revised this gist Dec 2, 2018. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions migration_permission_update_v1_to_v2.php
    Original file line number Diff line number Diff line change
    @@ -113,6 +113,7 @@ public function up()
    DB::table('role_has_permissions')->insert($role_has_permissions);

    // Drop V1 tables
    // Remove this lines if you want to keep the renamed V1 tables
    Schema::drop('role_has_permissions_v1');
    Schema::drop('user_has_permissions');
    Schema::drop('user_has_roles');
    @@ -127,6 +128,8 @@ public function up()
    */
    public function down()
    {
    // WARNING: You can't rollback to V1 tables with this script!

    $tableNames = config('permission.table_names');

    Schema::drop($tableNames['role_has_permissions']);
  7. fabricecw revised this gist Dec 2, 2018. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions migration_permission_update_v1_to_v2.php
    Original file line number Diff line number Diff line change
    @@ -16,6 +16,7 @@ public function up()
    {
    $tableNames = config('permission.table_names');
    $columnNames = config('permission.column_names');
    $userModelNamespace = 'App\User'; // Change this value if you didn't use default User namespace in V1


    // Rename V1 tables
    @@ -102,10 +103,10 @@ public function up()
    $permissions = collect(DB::table('permissions_v1')->select()->get())->map(function($x) { return (array) $x + ['guard_name' => config('auth.defaults.guard')]; })->toArray();
    DB::table('permissions')->insert($permissions);

    $model_has_permissions = collect(DB::table('user_has_permissions')->select('user_id AS model_id')->get())->map(function($x) { return (array) $x + ['model_type' => 'App\User']; })->toArray();
    $model_has_permissions = collect(DB::table('user_has_permissions')->select('user_id AS model_id')->get())->map(function($x) { return (array) $x + ['model_type' => $userModelNamespace]; })->toArray();
    DB::table('model_has_permissions')->insert($model_has_permissions);

    $model_has_roles = collect(DB::table('user_has_roles')->select(['user_id AS model_id', 'role_id'])->get())->map(function($x) { return (array) $x + ['model_type' => 'App\User']; })->toArray();
    $model_has_roles = collect(DB::table('user_has_roles')->select(['user_id AS model_id', 'role_id'])->get())->map(function($x) { return (array) $x + ['model_type' => $userModelNamespace]; })->toArray();
    DB::table('model_has_roles')->insert($model_has_roles);

    $role_has_permissions = collect(DB::table('role_has_permissions_v1')->select()->get())->map(function($x) { return (array) $x; })->toArray();
  8. fabricecw revised this gist Dec 2, 2018. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions migration_permission_update_v1_to_v2.php
    Original file line number Diff line number Diff line change
    @@ -16,17 +16,20 @@ public function up()
    {
    $tableNames = config('permission.table_names');
    $columnNames = config('permission.column_names');


    // Rename V1 tables
    Schema::rename('permissions', 'permissions_v1');
    Schema::rename('roles', 'roles_v1');
    Schema::rename('role_has_permissions', 'role_has_permissions_v1');

    // Drop V1 foreign key constraints
    Schema::table('role_has_permissions_v1', function ($table) {
    $table->dropForeign('role_has_permissions_permission_id_foreign');
    $table->dropForeign('role_has_permissions_role_id_foreign');
    });

    // Create V2.28 tables
    Schema::create($tableNames['permissions'], function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    @@ -92,6 +95,7 @@ public function up()
    app('cache')->forget('spatie.permission.cache');
    });

    // Migrate V1 tables to V2 tables
    $roles = collect(DB::table('roles_v1')->select()->get())->map(function($x) { return (array) $x + ['guard_name' => config('auth.defaults.guard')]; })->toArray();
    DB::table('roles')->insert($roles);

    @@ -107,6 +111,7 @@ public function up()
    $role_has_permissions = collect(DB::table('role_has_permissions_v1')->select()->get())->map(function($x) { return (array) $x; })->toArray();
    DB::table('role_has_permissions')->insert($role_has_permissions);

    // Drop V1 tables
    Schema::drop('role_has_permissions_v1');
    Schema::drop('user_has_permissions');
    Schema::drop('user_has_roles');
  9. fabricecw created this gist Dec 2, 2018.
    132 changes: 132 additions & 0 deletions migration_permission_update_v1_to_v2.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,132 @@
    <?php

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

    class PermissionUpdateV1ToV2 extends Migration
    {
    /**
    * Run the migrations.
    *
    * @return void
    */
    public function up()
    {
    $tableNames = config('permission.table_names');
    $columnNames = config('permission.column_names');

    // Rename V1 tables
    Schema::rename('permissions', 'permissions_v1');
    Schema::rename('roles', 'roles_v1');
    Schema::rename('role_has_permissions', 'role_has_permissions_v1');

    Schema::table('role_has_permissions_v1', function ($table) {
    $table->dropForeign('role_has_permissions_permission_id_foreign');
    $table->dropForeign('role_has_permissions_role_id_foreign');
    });

    Schema::create($tableNames['permissions'], function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('guard_name');
    $table->timestamps();
    });

    Schema::create($tableNames['roles'], function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('guard_name');
    $table->timestamps();
    });

    Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames, $columnNames) {
    $table->unsignedInteger('permission_id');

    $table->string('model_type');
    $table->unsignedBigInteger($columnNames['model_morph_key']);
    $table->index([$columnNames['model_morph_key'], 'model_type', ]);

    $table->foreign('permission_id')
    ->references('id')
    ->on($tableNames['permissions'])
    ->onDelete('cascade');

    $table->primary(['permission_id', $columnNames['model_morph_key'], 'model_type'],
    'model_has_permissions_permission_model_type_primary');
    });

    Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames) {
    $table->unsignedInteger('role_id');

    $table->string('model_type');
    $table->unsignedBigInteger($columnNames['model_morph_key']);
    $table->index([$columnNames['model_morph_key'], 'model_type', ]);

    $table->foreign('role_id')
    ->references('id')
    ->on($tableNames['roles'])
    ->onDelete('cascade');

    $table->primary(['role_id', $columnNames['model_morph_key'], 'model_type'],
    'model_has_roles_role_model_type_primary');
    });

    Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames) {
    $table->unsignedInteger('permission_id');
    $table->unsignedInteger('role_id');

    $table->foreign('permission_id')
    ->references('id')
    ->on($tableNames['permissions'])
    ->onDelete('cascade');

    $table->foreign('role_id')
    ->references('id')
    ->on($tableNames['roles'])
    ->onDelete('cascade');

    $table->primary(['permission_id', 'role_id']);

    app('cache')->forget('spatie.permission.cache');
    });

    $roles = collect(DB::table('roles_v1')->select()->get())->map(function($x) { return (array) $x + ['guard_name' => config('auth.defaults.guard')]; })->toArray();
    DB::table('roles')->insert($roles);

    $permissions = collect(DB::table('permissions_v1')->select()->get())->map(function($x) { return (array) $x + ['guard_name' => config('auth.defaults.guard')]; })->toArray();
    DB::table('permissions')->insert($permissions);

    $model_has_permissions = collect(DB::table('user_has_permissions')->select('user_id AS model_id')->get())->map(function($x) { return (array) $x + ['model_type' => 'App\User']; })->toArray();
    DB::table('model_has_permissions')->insert($model_has_permissions);

    $model_has_roles = collect(DB::table('user_has_roles')->select(['user_id AS model_id', 'role_id'])->get())->map(function($x) { return (array) $x + ['model_type' => 'App\User']; })->toArray();
    DB::table('model_has_roles')->insert($model_has_roles);

    $role_has_permissions = collect(DB::table('role_has_permissions_v1')->select()->get())->map(function($x) { return (array) $x; })->toArray();
    DB::table('role_has_permissions')->insert($role_has_permissions);

    Schema::drop('role_has_permissions_v1');
    Schema::drop('user_has_permissions');
    Schema::drop('user_has_roles');
    Schema::drop('roles_v1');
    Schema::drop('permissions_v1');
    }

    /**
    * Reverse the migrations.
    *
    * @return void
    */
    public function down()
    {
    $tableNames = config('permission.table_names');

    Schema::drop($tableNames['role_has_permissions']);
    Schema::drop($tableNames['model_has_roles']);
    Schema::drop($tableNames['model_has_permissions']);
    Schema::drop($tableNames['roles']);
    Schema::drop($tableNames['permissions']);
    }
    }