3
3
use Illuminate \Support \Facades \Schema ;
4
4
use Illuminate \Database \Schema \Blueprint ;
5
5
use Illuminate \Database \Migrations \Migration ;
6
- use Spatie \Permission \PermissionRegistrar ;
7
6
8
- class CreatePermissionTables extends Migration
7
+ return new class extends Migration
9
8
{
10
9
/**
11
10
* Run the migrations.
12
- *
13
- * @return void
14
11
*/
15
- public function up ()
12
+ public function up (): void
16
13
{
14
+ $ teams = config ('permission.teams ' );
17
15
$ tableNames = config ('permission.table_names ' );
18
16
$ 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 ' ;
20
19
21
20
if (empty ($ tableNames )) {
22
21
throw new \Exception ('Error: config/permission.php not loaded. Run [php artisan config:clear] and try again. ' );
@@ -26,22 +25,24 @@ public function up()
26
25
}
27
26
28
27
Schema::create ($ tableNames ['permissions ' ], function (Blueprint $ table ) {
28
+ //$table->engine('InnoDB');
29
29
$ 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 );
32
32
$ table ->timestamps ();
33
33
34
34
$ table ->unique (['name ' , 'guard_name ' ]);
35
35
});
36
36
37
37
Schema::create ($ tableNames ['roles ' ], function (Blueprint $ table ) use ($ teams , $ columnNames ) {
38
+ //$table->engine('InnoDB');
38
39
$ table ->bigIncrements ('id ' ); // role id
39
40
if ($ teams || config ('permission.testing ' )) { // permission.testing is a fix for sqlite testing
40
41
$ table ->unsignedBigInteger ($ columnNames ['team_foreign_key ' ])->nullable ();
41
42
$ table ->index ($ columnNames ['team_foreign_key ' ], 'roles_team_foreign_key_index ' );
42
43
}
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 );
45
46
$ table ->timestamps ();
46
47
if ($ teams || config ('permission.testing ' )) {
47
48
$ table ->unique ([$ columnNames ['team_foreign_key ' ], 'name ' , 'guard_name ' ]);
@@ -50,68 +51,68 @@ public function up()
50
51
}
51
52
});
52
53
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 );
55
56
56
57
$ table ->string ('model_type ' );
57
58
$ table ->unsignedBigInteger ($ columnNames ['model_morph_key ' ]);
58
59
$ table ->index ([$ columnNames ['model_morph_key ' ], 'model_type ' ], 'model_has_permissions_model_id_model_type_index ' );
59
60
60
- $ table ->foreign (PermissionRegistrar:: $ pivotPermission )
61
+ $ table ->foreign ($ pivotPermission )
61
62
->references ('id ' ) // permission id
62
63
->on ($ tableNames ['permissions ' ])
63
64
->onDelete ('cascade ' );
64
65
if ($ teams ) {
65
66
$ table ->unsignedBigInteger ($ columnNames ['team_foreign_key ' ]);
66
67
$ table ->index ($ columnNames ['team_foreign_key ' ], 'model_has_permissions_team_foreign_key_index ' );
67
68
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 ' ],
69
70
'model_has_permissions_permission_model_type_primary ' );
70
71
} else {
71
- $ table ->primary ([PermissionRegistrar:: $ pivotPermission , $ columnNames ['model_morph_key ' ], 'model_type ' ],
72
+ $ table ->primary ([$ pivotPermission , $ columnNames ['model_morph_key ' ], 'model_type ' ],
72
73
'model_has_permissions_permission_model_type_primary ' );
73
74
}
74
75
75
76
});
76
77
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 );
79
80
80
81
$ table ->string ('model_type ' );
81
82
$ table ->unsignedBigInteger ($ columnNames ['model_morph_key ' ]);
82
83
$ table ->index ([$ columnNames ['model_morph_key ' ], 'model_type ' ], 'model_has_roles_model_id_model_type_index ' );
83
84
84
- $ table ->foreign (PermissionRegistrar:: $ pivotRole )
85
+ $ table ->foreign ($ pivotRole )
85
86
->references ('id ' ) // role id
86
87
->on ($ tableNames ['roles ' ])
87
88
->onDelete ('cascade ' );
88
89
if ($ teams ) {
89
90
$ table ->unsignedBigInteger ($ columnNames ['team_foreign_key ' ]);
90
91
$ table ->index ($ columnNames ['team_foreign_key ' ], 'model_has_roles_team_foreign_key_index ' );
91
92
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 ' ],
93
94
'model_has_roles_role_model_type_primary ' );
94
95
} else {
95
- $ table ->primary ([PermissionRegistrar:: $ pivotRole , $ columnNames ['model_morph_key ' ], 'model_type ' ],
96
+ $ table ->primary ([$ pivotRole , $ columnNames ['model_morph_key ' ], 'model_type ' ],
96
97
'model_has_roles_role_model_type_primary ' );
97
98
}
98
99
});
99
100
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 );
103
104
104
- $ table ->foreign (PermissionRegistrar:: $ pivotPermission )
105
+ $ table ->foreign ($ pivotPermission )
105
106
->references ('id ' ) // permission id
106
107
->on ($ tableNames ['permissions ' ])
107
108
->onDelete ('cascade ' );
108
109
109
- $ table ->foreign (PermissionRegistrar:: $ pivotRole )
110
+ $ table ->foreign ($ pivotRole )
110
111
->references ('id ' ) // role id
111
112
->on ($ tableNames ['roles ' ])
112
113
->onDelete ('cascade ' );
113
114
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 ' );
115
116
});
116
117
117
118
app ('cache ' )
@@ -121,10 +122,8 @@ public function up()
121
122
122
123
/**
123
124
* Reverse the migrations.
124
- *
125
- * @return void
126
125
*/
127
- public function down ()
126
+ public function down (): void
128
127
{
129
128
$ tableNames = config ('permission.table_names ' );
130
129
@@ -138,4 +137,4 @@ public function down()
138
137
Schema::drop ($ tableNames ['roles ' ]);
139
138
Schema::drop ($ tableNames ['permissions ' ]);
140
139
}
141
- }
140
+ };
0 commit comments