Skip to content

Commit af00a7f

Browse files
committed
chore: permission fixes try
1 parent 79e0c66 commit af00a7f

3 files changed

Lines changed: 146 additions & 19 deletions

File tree

testbench.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ providers:
1414
- Workbench\App\Providers\WorkbenchServiceProvider
1515

1616
migrations:
17-
- vendor/orchestra/testbench-core/laravel/migrations
18-
- vendor/orchestra/testbench-core/laravel/database/migrations
1917
- vendor/spatie/laravel-permission/database/migrations
2018
- workbench/database/migrations
2119
- database/migrations

workbench/database/migrations/2025_08_21_112822_create_permission_tables.php

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
$teams = config('permission.teams');
15+
$tableNames = config('permission.table_names');
16+
$columnNames = config('permission.column_names');
17+
$pivotRole = $columnNames['role_pivot_key'] ?? 'role_id';
18+
$pivotPermission = $columnNames['permission_pivot_key'] ?? 'permission_id';
19+
20+
throw_if(empty($tableNames), new Exception('Error: config/permission.php not loaded. Run [php artisan config:clear] and try again.'));
21+
throw_if($teams && empty($columnNames['team_foreign_key'] ?? null), new Exception('Error: team_foreign_key on config/permission.php not loaded. Run [php artisan config:clear] and try again.'));
22+
23+
if (! Schema::hasTable($tableNames['permissions'])) {
24+
Schema::create($tableNames['permissions'], static function (Blueprint $table) {
25+
// $table->engine('InnoDB');
26+
$table->bigIncrements('id'); // permission id
27+
$table->string('name'); // For MyISAM use string('name', 225); // (or 166 for InnoDB with Redundant/Compact row format)
28+
$table->string('guard_name'); // For MyISAM use string('guard_name', 25);
29+
$table->timestamps();
30+
31+
$table->unique(['name', 'guard_name']);
32+
});
33+
}
34+
35+
if (! Schema::hasTable($tableNames['roles'])) {
36+
Schema::create($tableNames['roles'], static function (Blueprint $table) use ($teams, $columnNames) {
37+
// $table->engine('InnoDB');
38+
$table->bigIncrements('id'); // role id
39+
if ($teams || config('permission.testing')) { // permission.testing is a fix for sqlite testing
40+
$table->unsignedBigInteger($columnNames['team_foreign_key'])->nullable();
41+
$table->index($columnNames['team_foreign_key'], 'roles_team_foreign_key_index');
42+
}
43+
$table->string('name'); // For MyISAM use string('name', 225); // (or 166 for InnoDB with Redundant/Compact row format)
44+
$table->string('guard_name'); // For MyISAM use string('guard_name', 25);
45+
$table->timestamps();
46+
if ($teams || config('permission.testing')) {
47+
$table->unique([$columnNames['team_foreign_key'], 'name', 'guard_name']);
48+
} else {
49+
$table->unique(['name', 'guard_name']);
50+
}
51+
});
52+
}
53+
54+
if (! Schema::hasTable($tableNames['model_has_permissions'])) {
55+
Schema::create($tableNames['model_has_permissions'], static function (Blueprint $table) use ($tableNames, $columnNames, $pivotPermission, $teams) {
56+
$table->unsignedBigInteger($pivotPermission);
57+
58+
$table->string('model_type');
59+
$table->unsignedBigInteger($columnNames['model_morph_key']);
60+
$table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_permissions_model_id_model_type_index');
61+
62+
$table->foreign($pivotPermission)
63+
->references('id') // permission id
64+
->on($tableNames['permissions'])
65+
->onDelete('cascade');
66+
if ($teams) {
67+
$table->unsignedBigInteger($columnNames['team_foreign_key']);
68+
$table->index($columnNames['team_foreign_key'], 'model_has_permissions_team_foreign_key_index');
69+
70+
$table->primary([$columnNames['team_foreign_key'], $pivotPermission, $columnNames['model_morph_key'], 'model_type'],
71+
'model_has_permissions_permission_model_type_primary');
72+
} else {
73+
$table->primary([$pivotPermission, $columnNames['model_morph_key'], 'model_type'],
74+
'model_has_permissions_permission_model_type_primary');
75+
}
76+
77+
});
78+
}
79+
80+
if (! Schema::hasTable($tableNames['model_has_roles'])) {
81+
Schema::create($tableNames['model_has_roles'], static function (Blueprint $table) use ($tableNames, $columnNames, $pivotRole, $teams) {
82+
$table->unsignedBigInteger($pivotRole);
83+
84+
$table->string('model_type');
85+
$table->unsignedBigInteger($columnNames['model_morph_key']);
86+
$table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_roles_model_id_model_type_index');
87+
88+
$table->foreign($pivotRole)
89+
->references('id') // role id
90+
->on($tableNames['roles'])
91+
->onDelete('cascade');
92+
if ($teams) {
93+
$table->unsignedBigInteger($columnNames['team_foreign_key']);
94+
$table->index($columnNames['team_foreign_key'], 'model_has_roles_team_foreign_key_index');
95+
96+
$table->primary([$columnNames['team_foreign_key'], $pivotRole, $columnNames['model_morph_key'], 'model_type'],
97+
'model_has_roles_role_model_type_primary');
98+
} else {
99+
$table->primary([$pivotRole, $columnNames['model_morph_key'], 'model_type'],
100+
'model_has_roles_role_model_type_primary');
101+
}
102+
});
103+
}
104+
105+
if (! Schema::hasTable($tableNames['role_has_permissions'])) {
106+
Schema::create($tableNames['role_has_permissions'], static function (Blueprint $table) use ($tableNames, $pivotRole, $pivotPermission) {
107+
$table->unsignedBigInteger($pivotPermission);
108+
$table->unsignedBigInteger($pivotRole);
109+
110+
$table->foreign($pivotPermission)
111+
->references('id') // permission id
112+
->on($tableNames['permissions'])
113+
->onDelete('cascade');
114+
115+
$table->foreign($pivotRole)
116+
->references('id') // role id
117+
->on($tableNames['roles'])
118+
->onDelete('cascade');
119+
120+
$table->primary([$pivotPermission, $pivotRole], 'role_has_permissions_permission_id_role_id_primary');
121+
});
122+
}
123+
124+
app('cache')
125+
->store(config('permission.cache.store') != 'default' ? config('permission.cache.store') : null)
126+
->forget(config('permission.cache.key'));
127+
}
128+
129+
/**
130+
* Reverse the migrations.
131+
*/
132+
public function down(): void
133+
{
134+
$tableNames = config('permission.table_names');
135+
136+
if (empty($tableNames)) {
137+
throw new \Exception('Error: config/permission.php not found and defaults could not be merged. Please publish the package configuration before proceeding, or drop the tables manually.');
138+
}
139+
140+
Schema::drop($tableNames['role_has_permissions']);
141+
Schema::drop($tableNames['model_has_roles']);
142+
Schema::drop($tableNames['model_has_permissions']);
143+
Schema::drop($tableNames['roles']);
144+
Schema::drop($tableNames['permissions']);
145+
}
146+
};

0 commit comments

Comments
 (0)