44use Illuminate \Database \Schema \Blueprint ;
55use Illuminate \Support \Facades \Schema ;
66
7- class CreatePermissionTables extends Migration
7+ return new class extends Migration
88{
99 public function up ()
1010 {
11- Schema::create ('roles ' , function (Blueprint $ table ) {
12- $ table ->id ();
13- $ table ->string ('name ' )->unique ()->index ();
14- $ table ->timestamps ();
15- });
11+ if (!Schema::hasTable ('roles ' )) {
12+ Schema::create ('roles ' , function (Blueprint $ table ) {
13+ $ table ->id ();
14+ $ table ->string ('name ' )->unique ()->index ();
15+ $ table ->timestamps ();
16+ });
17+ }
1618
17- Schema::create ('permissions ' , function (Blueprint $ table ) {
18- $ table ->id ();
19- $ table ->string ('name ' )->unique ()->index ();
20- $ table ->timestamps ();
21- });
19+ if (!Schema::hasTable ('permissions ' )) {
20+ Schema::create ('permissions ' , function (Blueprint $ table ) {
21+ $ table ->id ();
22+ $ table ->string ('name ' )->unique ()->index ();
23+ $ table ->timestamps ();
24+ });
25+ }
2226
23- Schema::create ('role_permission ' , function (Blueprint $ table ) {
24- $ table ->id ();
25- $ table ->foreignId ('role_id ' )->constrained ()->onDelete ('cascade ' );
26- $ table ->foreignId ('permission_id ' )->constrained ()->onDelete ('cascade ' );
27- });
27+ if (!Schema::hasTable ('role_permission ' )) {
28+ Schema::create ('role_permission ' , function (Blueprint $ table ) {
29+ $ table ->id ();
30+ $ table ->foreignId ('role_id ' )->constrained ()->onDelete ('cascade ' );
31+ $ table ->foreignId ('permission_id ' )->constrained ()->onDelete ('cascade ' );
32+ });
33+ }
2834 }
2935
3036 public function down ()
@@ -33,4 +39,4 @@ public function down()
3339 Schema::dropIfExists ('permissions ' );
3440 Schema::dropIfExists ('roles ' );
3541 }
36- }
42+ };
0 commit comments