總結
資料庫遷移類似於資料庫的SVN,可方便用於團隊開發工程中的系統應用資料結構初始化、新建或刪除表、新增或修改表欄位等操作。
Thinkphp提供了think-migration
擴充套件使用者資料庫的遷移和資料填充
資料庫遷移
1. 安裝和建立遷移類
$ composer require topthink/think-migration=2.0.*
$ php think migrate:create Third
表名稱首字母需大寫,如下圖:
系統會自動生成遷移類檔案:
2. 編輯操作方法
use think\migration\Migrator;
use think\migration\db\Column;
use Phinx\Db\Adapter\MysqlAdapter; //如建立MYSQL特有欄位,需匯入該名稱空間
class Test extends Migrator
{
public function change()
{
$table = $this->table('third', ['engine' => 'InnoDB', 'collation' => 'utf8_bin', 'comment' => '測試表']);
$table->addColumn('member_id', 'integer', ['limit' => 10, 'signed' => false, 'default' => '0', 'comment' => 'MYSQL:int'])//unsigned:('signed' => false)
->addColumn('thirdid', 'integer', ['limit' => MysqlAdapter::INT_TINY, 'signed' => false, 'default' => '0', 'comment' => 'MYSQL:tinyint'])//需匯入名稱空間
->addColumn('platform', 'string', ['limit' => 30, 'default' => '', 'comment' => 'MYSQL:varchar'])
->addColumn('fee', 'decimal', ['precision' => 10, 'scale' => 2, 'default' => '0', 'comment' => 'decimal'])//decimal(10,2):('precision' => 10, 'scale' => 2)
->addColumn('money', 'float', ['precision' => 10, 'scale' => 2, 'default' => '0', 'comment' => 'float'])
->addColumn('openid', 'text', ['default' => '', 'comment' => 'text'])
->addColumn('content', 'text', ['limit' => MysqlAdapter::TEXT_LONG, 'default' => '', 'comment' => 'MYSQL:longtext'])
->addColumn('is_show', 'enum', ['values' => ['false', 'ture'], 'default' => 'false', 'comment' => 'enum'])
->addColumn('delete_time', 'integer', ['limit' => 10, 'signed' => false, 'null' => true, 'default' => NULL, 'comment' => 'delete_time'])//null:('null' => true)
->addIndex(['member_id'], ['name' => 'member_id'])
->addIndex(['thirdid', 'member_id'], ['unique' => true, 'name' => 'thirdid'])
->create();
$rows = [ //插入資料
[
'member_id' => 1,
'platform' => 'zhifubao',
],
[
'member_id' => 2,
'platform' => 'weixin',
],
];
$table->insert($rows)->save();
}
}
或使用up
和down
方法,但使用change
方法後,up
和down
方法將被忽略:
Please be aware that when a change method exists, Phinx will automatically ignore the up and down methods
public function up()
{
// add column type DOUBLE
$this->execute("ALTER TABLE third ADD COLUMN much_money DOUBLE(10,2) unsigned NOT NULL DEFAULT '0.00'");
}
public function down()
{
$this->dropTable('third');
}
3. 執行操作
$ php think migrate:run
結果如下:
同時資料庫中會生成third
表和migrations
表,migrations
表用於記錄本次操作的記錄:
4. 更新資料
再次建立migrate
:
$ php think migrate:create ChangeThird
再將需要更新的內容寫到change
方法中
public function change()
{
$table = $this->table('third');
$table->changeColumn('platform', 'string', ['limit' => 255])
->addColumn('password', 'string', array('after' => 'platform','limit' => 32, 'default' => md5('123456'), 'comment' => '使用者密碼'))
->save();
}
再次執行操作:
5. 執行回滾
up
與down
方法提供 migrate:run
和migrate:rollback
功能,可通過以下命令回滾down
中的操作
$ php think migrate:rollback -t third
third
類檔案中的down
方法:
public function down()
{
$this->dropTable('third');
}
結果如下:
third
表被刪除:
資料填充
1. 建立seed
$ php think seed:create Third
如下:
可看到生成的檔案:
2. 編輯seed檔案:
public function run()
{
$data[] = [
'member_id' => 1,
'thirdid' => '1',
'platform' => 'zhansan',
];
$this->table('third')->insert($data)->save();
}
3. 開始進行資料庫填充
$ php think seed:run
結果如下:
可指定一個或多個seed
$ php think seed:run -s Third
$ php think seed:run -s Third -s member
使用假資料生成器
安裝faker
類庫
$ composer require fzaninotto/faker
使用faker
生成資料
public function run()
{
$faker = Faker\Factory::create('zh_CN');//選擇中文庫
$data = [];
for ($i = 0; $i < 100; $i++) {
$data[] = [
'member_id' => $faker->randomDigit,
'thirdid' => $faker->randomDigit,
'platform' => $faker->word,
'openid' => sha1($faker->password),
'content' => $faker->realText(50),
];
}
$this->table('third')->insert($data)->save();
}
結果如下:
參考網址 ThinkPHP5.1、Phinx
本作品採用《CC 協議》,轉載必須註明作者和本文連結