# 數據遷移
>[success]在這里以`ThinkPHP5.0`為例,在ThinkPHP5中提供了數據庫遷移工具`migration`。
>
## 1. 什么是`migration`
>一種數據庫的版本控制,讓團隊在修改數據庫結構的同時,保持彼此的進度一致。幫你更簡單的管理數據庫。基于原生 thinkPHP 5.0 命令行工具,融入了 Phinx 的數據庫遷移
>
>[danger]特別團隊協作開發時,這一功能非常方便。
>
## 2. `migration`的安裝
>使用`Composer` 進行安裝
>
~~~
composer require topthink/think-migration
~~~
>[danger]在安裝時一定要注意版本,如果您選擇的是ThinkPHP5.0那要么安裝的`migration`版本也應為1.*。如果您使的是ThinkPHP5.1,可以直接使用上述命令安裝最新版本即可。
>
1.* 版本的安裝
~~~
composer require topthink/think-migration 1.*
~~~
## 3. `migration`的使用
>安裝好后,即可以通過 `ThinkPHP`的命令行進行查看其相關的命令
>
~~~
php think
~~~
~~~
Available commands:
build Build Application Dirs
clear Clear runtime file
help Displays help for a command
list Lists commands
make
make:controller Create a new resource controller class
make:model Create a new model class
migrate
migrate:breakpoint Manage breakpoints
migrate:create Create a new migration
migrate:rollback Rollback the last or to a specific migration
migrate:run Migrate the database
migrate:status Show migration status
optimize
optimize:autoload Optimizes PSR0 and PSR4 packages to be loaded with classmaps too, good for production.
optimize:config Build config and common file cache.
optimize:route Build route cache.
optimize:schema Build database schema cache.
seed
seed:create Create a new database seeder
seed:run Run database seeders
~~~
>[dagner]這時命令中就多數了數據遷移相關的命令
>
>[danger]### 使用前,要先配置數據庫(為空數據庫即可)
### 1. 創建遷移類
>創建遷移類,首字母必須為大寫
>
~~~
php think migrate:create Users
~~~
>[danger]`Users` 為表名稱(不含前輟)
>
>第一次執行的時候會確認是否創建遷移目錄;
>確認之后;
>這時會在項目根目錄下執行的該命令;
>那么在項目跟目錄下會看到database/migrations/20170711153001_Users.php;
>默認有一個change方法
遷移類實例
~~~
<?php
use Phinx\Migration\AbstractMigration;
class Users extends AbstractMigration
{
/**
* Change Method.
*/
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();
}
/**
* Migrate Up.
*/
public function up()
{
}
/**
* Migrate Down.
*/
public function down()
{
}
}
~~~
>[danger]注意:在設置數據表字段時會自動添加名為`id`的自增主鍵,如果想自定義自增主鍵的話,需要如下操作。
>
~~~php
<?php
public function change()
{
$table = $this->table('user',array('engine'=>'MyISAM'));
$table->setId('user_id')->setPrimaryKey('user_id')
->addColumn('username', 'string',array('limit' => 15,'default'=>'','comment'=>'用戶名'))
->create();
}
?>
~~~
>[danger]注意:若不需要自增主鍵的話,請如下操作
~~~
<?php
public function change()
{
$table = $this->table('user',array('engine'=>'MyISAM'));
$table->setId(false)->setPrimaryKey('user_id')
->addColumn('username', 'string',array('limit' => 15,'default'=>'','comment'=>'用戶名'))
->create();
}
?>
~~~
>[danger] #### 除了使用`change`方法操作之外,還可以使用`up`和`down`方法來創建。
~~~
public function up ()
{
$table = $this->table('user',array('engine'=>'MyISAM'));
$table->setId('user_id') //關閉自動設置主鍵
->setPrimaryKey('user_id') //設置主鍵
->addColumn('username', 'string',array('limit' => 15,'default'=>'','comment'=>'用戶名'))
->create();
}
public function down ()
{
$this->dropTable('user');
~~~
### 2. 常用命令
#### 1.執行數據遷移( 創建數據表)
~~~
php think migrate:run
~~~
#### 2. 執行回滾(刪除重新操作)
>[danger]調整表結構時需要執行,執行時會清空表中現在的數據
~~~
php think migrate:rollback
#回滾所有
php think migrate:rollback -t 0
~~~
- 前言
- PHP獲取服務器信息
- PHP中的常用函數-新手必備知識
- 日期時間相關的函數
- 時區設置
- time函數
- strtotime 時間戳
- date函數
- mktime函數
- 聯合使用 date() 和 mktime()
- PHP數據類型相關的函數
- PHP數組相關的函數
- array函數
- 數組的排序
- sort()和rsort()函數
- asort()和arsort()函數
- ksort()和krsort()函數
- array_multisort()函數
- array_reverse函數
- 數組的遍歷
- 數組中新增和刪除元素
- 數組頭部插入和刪除元素
- 數組尾部插入和刪除元素
- 刪除數組中重復的元素
- 刪除數組中指定的元素
- 數組的合并
- 隨機/打亂已知數組
- range函數
- 數組去除重復
- PHP常用功能函數
- URL地址處理函數
- post/get請求
- PHP字符串相關的函數
- PHP文件系統
- PHP正則表達式
- 正則表達式語法規則
- POSIX擴展的正則表達式函數
- 查找字串函數
- 替換字符串函數
- Perl兼容的正則表達式函數
- PHP中類的應用
- 中文編碼
- 關于stdClass
- 變量相關函數
- unset
- PHP數值相關的函數
- 數值取整
- 開發工具與開發環境使用技巧
- sublime
- 常用插件
- Atom
- 常用插件
- 常見問題
- Visual Studio Code
- vscode常用插件
- 編程推薦字體
- MAC下開發常識
- MAC下的常用設置
- MAC下的常用開發工具
- MAC下XAMMP的常見問題
- Apache配置基礎
- PhpStrom
- php中的常見問題
- 文件上傳相關問題
- API接口中常見問題
- 關于緩沖區問題
- PHP中注意事項
- 條件判斷
- PHP文件管理模塊
- 文件管理源碼
- 文件管理的常用函數
- 文件管理中文亂碼處理
- 自定義功能函數
- 文件下載
- PHP常用頭信息定義匯總
- 常見PHP網頁木馬
- 加密算法
- 1. Base58可逆加密
- 2. AES加密/解密
- mysql數據庫操作
- 命令行操作Mysql常用令行-查詢
- 命令行操作Mysql常用令行-操作
- Mysql使用中的技巧
- 在線數據庫管理中常用命令
- sql show命令
- mysql數據庫的備份與恢復
- 二進制日志介紹
- 二進制日志常用命令
- ThinkPHP
- 數據遷移
- 常見問題
- 驗證碼問題
- API接口中的異常處理
- API接口安全
- 解決跨域問題
- 自定義實用功能函數