## 數據庫遷移工具
使用數據庫遷移工具可以將數據庫結構和數據很容易的在不同的數據庫之間管理遷移。
在以前,為了實現“程序安裝”,你可能會導出一份sql文件,安裝時,用程序解析這個sql文件,執行里面的語句,這樣做有諸多的局限性,但現在使用數據庫遷移工具,你可使用一個強大的類庫API來創建數據庫結構和記錄,并且可以容易的安裝到Mysql,sqlite,sqlserver等數據庫。
原項目手冊:[phinx](https://book.cakephp.org/phinx/0/en/index.html)
使用范例:
> 使用之前你應當正確的連接到數據庫,不論是mysql,sqlite,sqlserver
### 安裝
```
composer require topthink/think-migration
```
### 創建遷移工具文件
```
//執行命令,創建一個操作文件,一定要用大駝峰寫法,如下
php think migrate:create AnyClassNameYouWant
//執行完成后,會在項目根目錄多一個database目錄,這里面存放類庫操作文件
//文件名類似/database/migrations/20190615151716_any_class_name_you_want.php
```
### 編輯文件
```
<?php
use think\migration\Migrator;
use think\migration\db\Column;
class AnyClassNameYouWant extends Migrator
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* renameColumn
* addIndex
* addForeignKey
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
// create the table
$table = $this->table('users',array('engine'=>'MyISAM'));
$table->addColumn('username', 'string',array('limit' => 15,'default'=>'','comment'=>'用戶名,登陸使用'))
->addColumn('password', 'string',array('limit' => 32,'default'=>md5('123456'),'comment'=>'用戶密碼'))
->addColumn('login_status', 'boolean',array('limit' => 1,'default'=>0,'comment'=>'登陸狀態'))
->addColumn('login_code', 'string',array('limit' => 32,'default'=>0,'comment'=>'排他性登陸標識'))
->addColumn('last_login_ip', 'integer',array('limit' => 11,'default'=>0,'comment'=>'最后登錄IP'))
->addColumn('last_login_time', 'datetime',array('default'=>0,'comment'=>'最后登錄時間'))
->addColumn('is_delete', 'boolean',array('limit' => 1,'default'=>0,'comment'=>'刪除狀態,1已刪除'))
->addIndex(array('username'), array('unique' => true))
->create();
}
}
```
### 執行遷移工具
```
php think migrate:run
//此時數據庫便創建了prefix_users表.
```
> 數據庫會有一個migrations表,這個是工具使用的表,不要修改
### 例子
~~~
$table = $this->table('followers', ['id' => false, 'primary_key' => ['user_id', 'follower_id']]);
$table->addColumn('user_id', 'integer')
->addColumn('follower_id', 'integer')
->addColumn('created', 'datetime')
->addIndex(['email','username'], ['limit' => ['email' => 5, 'username' => 2]])
->addIndex('user_guid', ['limit' => 6])
->create();
~~~
### 表支持的參數選項
| 選項 | 描述 |
| -- | ------ |
| comment | 給表結構設置文本注釋 |
| row\_format | 設置行記錄模格式|
| engine | 表引擎 *(默認 ``InnoDB``)* |
| collation | 表字符集 *(默認 ``utf8\_general\_ci``)* |
| signed | 是否無符號 `signed` *(默認 ``true``)* |
### 常用列
* biginteger
* binary
* boolean
* date
* datetime
* decimal
* float
* integer
* string
* text
* time
* timestamp
* uuid
### 所有的類型都支持的參數
| Option | Description |
| --- | --- |
| limit | 文本或者整型的長度 |
| length | `limit`別名 |
| default | 默認值 |
| null | 允許 `NULL` 值 (不該給主鍵設置 |
| after | 在哪個字段名后 *(只對MySQL有效)* |
| comment | 給列設置文本注釋 |
### 索引的用法
```
->addIndex(['email','username'], ['limit' => ['email' => 5, 'username' => 2]])
->addIndex('user_guid', ['limit' => 6])
->addIndex('email',['type'=>'fulltext'])
```
如上面例子所示,默認是`普通索引`,mysql可設置生效`復合索引`,mysql可以設置`fulltext`.
## 自動版本升級降級
該項目可以升級和還原,就像git/svn一樣rollback。
如果希望實現自動升級降級,那就把邏輯寫在change方法里,只最終調用`create`和`update`方法,不要調用`save`方法。
`change`方法內僅支持以下操作
* createTable
* renameTable
* addColumn
* renameColumn
* addIndex
* addForeignKey
如果真的有調用其他方法,可以寫到`up`和`down`方法里,這里的邏輯不支持自動還原,up寫升級的邏輯,down寫降級的邏輯。
~~~
public function change()
{
// create the table
$table = $this->table('user_logins');
$table->addColumn('user_id', 'integer')
->addColumn('created', 'datetime')
->create();
}
/**
* Migrate Up.
*/
public function up()
{
}
/**
* Migrate Down.
*/
public function down()
{
}
~~~
- 序言
- 基礎
- 安裝
- 開發規范
- 目錄結構
- 配置
- 架構
- 請求流程
- 架構總覽
- 入口文件
- 多應用模式
- URL訪問
- 容器和依賴注入
- 服務
- 門面
- 中間件
- 事件
- 路由
- 路由定義
- 變量規則
- 路由地址
- 路由參數
- 路由中間件
- 路由分組
- 資源路由
- 注解路由
- 路由綁定
- 域名路由
- MISS路由
- 跨域請求
- URL生成
- 控制器
- 控制器定義
- 基礎控制器
- 空控制器
- 資源控制器
- 控制器中間件
- 請求
- 請求對象
- 請求信息
- 輸入變量
- 請求類型
- HTTP頭信息
- 偽靜態
- 參數綁定
- 請求緩存
- 響應
- 響應輸出
- 響應參數
- 重定向
- 文件下載
- 數據庫
- 連接數據庫
- 分布式數據庫
- 查詢構造器
- 查詢數據
- 添加數據
- 更新數據
- 刪除數據
- 查詢表達式
- 鏈式操作
- where
- table
- alias
- field
- strict
- limit
- page
- order
- group
- having
- join
- union
- distinct
- lock
- cache
- cacheAlways
- comment
- fetchSql
- force
- partition
- failException
- sequence
- replace
- extra
- duplicate
- procedure
- 聚合查詢
- 分頁查詢
- 時間查詢
- 高級查詢
- 視圖查詢
- JSON字段
- 子查詢
- 原生查詢
- 獲取查詢參數
- 查詢事件
- 獲取器
- 事務操作
- 存儲過程
- 數據集
- 數據庫驅動
- 模型
- 定義
- 模型字段
- 新增
- 更新
- 刪除
- 查詢
- 查詢范圍
- JSON字段
- 獲取器
- 修改器
- 搜索器
- 數據集
- 自動時間戳
- 只讀字段
- 軟刪除
- 類型轉換
- 模型輸出
- 模型事件
- 模型關聯
- 一對一關聯
- 一對多關聯
- 遠程一對多
- 遠程一對一
- 多對多關聯
- 多態關聯
- 關聯預載入
- 關聯統計
- 關聯輸出
- 虛擬模型
- 視圖
- 模板變量
- 視圖過濾
- 模板渲染
- 模板引擎
- 視圖驅動
- 錯誤和日志
- 異常處理
- 日志處理
- 調試
- 調試模式
- Trace調試
- SQL調試
- 變量調試
- 遠程調試
- 驗證
- 驗證器
- 驗證規則
- 錯誤信息
- 驗證場景
- 路由驗證
- 內置規則
- 表單令牌
- 注解驗證
- 雜項
- 緩存
- Session
- Cookie
- 多語言
- 上傳
- 命令行
- 啟動內置服務器
- 查看版本
- 自動生成應用目錄
- 創建類庫文件
- 清除緩存文件
- 生成數據表字段緩存
- 生成路由映射緩存
- 輸出路由定義
- 自定義指令
- Debug輸出級別
- 擴展庫
- 數據庫遷移工具
- Workerman
- think助手工具庫
- 驗證碼
- Swoole
- 附錄
- 助手函數
- 升級指導
- 更新日志