2025_05_20_073544_create_permission_tables.php 6.4KB

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