2018_01_01_000000_create_permission_tables.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /*
  3. * This file is part of the Jiannei/lumen-api-starter.
  4. *
  5. * (c) Jiannei <longjian.huang@foxmail.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. use Illuminate\Database\Migrations\Migration;
  11. use Illuminate\Database\Schema\Blueprint;
  12. use Illuminate\Support\Facades\Schema;
  13. class CreatePermissionTables extends Migration
  14. {
  15. /**
  16. * Run the migrations.
  17. *
  18. * @return void
  19. */
  20. public function up()
  21. {
  22. $tableNames = config('permission.table_names');
  23. $columnNames = config('permission.column_names');
  24. if (empty($tableNames)) {
  25. throw new \Exception('Error: config/permission.php not loaded. Run [php artisan config:clear] and try again.');
  26. }
  27. Schema::create($tableNames['permissions'], function (Blueprint $table) {
  28. $table->bigIncrements('id');
  29. $table->string('name');
  30. $table->string('guard_name');
  31. $table->timestamps();
  32. });
  33. Schema::create($tableNames['roles'], function (Blueprint $table) {
  34. $table->bigIncrements('id');
  35. $table->string('name');
  36. $table->string('guard_name');
  37. $table->timestamps();
  38. });
  39. Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames, $columnNames) {
  40. $table->unsignedBigInteger('permission_id');
  41. $table->string('model_type');
  42. $table->unsignedBigInteger($columnNames['model_morph_key']);
  43. $table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_permissions_model_id_model_type_index');
  44. $table->foreign('permission_id')
  45. ->references('id')
  46. ->on($tableNames['permissions'])
  47. ->onDelete('cascade');
  48. $table->primary(['permission_id', $columnNames['model_morph_key'], 'model_type'],
  49. 'model_has_permissions_permission_model_type_primary');
  50. });
  51. Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames) {
  52. $table->unsignedBigInteger('role_id');
  53. $table->string('model_type');
  54. $table->unsignedBigInteger($columnNames['model_morph_key']);
  55. $table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_roles_model_id_model_type_index');
  56. $table->foreign('role_id')
  57. ->references('id')
  58. ->on($tableNames['roles'])
  59. ->onDelete('cascade');
  60. $table->primary(['role_id', $columnNames['model_morph_key'], 'model_type'],
  61. 'model_has_roles_role_model_type_primary');
  62. });
  63. Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames) {
  64. $table->unsignedBigInteger('permission_id');
  65. $table->unsignedBigInteger('role_id');
  66. $table->foreign('permission_id')
  67. ->references('id')
  68. ->on($tableNames['permissions'])
  69. ->onDelete('cascade');
  70. $table->foreign('role_id')
  71. ->references('id')
  72. ->on($tableNames['roles'])
  73. ->onDelete('cascade');
  74. $table->primary(['permission_id', 'role_id'], 'role_has_permissions_permission_id_role_id_primary');
  75. });
  76. app('cache')
  77. ->store(config('permission.cache.store') != 'default' ? config('permission.cache.store') : null)
  78. ->forget(config('permission.cache.key'));
  79. }
  80. /**
  81. * Reverse the migrations.
  82. *
  83. * @return void
  84. */
  85. public function down()
  86. {
  87. $tableNames = config('permission.table_names');
  88. if (empty($tableNames)) {
  89. 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.');
  90. }
  91. Schema::drop($tableNames['role_has_permissions']);
  92. Schema::drop($tableNames['model_has_roles']);
  93. Schema::drop($tableNames['model_has_permissions']);
  94. Schema::drop($tableNames['roles']);
  95. Schema::drop($tableNames['permissions']);
  96. }
  97. }