在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)