2024_08_14_093112_create_permission_tables.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. return new class extends Migration {
  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')->comment('权限名称'); // For MyISAM use string('name', 225); // (or 166 for InnoDB with Redundant/Compact row format)
  27. $table->string('guard_name')->default('web'); // 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')->comment('角色名称'); // For MyISAM use string('name', 225); // (or 166 for InnoDB with Redundant/Compact row format)
  39. $table->string('guard_name')->default('web'); // For MyISAM use string('guard_name', 25);
  40. $table->string('code', 100)->default('')->comment('角色权限标识');
  41. $table->integer('sort')->default(0)->comment('显示顺序');
  42. $table->tinyInteger('data_scope')->default(1)->comment('数据范围(1:全部数据 2:指定部门数据 3:所属部门数据 4:所属部门及下级数据)');
  43. $table->json('data_scope_dept_ids')->nullable()->comment('数据范围(指定部门)');
  44. $table->tinyInteger('status')->default(0)->comment('角色状态');
  45. $table->tinyInteger('type')->comment('角色类型');
  46. $table->text('remark')->nullable()->comment('备注');
  47. $table->string('creator', 64)->nullable()->comment('创建者');
  48. $table->string('updater', 64)->nullable()->comment('更新者');
  49. $table->timestamps();
  50. $table->softDeletes();
  51. if ($teams || config('permission.testing')) {
  52. $table->unique([$columnNames['team_foreign_key'], 'name', 'guard_name']);
  53. } else {
  54. $table->unique(['name', 'guard_name']);
  55. }
  56. });
  57. Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames, $columnNames, $pivotPermission, $teams) {
  58. $table->unsignedBigInteger($pivotPermission);
  59. $table->string('model_type');
  60. $table->unsignedBigInteger($columnNames['model_morph_key']);
  61. $table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_permissions_model_id_model_type_index');
  62. $table->foreign($pivotPermission)
  63. ->references('id') // permission id
  64. ->on($tableNames['permissions'])
  65. ->onDelete('cascade');
  66. if ($teams) {
  67. $table->unsignedBigInteger($columnNames['team_foreign_key']);
  68. $table->index($columnNames['team_foreign_key'], 'model_has_permissions_team_foreign_key_index');
  69. $table->primary([$columnNames['team_foreign_key'], $pivotPermission, $columnNames['model_morph_key'], 'model_type'],
  70. 'model_has_permissions_permission_model_type_primary');
  71. } else {
  72. $table->primary([$pivotPermission, $columnNames['model_morph_key'], 'model_type'],
  73. 'model_has_permissions_permission_model_type_primary');
  74. }
  75. });
  76. Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames, $pivotRole, $teams) {
  77. $table->unsignedBigInteger($pivotRole);
  78. $table->string('model_type');
  79. $table->unsignedBigInteger($columnNames['model_morph_key']);
  80. $table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_roles_model_id_model_type_index');
  81. $table->foreign($pivotRole)
  82. ->references('id') // role id
  83. ->on($tableNames['roles'])
  84. ->onDelete('cascade');
  85. if ($teams) {
  86. $table->unsignedBigInteger($columnNames['team_foreign_key']);
  87. $table->index($columnNames['team_foreign_key'], 'model_has_roles_team_foreign_key_index');
  88. $table->primary([$columnNames['team_foreign_key'], $pivotRole, $columnNames['model_morph_key'], 'model_type'],
  89. 'model_has_roles_role_model_type_primary');
  90. } else {
  91. $table->primary([$pivotRole, $columnNames['model_morph_key'], 'model_type'],
  92. 'model_has_roles_role_model_type_primary');
  93. }
  94. });
  95. Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames, $pivotRole, $pivotPermission) {
  96. $table->unsignedBigInteger($pivotPermission);
  97. $table->unsignedBigInteger($pivotRole);
  98. $table->foreign($pivotPermission)
  99. ->references('id') // permission id
  100. ->on($tableNames['permissions'])
  101. ->onDelete('cascade');
  102. $table->foreign($pivotRole)
  103. ->references('id') // role id
  104. ->on($tableNames['roles'])
  105. ->onDelete('cascade');
  106. $table->primary([$pivotPermission, $pivotRole], 'role_has_permissions_permission_id_role_id_primary');
  107. });
  108. app('cache')
  109. ->store(config('permission.cache.store') != 'default' ? config('permission.cache.store') : null)
  110. ->forget(config('permission.cache.key'));
  111. }
  112. /**
  113. * Reverse the migrations.
  114. */
  115. public function down(): void
  116. {
  117. $tableNames = config('permission.table_names');
  118. if (empty($tableNames)) {
  119. 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.');
  120. }
  121. Schema::drop($tableNames['role_has_permissions']);
  122. Schema::drop($tableNames['model_has_roles']);
  123. Schema::drop($tableNames['model_has_permissions']);
  124. Schema::drop($tableNames['roles']);
  125. Schema::drop($tableNames['permissions']);
  126. }
  127. };