原始sql

  1. CREATE TABLE `user` (
  2. `id` INT NOT NULL AUTO_INCREMENT,
  3. `username` VARCHAR (30) NOT NULL COMMENT '用户名',
  4. `password` VARCHAR (60) NOT NULL COMMENT '密码',
  5. `email` VARCHAR (50) NOT NULL COMMENT 'email',
  6. `status` TINYINT (1) NOT NULL COMMENT '状态',
  7. `add_time` INT NOT NULL COMMENT '添加时间',
  8. `update_time` INT NOT NULL COMMENT '更新时间',
  9. PRIMARY KEY (`id`)
  10. ) ENGINE = INNODB DEFAULT CHARSET = UTF8 COMMENT '用户表' ;
  11. CREATE TABLE `user_token` (
  12. `id` INT NOT NULL AUTO_INCREMENT,
  13. `token` VARCHAR (60) NOT NULL COMMENT 'refresh token',
  14. `version` CHAR(10) NOT NULL COMMENT '版本',
  15. `status` TINYINT (1) NOT NULL DEFAULT 0 COMMENT '状态',
  16. `pub_time` INT NOT NULL COMMENT '发布时间',
  17. `expire_time` INT NOT NULL COMMENT '过期时间',
  18. PRIMARY KEY (`id`)
  19. ) ENGINE = INNODB DEFAULT CHARSET = UTF8 COMMENT 'refresh token' ;
  20. CREATE TABLE `task` (
  21. `id` INT NOT NULL AUTO_INCREMENT,
  22. `title` VARCHAR (50) NOT NULL COMMENT '任务标题',
  23. `content` TEXT NOT NULL COMMENT '任务详情',
  24. `job_id` INT NOT NULL DEFAULT 0 COMMENT '任务ID',
  25. `type` TINYINT (1) NOT NULL DEFAULT 0 COMMENT '类型(正常与延迟)',
  26. `status` TINYINT (1) NOT NULL DEFAULT 0 COMMENT '状态',
  27. `add_time` INT NOT NULL COMMENT '添加时间',
  28. `update_time` INT NOT NULL COMMENT '修改时间',
  29. `expect_time` INT NOT NULL COMMENT '预期执行时间',
  30. `exec_time` INT NOT NULL COMMENT '实际执行时间',
  31. `exec_status` TINYINT (1) NOT NULL COMMENT '执行结果',
  32. `exec_count` TINYINT (4) NOT NULL COMMENT '执行次数',
  33. `is_del` TINYINT (1) NOT NULL DEFAULT 0 COMMENT '是否删除',
  34. PRIMARY KEY (`id`)
  35. ) ENGINE = INNODB DEFAULT CHARSET = UTF8 COMMENT '用户任务表' ;

封装的 Migration

新建base目录,并新建 base/Migration.php 文件。

  1. <?php
  2. namespace app\base;
  3. /**
  4. * 封装公共的 Migration
  5. * @author vogin
  6. */
  7. class Migration extends \yii\db\Migration
  8. {
  9. /**
  10. * 创建引擎innodb的表单
  11. *
  12. * @param string $table
  13. * 表名
  14. * @param array $columns
  15. * 字段
  16. * @param string $tableComment
  17. * 表名注释
  18. */
  19. public function createInnoDBTable ($table, $columns, $tableComment = '')
  20. {
  21. // 表单的内容
  22. $tableOptions = 'ENGINE=InnoDB DEFAULT CHARSET=utf8';
  23. $this->createTable($table, $columns, $tableOptions);
  24. // 添加表注释
  25. if($tableComment)
  26. {
  27. $this->addCommentOnTable($table, $tableComment);
  28. }
  29. }
  30. }

创建三个sql的Migration

创建与改写 user 的 migration

选择 yes, 会在根目录下创建 migrations 文件夹,并在其下新增 user 的 migration 文件(名称与时间有关系,每次创建都不一样)

