diff --git a/README.md b/README.md index b98f8ac..f1dde43 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,15 @@ ] ], ``` +* **创建所需要的表** +``` +//用户表user和菜单表menu +yii migrate --migrationPath=@vendor/windhoney/yii2-rest-rbac/migrations +//rbac相关权限表 +yii migrate --migrationPath=@yii/rbac/migrations/ +//oauth2相关表 +yii migrate --migrationPath=@vendor/filsh/yii2-oauth2-server/migrations +``` * **添加路由配置** diff --git a/components/Configs.php b/components/Configs.php index cccb70f..570f0aa 100644 --- a/components/Configs.php +++ b/components/Configs.php @@ -57,9 +57,9 @@ class Configs extends \yii\base\Object /** * @var string MenuOld table name. */ - public $menuTable = '{{menu}}'; + public $menuTable = 'menu'; /** - * @var string MenuOld table name. + * @var string Menu table name. */ public $userTable = 'user'; /** diff --git a/migrations/m140602_111327_create_menu_table.php b/migrations/m140602_111327_create_menu_table.php new file mode 100644 index 0000000..fb416db --- /dev/null +++ b/migrations/m140602_111327_create_menu_table.php @@ -0,0 +1,43 @@ + + * @since 1.0 + */ +class m140602_111327_create_menu_table extends \yii\db\Migration +{ + + /** + * @inheritdoc + */ + public function up() + { + $menuTable = Configs::instance()->menuTable; + $tableOptions = null; + if ($this->db->driverName === 'mysql') { + $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB'; + } + + $this->createTable($menuTable, [ + 'id' => $this->primaryKey(), + 'name' => $this->string(128)->notNull(), + 'parent' => $this->integer(), + 'route' => $this->string(), + 'order' => $this->integer(), + 'data' => $this->binary(), + "FOREIGN KEY ([[parent]]) REFERENCES {$menuTable}([[id]]) ON DELETE SET NULL ON UPDATE CASCADE", + ], $tableOptions); + } + + /** + * @inheritdoc + */ + public function down() + { + $this->dropTable(Configs::instance()->menuTable); + } +} diff --git a/migrations/m160312_050000_create_user.php b/migrations/m160312_050000_create_user.php new file mode 100644 index 0000000..42248d9 --- /dev/null +++ b/migrations/m160312_050000_create_user.php @@ -0,0 +1,42 @@ +db->driverName === 'mysql') { + // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci + $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB'; + } + + $userTable = Configs::instance()->userTable; + + // Check if the table exists + if ($this->db->schema->getTableSchema($userTable, true) === null) { + $this->createTable($userTable, [ + 'id' => $this->primaryKey(), + 'username' => $this->string(32)->notNull(), + 'auth_key' => $this->string(32)->notNull(), + 'password_hash' => $this->string()->notNull(), + 'password_reset_token' => $this->string(), + 'email' => $this->string()->notNull(), + 'status' => $this->smallInteger()->notNull()->defaultValue(10), + 'created_at' => $this->integer()->notNull(), + 'updated_at' => $this->integer()->notNull(), + ], $tableOptions); + } + } + + public function down() + { + $userTable = Configs::instance()->userTable; + if ($this->db->schema->getTableSchema($userTable, true) !== null) { + $this->dropTable($userTable); + } + } +} diff --git a/migrations/schema-mssql.sql b/migrations/schema-mssql.sql new file mode 100644 index 0000000..5017958 --- /dev/null +++ b/migrations/schema-mssql.sql @@ -0,0 +1,33 @@ +/** + * Database schema required by yii2-admin. + * + * @author Misbahul D Munir + * @since 2.5 + */ + +drop table if exists [menu]; +drop table if exists [user]; + +create table [menu] +( + [id] int IDENTITY PRIMARY KEY, + [name] varchar(128), + [parent] int(11), + [route] varchar(256), + [order] int(11), + [data] text, + foreign key (parent) references [menu]([id]) ON DELETE SET NULL ON UPDATE CASCADE +); + +create table [user] +( + [id] int IDENTITY PRIMARY KEY, + [username] varchar(32) NOT NULL, + [auth_key] varchar(32) NOT NULL, + [password_hash] varchar(256) NOT NULL, + [password_reset_token] varchar(256), + [email] varchar(256) NOT NULL, + [status] integer not null default 10, + [created_at] integer not null, + [updated_at] integer not null +); diff --git a/migrations/schema-mysql.sql b/migrations/schema-mysql.sql new file mode 100644 index 0000000..d8a2dd6 --- /dev/null +++ b/migrations/schema-mysql.sql @@ -0,0 +1,33 @@ +/** + * Database schema required by yii2-admin. + * + * @author Misbahul D Munir + * @since 2.5 + */ + +drop table if exists `menu`; +drop table if exists `user` cascade; + +create table `menu` +( + `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, + `name` varchar(128), + `parent` int(11), + `route` varchar(256), + `order` int(11), + `data` blob, + foreign key (`parent`) references `menu`(`id`) ON DELETE SET NULL ON UPDATE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +create table `user` +( + `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, + `username` varchar(32) NOT NULL, + `auth_key` varchar(32) NOT NULL, + `password_hash` varchar(256) NOT NULL, + `password_reset_token` varchar(256), + `email` varchar(256) NOT NULL, + `status` integer not null default 10, + `created_at` integer not null, + `updated_at` integer not null +)ENGINE=InnoDB DEFAULT CHARSET=utf8; diff --git a/migrations/schema-oci.sql b/migrations/schema-oci.sql new file mode 100644 index 0000000..3072ed7 --- /dev/null +++ b/migrations/schema-oci.sql @@ -0,0 +1,33 @@ +/** + * Database schema required by yii2-admin. + * + * @author Misbahul D Munir + * @since 2.5 + */ + +drop table if exists "menu"; +drop table if exists "user"; + +create table "menu" +( + "id" NUMBER(10) NOT NULL PRIMARY KEY, + "name" varchar(128), + "parent" number(10), + "route" varchar(256), + "order" number(10), + "data" BYTEA, + foreign key (parent) references "menu"("id") ON DELETE SET NULL ON UPDATE CASCADE +); + +create table "user" +( + "id" NUMBER(10) NOT NULL PRIMARY KEY, + "username" varchar(32) NOT NULL, + "auth_key" varchar(32) NOT NULL, + "password_hash" varchar(256) NOT NULL, + "password_reset_token" varchar(256), + "email" varchar(256) NOT NULL, + "status" integer not null default 10, + "created_at" number(10) not null, + "updated_at" number(10) not null +); diff --git a/migrations/schema-pgsql.sql b/migrations/schema-pgsql.sql new file mode 100644 index 0000000..334a932 --- /dev/null +++ b/migrations/schema-pgsql.sql @@ -0,0 +1,33 @@ +/** + * Database schema required by yii2-admin. + * + * @author Misbahul D Munir + * @since 2.5 + */ + +drop table if exists "menu"; +drop table if exists "user"; + +create table "menu" +( + "id" serial NOT NULL PRIMARY KEY, + "name" varchar(128), + "parent" integer, + "route" varchar(256), + "order" integer, + "data" bytea, + foreign key ("parent") references "menu"("id") ON DELETE SET NULL ON UPDATE CASCADE +); + +create table "user" +( + "id" serial NOT NULL PRIMARY KEY, + "username" varchar(32) NOT NULL, + "auth_key" varchar(32) NOT NULL, + "password_hash" varchar(256) NOT NULL, + "password_reset_token" varchar(256), + "email" varchar(256) NOT NULL, + "status" integer not null default 10, + "created_at" integer not null, + "updated_at" integer not null +); diff --git a/migrations/schema-sqlite.sql b/migrations/schema-sqlite.sql new file mode 100644 index 0000000..771d442 --- /dev/null +++ b/migrations/schema-sqlite.sql @@ -0,0 +1,33 @@ +/** + * Database schema required by yii2-admin. + * + * @author Misbahul D Munir + * @since 2.5 + */ + +drop table if exists "menu"; +drop table if exists "user"; + +create table "menu" +( + "id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, + "name" varchar(128), + "parent" int(11), + "route" varchar(256), + "order" int(11), + "data" LONGBLOB, + foreign key ("parent") references "menu"("id") ON DELETE SET NULL ON UPDATE CASCADE +); + +create table "user" +( + "id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, + "username" varchar(32) NOT NULL, + "auth_key" varchar(32) NOT NULL, + "password_hash" varchar(256) NOT NULL, + "password_reset_token" varchar(256), + "email" varchar(256) NOT NULL, + "status" integer not null default 10, + "created_at" integer not null, + "updated_at" integer not null +);