总结
数据库迁移类似于数据库的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:tiny