m160728_025849_create_rbac_table.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. use yii\db\Migration;
  3. use yii\db\mysql\Schema;
  4. /**
  5. * Handles the creation for table `{{%rbac}}`.
  6. */
  7. class m160728_025849_create_rbac_table extends Migration
  8. {
  9. public $tableOptions = 'CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE=InnoDB';
  10. /**
  11. * @inheritdoc
  12. */
  13. public function safeUp()
  14. {
  15. $this->execute('SET foreign_key_checks = 0');
  16. // auth_assignment
  17. $this->createTable('{{%auth_assignment}}', [
  18. 'item_name' => Schema::TYPE_STRING . "(64) NOT NULL",
  19. 'user_id' => Schema::TYPE_STRING . "(64) NOT NULL",
  20. 'created_at' => Schema::TYPE_INTEGER . "(11) NULL",
  21. 'PRIMARY KEY (item_name, user_id)',
  22. ], $this->tableOptions);
  23. // auth_item
  24. $this->createTable('{{%auth_item}}', [
  25. 'name' => Schema::TYPE_STRING . "(64) NOT NULL",
  26. 'type' => Schema::TYPE_INTEGER . "(11) NOT NULL",
  27. 'description' => Schema::TYPE_TEXT . " NULL",
  28. 'rule_name' => Schema::TYPE_STRING . "(64) NULL",
  29. 'data' => Schema::TYPE_TEXT . " NULL",
  30. 'created_at' => Schema::TYPE_INTEGER . "(11) NULL",
  31. 'updated_at' => Schema::TYPE_INTEGER . "(11) NULL",
  32. 'PRIMARY KEY (name)',
  33. ], $this->tableOptions);
  34. // auth_item_child
  35. $this->createTable('{{%auth_item_child}}', [
  36. 'parent' => Schema::TYPE_STRING . "(64) NOT NULL",
  37. 'child' => Schema::TYPE_STRING . "(64) NOT NULL",
  38. 'PRIMARY KEY (parent, child)',
  39. ], $this->tableOptions);
  40. // auth_rule
  41. $this->createTable('{{%auth_rule}}', [
  42. 'name' => Schema::TYPE_STRING . "(64) NOT NULL",
  43. 'data' => Schema::TYPE_TEXT . " NULL",
  44. 'created_at' => Schema::TYPE_INTEGER . "(11) NULL",
  45. 'updated_at' => Schema::TYPE_INTEGER . "(11) NULL",
  46. 'PRIMARY KEY (name)',
  47. ], $this->tableOptions);
  48. $this->batchInsert('{{%auth_item}}', ['name', 'type', 'description', 'rule_name', 'data', 'created_at', 'updated_at'], [['superAdmin',1,'超级管理员',NULL,NULL,1443080982,1443408507], ['/*',2,NULL,NULL,NULL,1458640575,1458640575], ['admin',2,'后台登录权限',NULL,NULL,1458640575,1458640575]]);
  49. $this->insert('{{%auth_assignment}}', ['item_name' => 'superAdmin', 'user_id' => '1', 'created_at' => 1443080982]);
  50. $this->batchInsert('{{%auth_item_child}}', ['parent', 'child'], [['superAdmin','/*'],['superAdmin','admin']]);
  51. // fk: auth_assignment
  52. $this->addForeignKey('fk_auth_assignment_item_name', '{{%auth_assignment}}', 'item_name', '{{%auth_item}}', 'name');
  53. // fk: auth_item
  54. $this->addForeignKey('fk_auth_item_rule_name', '{{%auth_item}}', 'rule_name', '{{%auth_rule}}', 'name');
  55. // fk: auth_item_child
  56. $this->addForeignKey('fk_auth_item_child_parent', '{{%auth_item_child}}', 'parent', '{{%auth_item}}', 'name');
  57. $this->addForeignKey('fk_auth_item_child_child', '{{%auth_item_child}}', 'child', '{{%auth_item}}', 'name');
  58. $this->execute('SET foreign_key_checks = 1');
  59. }
  60. /**
  61. * @inheritdoc
  62. */
  63. public function safeDown()
  64. {
  65. $this->execute('SET foreign_key_checks = 0');
  66. $this->dropTable('{{%auth_assignment}}'); // fk: item_name
  67. $this->dropTable('{{%auth_item_child}}'); // fk: parent, child
  68. $this->dropTable('{{%auth_item}}'); // fk: rule_name
  69. $this->dropTable('{{%auth_rule}}');
  70. $this->execute('SET foreign_key_checks = 1');
  71. }
  72. }