Skip to content

Commit 1e2cf00

Browse files
committed
permission migration issues fix
1 parent be44d18 commit 1e2cf00

File tree

1 file changed

+29
-30
lines changed

1 file changed

+29
-30
lines changed

database/migrations/2022_09_30_172105_create_permission_tables.php renamed to database/migrations/2024_11_25_022836_create_permission_tables.php

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,19 @@
33
use Illuminate\Support\Facades\Schema;
44
use Illuminate\Database\Schema\Blueprint;
55
use Illuminate\Database\Migrations\Migration;
6-
use Spatie\Permission\PermissionRegistrar;
76

8-
class CreatePermissionTables extends Migration
7+
return new class extends Migration
98
{
109
/**
1110
* Run the migrations.
12-
*
13-
* @return void
1411
*/
15-
public function up()
12+
public function up(): void
1613
{
14+
$teams = config('permission.teams');
1715
$tableNames = config('permission.table_names');
1816
$columnNames = config('permission.column_names');
19-
$teams = config('permission.teams');
17+
$pivotRole = $columnNames['role_pivot_key'] ?? 'role_id';
18+
$pivotPermission = $columnNames['permission_pivot_key'] ?? 'permission_id';
2019

2120
if (empty($tableNames)) {
2221
throw new \Exception('Error: config/permission.php not loaded. Run [php artisan config:clear] and try again.');
@@ -26,22 +25,24 @@ public function up()
2625
}
2726

2827
Schema::create($tableNames['permissions'], function (Blueprint $table) {
28+
//$table->engine('InnoDB');
2929
$table->bigIncrements('id'); // permission id
30-
$table->string('name'); // For MySQL 8.0 use string('name', 125);
31-
$table->string('guard_name'); // For MySQL 8.0 use string('guard_name', 125);
30+
$table->string('name'); // For MyISAM use string('name', 225); // (or 166 for InnoDB with Redundant/Compact row format)
31+
$table->string('guard_name'); // For MyISAM use string('guard_name', 25);
3232
$table->timestamps();
3333

3434
$table->unique(['name', 'guard_name']);
3535
});
3636

3737
Schema::create($tableNames['roles'], function (Blueprint $table) use ($teams, $columnNames) {
38+
//$table->engine('InnoDB');
3839
$table->bigIncrements('id'); // role id
3940
if ($teams || config('permission.testing')) { // permission.testing is a fix for sqlite testing
4041
$table->unsignedBigInteger($columnNames['team_foreign_key'])->nullable();
4142
$table->index($columnNames['team_foreign_key'], 'roles_team_foreign_key_index');
4243
}
43-
$table->string('name'); // For MySQL 8.0 use string('name', 125);
44-
$table->string('guard_name'); // For MySQL 8.0 use string('guard_name', 125);
44+
$table->string('name'); // For MyISAM use string('name', 225); // (or 166 for InnoDB with Redundant/Compact row format)
45+
$table->string('guard_name'); // For MyISAM use string('guard_name', 25);
4546
$table->timestamps();
4647
if ($teams || config('permission.testing')) {
4748
$table->unique([$columnNames['team_foreign_key'], 'name', 'guard_name']);
@@ -50,68 +51,68 @@ public function up()
5051
}
5152
});
5253

53-
Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames, $columnNames, $teams) {
54-
$table->unsignedBigInteger(PermissionRegistrar::$pivotPermission);
54+
Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames, $columnNames, $pivotPermission, $teams) {
55+
$table->unsignedBigInteger($pivotPermission);
5556

5657
$table->string('model_type');
5758
$table->unsignedBigInteger($columnNames['model_morph_key']);
5859
$table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_permissions_model_id_model_type_index');
5960

60-
$table->foreign(PermissionRegistrar::$pivotPermission)
61+
$table->foreign($pivotPermission)
6162
->references('id') // permission id
6263
->on($tableNames['permissions'])
6364
->onDelete('cascade');
6465
if ($teams) {
6566
$table->unsignedBigInteger($columnNames['team_foreign_key']);
6667
$table->index($columnNames['team_foreign_key'], 'model_has_permissions_team_foreign_key_index');
6768

68-
$table->primary([$columnNames['team_foreign_key'], PermissionRegistrar::$pivotPermission, $columnNames['model_morph_key'], 'model_type'],
69+
$table->primary([$columnNames['team_foreign_key'], $pivotPermission, $columnNames['model_morph_key'], 'model_type'],
6970
'model_has_permissions_permission_model_type_primary');
7071
} else {
71-
$table->primary([PermissionRegistrar::$pivotPermission, $columnNames['model_morph_key'], 'model_type'],
72+
$table->primary([$pivotPermission, $columnNames['model_morph_key'], 'model_type'],
7273
'model_has_permissions_permission_model_type_primary');
7374
}
7475

7576
});
7677

77-
Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames, $teams) {
78-
$table->unsignedBigInteger(PermissionRegistrar::$pivotRole);
78+
Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames, $pivotRole, $teams) {
79+
$table->unsignedBigInteger($pivotRole);
7980

8081
$table->string('model_type');
8182
$table->unsignedBigInteger($columnNames['model_morph_key']);
8283
$table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_roles_model_id_model_type_index');
8384

84-
$table->foreign(PermissionRegistrar::$pivotRole)
85+
$table->foreign($pivotRole)
8586
->references('id') // role id
8687
->on($tableNames['roles'])
8788
->onDelete('cascade');
8889
if ($teams) {
8990
$table->unsignedBigInteger($columnNames['team_foreign_key']);
9091
$table->index($columnNames['team_foreign_key'], 'model_has_roles_team_foreign_key_index');
9192

92-
$table->primary([$columnNames['team_foreign_key'], PermissionRegistrar::$pivotRole, $columnNames['model_morph_key'], 'model_type'],
93+
$table->primary([$columnNames['team_foreign_key'], $pivotRole, $columnNames['model_morph_key'], 'model_type'],
9394
'model_has_roles_role_model_type_primary');
9495
} else {
95-
$table->primary([PermissionRegistrar::$pivotRole, $columnNames['model_morph_key'], 'model_type'],
96+
$table->primary([$pivotRole, $columnNames['model_morph_key'], 'model_type'],
9697
'model_has_roles_role_model_type_primary');
9798
}
9899
});
99100

100-
Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames) {
101-
$table->unsignedBigInteger(PermissionRegistrar::$pivotPermission);
102-
$table->unsignedBigInteger(PermissionRegistrar::$pivotRole);
101+
Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames, $pivotRole, $pivotPermission) {
102+
$table->unsignedBigInteger($pivotPermission);
103+
$table->unsignedBigInteger($pivotRole);
103104

104-
$table->foreign(PermissionRegistrar::$pivotPermission)
105+
$table->foreign($pivotPermission)
105106
->references('id') // permission id
106107
->on($tableNames['permissions'])
107108
->onDelete('cascade');
108109

109-
$table->foreign(PermissionRegistrar::$pivotRole)
110+
$table->foreign($pivotRole)
110111
->references('id') // role id
111112
->on($tableNames['roles'])
112113
->onDelete('cascade');
113114

114-
$table->primary([PermissionRegistrar::$pivotPermission, PermissionRegistrar::$pivotRole], 'role_has_permissions_permission_id_role_id_primary');
115+
$table->primary([$pivotPermission, $pivotRole], 'role_has_permissions_permission_id_role_id_primary');
115116
});
116117

117118
app('cache')
@@ -121,10 +122,8 @@ public function up()
121122

122123
/**
123124
* Reverse the migrations.
124-
*
125-
* @return void
126125
*/
127-
public function down()
126+
public function down(): void
128127
{
129128
$tableNames = config('permission.table_names');
130129

@@ -138,4 +137,4 @@ public function down()
138137
Schema::drop($tableNames['roles']);
139138
Schema::drop($tableNames['permissions']);
140139
}
141-
}
140+
};

0 commit comments

Comments
 (0)