phinx数据库脚本迁移工具

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
{
    /**
     * 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()
    {
       //添加qaSource字段(qa来源)
        $wbrqa = $this->table("wbrqa");
        if (!$wbrqa->hasColumn('qaSource')) {
            $wbrqa->addColumn("qaSource", "string", ['limit' => 30, 'null' => true, 'default' => '', 'comment' => 'qa来源'])->update();
        }
    }
        //更改列属性[wbrqa: qid、aid更改字段属性为varchar 用户于存储mongoDb: _id、parentId]
        $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

猜你喜欢

转载自blog.csdn.net/weixin_39690767/article/details/80267521
今日推荐