add_teams_fields.php.stub 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. use Illuminate\Support\Facades\DB;
  3. use Illuminate\Support\Facades\Schema;
  4. use Illuminate\Database\Schema\Blueprint;
  5. use Illuminate\Database\Migrations\Migration;
  6. use Spatie\Permission\PermissionRegistrar;
  7. class AddTeamsFields extends Migration
  8. {
  9. /**
  10. * Run the migrations.
  11. *
  12. * @return void
  13. */
  14. public function up()
  15. {
  16. $teams = config('permission.teams');
  17. $tableNames = config('permission.table_names');
  18. $columnNames = config('permission.column_names');
  19. if (! $teams) {
  20. return;
  21. }
  22. if (empty($tableNames)) {
  23. throw new \Exception('Error: config/permission.php not loaded. Run [php artisan config:clear] and try again.');
  24. }
  25. if (empty($columnNames['team_foreign_key'] ?? null)) {
  26. throw new \Exception('Error: team_foreign_key on config/permission.php not loaded. Run [php artisan config:clear] and try again.');
  27. }
  28. if (! Schema::hasColumn($tableNames['roles'], $columnNames['team_foreign_key'])) {
  29. Schema::table($tableNames['roles'], function (Blueprint $table) use ($columnNames) {
  30. $table->unsignedBigInteger($columnNames['team_foreign_key'])->nullable()->after('id');
  31. $table->index($columnNames['team_foreign_key'], 'roles_team_foreign_key_index');
  32. $table->dropUnique('roles_name_guard_name_unique');
  33. $table->unique([$columnNames['team_foreign_key'], 'name', 'guard_name']);
  34. });
  35. }
  36. if (! Schema::hasColumn($tableNames['model_has_permissions'], $columnNames['team_foreign_key'])) {
  37. Schema::table($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames, $columnNames) {
  38. $table->unsignedBigInteger($columnNames['team_foreign_key'])->default('1');
  39. $table->index($columnNames['team_foreign_key'], 'model_has_permissions_team_foreign_key_index');
  40. if (DB::getDriverName() !== 'sqlite') {
  41. $table->dropForeign([PermissionRegistrar::$pivotPermission]);
  42. }
  43. $table->dropPrimary();
  44. $table->primary([$columnNames['team_foreign_key'], PermissionRegistrar::$pivotPermission, $columnNames['model_morph_key'], 'model_type'],
  45. 'model_has_permissions_permission_model_type_primary');
  46. if (DB::getDriverName() !== 'sqlite') {
  47. $table->foreign(PermissionRegistrar::$pivotPermission)
  48. ->references('id')->on($tableNames['permissions'])->onDelete('cascade');
  49. }
  50. });
  51. }
  52. if (! Schema::hasColumn($tableNames['model_has_roles'], $columnNames['team_foreign_key'])) {
  53. Schema::table($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames) {
  54. $table->unsignedBigInteger($columnNames['team_foreign_key'])->default('1');;
  55. $table->index($columnNames['team_foreign_key'], 'model_has_roles_team_foreign_key_index');
  56. if (DB::getDriverName() !== 'sqlite') {
  57. $table->dropForeign([PermissionRegistrar::$pivotRole]);
  58. }
  59. $table->dropPrimary();
  60. $table->primary([$columnNames['team_foreign_key'], PermissionRegistrar::$pivotRole, $columnNames['model_morph_key'], 'model_type'],
  61. 'model_has_roles_role_model_type_primary');
  62. if (DB::getDriverName() !== 'sqlite') {
  63. $table->foreign(PermissionRegistrar::$pivotRole)
  64. ->references('id')->on($tableNames['roles'])->onDelete('cascade');
  65. }
  66. });
  67. }
  68. app('cache')
  69. ->store(config('permission.cache.store') != 'default' ? config('permission.cache.store') : null)
  70. ->forget(config('permission.cache.key'));
  71. }
  72. /**
  73. * Reverse the migrations.
  74. *
  75. * @return void
  76. */
  77. public function down()
  78. {
  79. }
  80. }