phinx数据库脚本迁移工具
![]()
Phinx 可以使用 Composer 进行安装,Composer是一个PHP依赖管理工具。更多信息请访问 Composer 官网。
Phinx 至少需要PHP 5.4 或更新的版本
第一步:安装
composer require robmorgan/phinx
第二步:初始化
安装后,Phinx 现在可以在你的项目中执行初始化
php vendor/robmorgan/phinx/bin/phinx init
第三步:配置文件
phinx.yml
第四步:创建迁移 文件名驼峰命名
php vendor/robmorgan/phinx/bin/phinx create MyNewMigration
![这里写图片描述]()
这将创建一个新的迁移脚本,格式是 YYYYMMDDHHMMSS_my_new_migration.php ,前14个字符是当前的timestamp,精确到秒。
![这里写图片描述]()
如果你指定了多个脚本路径,将会提示你选择哪一个。
Phinx 自动创建的迁移脚本框架有一个方法:
<?php
use Phinx\Migration\AbstractMigration;
class MyNewMigration extends AbstractMigration
{
public function change()
{
$wbrqa = $this->table("wbrqa");
if (!$wbrqa->hasColumn('qaSource')) {
$wbrqa->addColumn("qaSource", "string", ['limit' => 30, 'null' => true, 'default' => '', 'comment' => 'qa来源'])->update();
}
}
$wbrqa = $this->table("wbrqa");
$wbrqa->changeColumn('qId', 'string', ['limit' => 255, 'null' => true])->save();
$wbrqa->changeColumn('aId', 'string', ['limit' => 255, 'null' => true])->save();
$wbrqa->changeColumn('qUserId', 'string', ['limit' => 255, 'null' => true])->save();
$wbrqa = $this->table("wbrqa");
if (!$wbrqa->hasColumn('isDeleted')) {
$wbrqa->addColumn("isDeleted", "integer", ['limit' => 2, 'null' => true, 'default' => '0', 'comment' => '是否删除'])->update();
}
$wbrqa->hasIndex(['isDeleted', 'index_wbrqa_isDeleted']);
}
第五步:执行脚本
php vendor/robmorgan/phinx/bin/phinx migrate -e localhost
注意点:
> php vendor/robmorgan/phinx/bin/phinx migrate -e *localhost*
此处的localhost为你本地的环境,也可以是线上环境,但是在使用之前,必须配好环境。
环境配置在下一文章详细说明。
链接地址:https://blog.csdn.net/weixin_39690767/article/details/80267801
原文地址https://blog.csdn.net/weixin_39690767/article/details/80267521