yii migrate/create create_user_table

  1. <?php
  2. use app\base\Migration;
  3. /**
  4. * Handles the creation of table `user`.
  5. */
  6. class m201128_014542_create_user_table extends Migration
  7. {
  8. /**
  9. * {@inheritdoc}
  10. */
  11. public function safeUp ()
  12. {
  13. // CREATE TABLE `user` (
  14. // `id` INT NOT NULL AUTO_INCREMENT,
  15. // `username` VARCHAR (30) NOT NULL COMMENT '用户名',
  16. // `password` VARCHAR (60) NOT NULL COMMENT '密码',
  17. // `email` VARCHAR (50) NOT NULL COMMENT 'email',
  18. // `status` TINYINT (1) NOT NULL DEFAULT 0 COMMENT '状态',
  19. // `add_time` INT NOT NULL COMMENT '添加时间',
  20. // `update_time` INT NOT NULL COMMENT '更新时间',
  21. // PRIMARY KEY (`id`)
  22. // ) ENGINE = INNODB DEFAULT CHARSET = UTF8 COMMENT '用户表';
  23. $this->createInnoDBTable('{{%user}}', [
  24. 'id' => $this->primaryKey(),
  25. 'username' => $this->string(30)->notNull()->comment('用户名'),
  26. 'password' => $this->string(60)->notNull()->comment('密码'),
  27. 'email' => $this->string(50)->notNull()->comment('email'),
  28. 'status' => $this->tinyInteger(1)->notNull()->defaultValue(0)->comment('状态'),
  29. 'add_time' => $this->integer()->notNull()->comment('添加时间'),
  30. 'update_time' => $this->integer()->notNull()->comment('更新时间')
  31. ], '用户表');
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function safeDown ()
  37. {
  38. $this->dropTable('{{%user}}');
  39. }
  40. }

创建与改写 user_token 的 migration

yii migrate/create create_user_token_table

  1. <?php
  2. use app\base\Migration;
  3. /**
  4. * Handles the creation of table `user_token`.
  5. */
  6. class m201128_022322_create_user_token_table extends Migration
  7. {
  8. /**
  9. * {@inheritdoc}
  10. */
  11. public function safeUp ()
  12. {
  13. $this->createInnoDBTable('{{%user_token}}', [
  14. 'id' => $this->primaryKey(),
  15. 'token' => $this->string(60)->notNull()->comment('refresh token'),
  16. 'version' => $this->char(10)->notNull()->comment('版本标识'),
  17. 'status' => $this->tinyInteger(1)->notNull()->defaultValue(0)->comment('状态'),
  18. 'pub_time' => $this->integer()->notNull()->comment('发布时间'),
  19. 'expire_time' => $this->integer()->notNull()->comment('过期时间')
  20. ], '用户token表');
  21. }
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function safeDown ()
  26. {
  27. $this->dropTable('{{%user_token}}');
  28. }
  29. }

创建与改写 task 的 migration

yii migrate/create create_task_table

  1. <?php
  2. use app\base\Migration;
  3. /**
  4. * Handles the creation of table `task`.
  5. */
  6. class m201128_022828_create_task_table extends Migration
  7. {
  8. //CREATE TABLE `task` (
  9. //`id` INT NOT NULL AUTO_INCREMENT,
  10. //`title` VARCHAR (50) NOT NULL COMMENT '任务标题',
  11. //`content` TEXT NOT NULL COMMENT '任务详情',
  12. //`job_id` INT NOT NULL DEFAULT 0 COMMENT '任务ID',
  13. //`type` TINYINT (1) NOT NULL DEFAULT 0 COMMENT '类型(正常与延迟)',
  14. //`status` TINYINT (1) NOT NULL DEFAULT 0 COMMENT '状态',
  15. //`add_time` INT NOT NULL COMMENT '添加时间',
  16. //`update_time` INT NOT NULL COMMENT '修改时间',
  17. //`expect_time` INT NOT NULL COMMENT '预期执行时间',
  18. //`exec_time` INT NOT NULL COMMENT '实际执行时间',
  19. //`exec_status` TINYINT (1) NOT NULL COMMENT '执行结果',
  20. //`exec_count` TINYINT (4) NOT NULL COMMENT '执行次数',
  21. //`is_del` TINYINT (1) NOT NULL DEFAULT 0 COMMENT '是否删除',
  22. //PRIMARY KEY (`id`)
  23. //) ENGINE = INNODB DEFAULT CHARSET = UTF8 COMMENT '用户任务表' ;
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function safeUp ()
  28. {
  29. $this->createInnoDBTable('{{%task}}', [
  30. 'id' => $this->primaryKey(),
  31. 'title' => $this->string(50)->notNull()->comment('任务标题'),
  32. 'content' => $this->text()->notNull()->comment('任务详情'),
  33. 'job_id' => $this->integer()->notNull()->defaultValue(0)->comment('任务ID'),
  34. 'type' => $this->tinyInteger(1)->notNull()->defaultValue(0)->comment('类型'),
  35. 'status' => $this->tinyInteger(1)->notNull()->defaultValue(0)->comment('状态'),
  36. 'add_time' => $this->integer()->notNull()->comment('添加时间'),
  37. 'update_time' => $this->integer()->notNull()->comment('修改时间'),
  38. 'expect_time' => $this->integer()->notNull()->comment('预期执行时间'),
  39. 'exec_time' => $this->integer()->notNull()->comment('实际执行时间'),
  40. 'exec_status' => $this->tinyInteger(1)->notNull()->defaultValue(0)->comment('执行结果'),
  41. 'exec_count' => $this->tinyInteger(1)->notNull()->defaultValue(0)->comment('执行次数'),
  42. 'is_del' => $this->tinyInteger(1)->notNull()->defaultValue(0)->comment('是否删除'),
  43. ], '用户任务表');
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function safeDown ()
  49. {
  50. $this->dropTable('{{%task}}');
  51. }
  52. }

执行所有migration

  1. yii migrate

执行完成之后,就会在数据库中建立三个表 user, user_token, task。