12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- <?php
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Schema;
- use Illuminate\Database\Schema\Blueprint;
- use Illuminate\Database\Migrations\Migration;
- use Spatie\Permission\PermissionRegistrar;
- class AddTeamsFields extends Migration
- {
- /**
- * Run the migrations.
- *
- * @return void
- */
- public function up()
- {
- $teams = config('permission.teams');
- $tableNames = config('permission.table_names');
- $columnNames = config('permission.column_names');
- if (! $teams) {
- return;
- }
- if (empty($tableNames)) {
- throw new \Exception('Error: config/permission.php not loaded. Run [php artisan config:clear] and try again.');
- }
- if (empty($columnNames['team_foreign_key'] ?? null)) {
- throw new \Exception('Error: team_foreign_key on config/permission.php not loaded. Run [php artisan config:clear] and try again.');
- }
- if (! Schema::hasColumn($tableNames['roles'], $columnNames['team_foreign_key'])) {
- Schema::table($tableNames['roles'], function (Blueprint $table) use ($columnNames) {
- $table->unsignedBigInteger($columnNames['team_foreign_key'])->nullable()->after('id');
- $table->index($columnNames['team_foreign_key'], 'roles_team_foreign_key_index');
- $table->dropUnique('roles_name_guard_name_unique');
- $table->unique([$columnNames['team_foreign_key'], 'name', 'guard_name']);
- });
- }
- if (! Schema::hasColumn($tableNames['model_has_permissions'], $columnNames['team_foreign_key'])) {
- Schema::table($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames, $columnNames) {
- $table->unsignedBigInteger($columnNames['team_foreign_key'])->default('1');
- $table->index($columnNames['team_foreign_key'], 'model_has_permissions_team_foreign_key_index');
- if (DB::getDriverName() !== 'sqlite') {
- $table->dropForeign([PermissionRegistrar::$pivotPermission]);
- }
- $table->dropPrimary();
- $table->primary([$columnNames['team_foreign_key'], PermissionRegistrar::$pivotPermission, $columnNames['model_morph_key'], 'model_type'],
- 'model_has_permissions_permission_model_type_primary');
- if (DB::getDriverName() !== 'sqlite') {
- $table->foreign(PermissionRegistrar::$pivotPermission)
- ->references('id')->on($tableNames['permissions'])->onDelete('cascade');
- }
- });
- }
- if (! Schema::hasColumn($tableNames['model_has_roles'], $columnNames['team_foreign_key'])) {
- Schema::table($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames) {
- $table->unsignedBigInteger($columnNames['team_foreign_key'])->default('1');;
- $table->index($columnNames['team_foreign_key'], 'model_has_roles_team_foreign_key_index');
- if (DB::getDriverName() !== 'sqlite') {
- $table->dropForeign([PermissionRegistrar::$pivotRole]);
- }
- $table->dropPrimary();
- $table->primary([$columnNames['team_foreign_key'], PermissionRegistrar::$pivotRole, $columnNames['model_morph_key'], 'model_type'],
- 'model_has_roles_role_model_type_primary');
- if (DB::getDriverName() !== 'sqlite') {
- $table->foreign(PermissionRegistrar::$pivotRole)
- ->references('id')->on($tableNames['roles'])->onDelete('cascade');
- }
- });
- }
- app('cache')
- ->store(config('permission.cache.store') != 'default' ? config('permission.cache.store') : null)
- ->forget(config('permission.cache.key'));
- }
- /**
- * Reverse the migrations.
- *
- * @return void
- */
- public function down()
- {
- }
- }
|