create_permission_tables.php.stub 6.4 KB

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