1.选型

  • 1.1 composer require topthink/think-migration:1.1.1        (tp5.0只支持1.x版本的迁移)

2.使用

  • 2.1 php think migrate:create CreateUserTable    ———-create后面是要创建的表名 采用大驼峰法,完成之后会在根目录生成一个database文件夹,其中的migrations是你生成的文件
  • 2.2  删除change方法!!! up:在migrate:run时执行(前提是文件中不存在change方法)down:在migrate:rollback时执行(前提是文件中不存在change方法)change:migrate:run 和migrate:rollback时执行 (如果存在该方法 则不会去执行up 与down)
  • 2.3如下图所示
  • 2.4 完成之后执行命令 php think migrate:run
  • 2.5 迁移错误可以通过 php think migrate:rollback  -t 回滚到制定的版本,回滚的-t这个t写id,id可以通过php think migrate:status查到

php think migrate:rollback //回滚最后一次的版本
php think migrate:rollback -t //版本 id 回滚到这个版本 id 的时间线,和 git 回滚操作相似(不包含这个版本 id )
php think migrate:rollback -t 0 //回滚所有的版本
php think migrate:rollback -t 0 -f //如果断点阻塞了回滚,强制回滚到最初

  • 2.6 php think migrate:status 查看所有的迁移状态 这个命令可以看到所有版本的id

  • 2.7 breakpoint 命令用来设置断点,对回滚进行限制,只能回滚到断点之后的版本

php think migrate:breakpoint //将断点设在最新的迁移版本上
php think migrate:breakpoint -t //版本id 将断点打到版本id的迁移版本上
php think migrate:breakpoint -r //移除所有断点

3.seed

  • 3.1 mirgrate 只能生成数据表结构 seed可以填充数据也可以用faker插件来生成数据
  • 3.2 php think seed:create UserSeed
  • 3.3 对应database里文件夹seed会生成文件,文件内写法如下

  • php think seed:run 执行命令完成之后就会生成数据