您现在的位置是:首页 > 文章详情

phinx数据库脚本迁移工具

日期:2018-05-11点击:410

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

原文链接:https://yq.aliyun.com/articles/592194
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章