2024_11_28_104056_create_permission_tables.php 6.4 KB

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