2024_02_04_012857_create_permission_tables.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. use Illuminate\Support\Facades\Schema;
  3. use Illuminate\Database\Schema\Blueprint;
  4. use Illuminate\Database\Migrations\Migration;
  5. return new class extends Migration
  6. {
  7. /**
  8. * Run the migrations.
  9. */
  10. public function up(): void
  11. {
  12. $teams = config('permission.teams');
  13. $tableNames = config('permission.table_names');
  14. $columnNames = config('permission.column_names');
  15. $pivotRole = $columnNames['role_pivot_key'] ?? 'role_id';
  16. $pivotPermission = $columnNames['permission_pivot_key'] ?? 'permission_id';
  17. if (empty($tableNames)) {
  18. throw new \Exception('Error: config/permission.php not loaded. Run [php artisan config:clear] and try again.');
  19. }
  20. if ($teams && empty($columnNames['team_foreign_key'] ?? null)) {
  21. throw new \Exception('Error: team_foreign_key on config/permission.php not loaded. Run [php artisan config:clear] and try again.');
  22. }
  23. Schema::create($tableNames['permissions'], function (Blueprint $table) {
  24. $table->bigIncrements('id'); // permission id
  25. $table->string('name'); // For MySQL 8.0 use string('name', 125);
  26. $table->string('guard_name'); // For MySQL 8.0 use string('guard_name', 125);
  27. $table->timestamps();
  28. $table->unique(['name', 'guard_name']);
  29. });
  30. Schema::create($tableNames['roles'], function (Blueprint $table) use ($teams, $columnNames) {
  31. $table->bigIncrements('id'); // role id
  32. if ($teams || config('permission.testing')) { // permission.testing is a fix for sqlite testing
  33. $table->unsignedBigInteger($columnNames['team_foreign_key'])->nullable();
  34. $table->index($columnNames['team_foreign_key'], 'roles_team_foreign_key_index');
  35. }
  36. $table->string('name'); // For MySQL 8.0 use string('name', 125);
  37. $table->string('guard_name'); // For MySQL 8.0 use string('guard_name', 125);
  38. $table->timestamps();
  39. if ($teams || config('permission.testing')) {
  40. $table->unique([$columnNames['team_foreign_key'], 'name', 'guard_name']);
  41. } else {
  42. $table->unique(['name', 'guard_name']);
  43. }
  44. });
  45. Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames, $columnNames, $pivotPermission, $teams) {
  46. $table->unsignedBigInteger($pivotPermission);
  47. $table->string('model_type');
  48. $table->unsignedBigInteger($columnNames['model_morph_key']);
  49. $table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_permissions_model_id_model_type_index');
  50. $table->foreign($pivotPermission)
  51. ->references('id') // permission id
  52. ->on($tableNames['permissions'])
  53. ->onDelete('cascade');
  54. if ($teams) {
  55. $table->unsignedBigInteger($columnNames['team_foreign_key']);
  56. $table->index($columnNames['team_foreign_key'], 'model_has_permissions_team_foreign_key_index');
  57. $table->primary([$columnNames['team_foreign_key'], $pivotPermission, $columnNames['model_morph_key'], 'model_type'],
  58. 'model_has_permissions_permission_model_type_primary');
  59. } else {
  60. $table->primary([$pivotPermission, $columnNames['model_morph_key'], 'model_type'],
  61. 'model_has_permissions_permission_model_type_primary');
  62. }
  63. });
  64. Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames, $pivotRole, $teams) {
  65. $table->unsignedBigInteger($pivotRole);
  66. $table->string('model_type');
  67. $table->unsignedBigInteger($columnNames['model_morph_key']);
  68. $table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_roles_model_id_model_type_index');
  69. $table->foreign($pivotRole)
  70. ->references('id') // role id
  71. ->on($tableNames['roles'])
  72. ->onDelete('cascade');
  73. if ($teams) {
  74. $table->unsignedBigInteger($columnNames['team_foreign_key']);
  75. $table->index($columnNames['team_foreign_key'], 'model_has_roles_team_foreign_key_index');
  76. $table->primary([$columnNames['team_foreign_key'], $pivotRole, $columnNames['model_morph_key'], 'model_type'],
  77. 'model_has_roles_role_model_type_primary');
  78. } else {
  79. $table->primary([$pivotRole, $columnNames['model_morph_key'], 'model_type'],
  80. 'model_has_roles_role_model_type_primary');
  81. }
  82. });
  83. Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames, $pivotRole, $pivotPermission) {
  84. $table->unsignedBigInteger($pivotPermission);
  85. $table->unsignedBigInteger($pivotRole);
  86. $table->foreign($pivotPermission)
  87. ->references('id') // permission id
  88. ->on($tableNames['permissions'])
  89. ->onDelete('cascade');
  90. $table->foreign($pivotRole)
  91. ->references('id') // role id
  92. ->on($tableNames['roles'])
  93. ->onDelete('cascade');
  94. $table->primary([$pivotPermission, $pivotRole], 'role_has_permissions_permission_id_role_id_primary');
  95. });
  96. app('cache')
  97. ->store(config('permission.cache.store') != 'default' ? config('permission.cache.store') : null)
  98. ->forget(config('permission.cache.key'));
  99. }
  100. /**
  101. * Reverse the migrations.
  102. */
  103. public function down(): void
  104. {
  105. $tableNames = config('permission.table_names');
  106. if (empty($tableNames)) {
  107. 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.');
  108. }
  109. Schema::drop($tableNames['role_has_permissions']);
  110. Schema::drop($tableNames['model_has_roles']);
  111. Schema::drop($tableNames['model_has_permissions']);
  112. Schema::drop($tableNames['roles']);
  113. Schema::drop($tableNames['permissions']);
  114. }
  115. };