在thinkphp5中提供了数据迁移工具(think-migration),它是机遇phinx开发(文档地址:http://docs.phinx.org/en/latest/)php
一:配置think-migrationthinkphp
在commond.php 中添加数据库
<?php return [ "think\\migration\\command\\migrate\\Create", "think\\migration\\command\\migrate\\Run", "think\\migration\\command\\migrate\\Rollback", "think\\migration\\command\\migrate\\Status", "think\\migration\\command\\seed\\Create", "think\\migration\\command\\seed\\Run", ]; ?>
注意因为think-migration存放在thinkphp/vendor中 因此在think中须要将vendor加入auoloadthinkphp5
require __DIR__.'/../thinkphp/vendor/autoload.php';
二:命令行运行工具
在命令行输入php think 能够看见ui
migrate:数据库迁移工具 seed:数据库填充工具spa
主要讨论migrate:命令行
migrate:create : 建立一个新的数据迁移类,php think migrate:create <file>,文件名须采用驼峰命名法code
forexample:php think migrate:create ScProductImage 文件会在制定目录下生成一个php文件blog
***********************************************************
migrate:run : 完成数据迁移工做 php think migrate:run
***********************************************************
migrate:status:查看migrate工做状态 php think migrate:status
***********************************************************************************************
migrate:rollback : 回滚数据到指定的一个数据迁移版本 php think migrate:rollback -t <timeline>
<timeline> 就是咱们上图上面红框表示的值
三:migrate文件编写
在migrate中有三个方法
up:在migrate:run时执行(前提是文件中不存在change方法)
down:在migrate:rollback时执行(前提是文件中不存在change方法)
change:migrate:run 和migrate:rollback时执行 (若是存在该方法 则不会去执行up 与down)