天天看點

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

原文位址https://blog.csdn.net/weixin_39690767/article/details/80267521