phinx数据库脚本迁移工具

简介: phinx数据库脚本迁移工具 Phinx 可以使用 Composer 进行安装,Composer是一个PHP依赖管理工具。更多信息请访问 Composer 官网。 Phinx 至少需要PHP 5.

phinx数据库脚本迁移工具

Phinx 可以使用 Composer 进行安装,Composer是一个PHP依赖管理工具。更多信息请访问 Composer 官网。

Phinx 至少需要PHP 5.4 或更新的版本

第一步:安装

composer require robmorgan/phinx
AI 代码解读

第二步:初始化

安装后,Phinx 现在可以在你的项目中执行初始化

php vendor/robmorgan/phinx/bin/phinx init
AI 代码解读

第三步:配置文件

phinx.yml
AI 代码解读

第四步:创建迁移 文件名驼峰命名

php vendor/robmorgan/phinx/bin/phinx create MyNewMigration
AI 代码解读

这里写图片描述

这将创建一个新的迁移脚本,格式是 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']);
}
AI 代码解读

第五步:执行脚本

php vendor/robmorgan/phinx/bin/phinx migrate -e localhost
AI 代码解读

注意点:


> php vendor/robmorgan/phinx/bin/phinx migrate -e *localhost*
AI 代码解读

此处的localhost为你本地的环境,也可以是线上环境,但是在使用之前,必须配好环境。
环境配置在下一文章详细说明。
链接地址:https://blog.csdn.net/weixin_39690767/article/details/80267801

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

目录
打赏
0
0
0
0
101
分享
相关文章
会议室管理系统源码(含数据库脚本)
会议室管理系统源码(含数据库脚本)
66 0
实现MySQL数据库的定时自动备份脚本。
拿走,不谢,这个脚本配方(指引)保证你的数据库数据像蛋糕店一样地天天更新,还能确保老旧的蛋糕(数据)不会堆积满仓库。这下可好,数据安全有保障,数据库管理员也能轻松一点,偶尔闲下来的时候,煮杯咖啡,看个剧岂不美哉?别忘了偶尔检查一下你的自动备份是否正常工作,以防万一蛋糕机器出了点小差错。
107 20
jsp CRM客户管理系统(含数据库脚本以及文档)
jsp CRM客户管理系统(含数据库脚本以及文档)
81 10
Java汽车租赁系统源码(含数据库脚本)
Java汽车租赁系统源码(含数据库脚本)
68 4
定期备份数据库:基于 Shell 脚本的自动化方案
本篇文章分享一个简单的 Shell 脚本,用于定期备份 MySQL 数据库,并自动将备份传输到远程服务器,帮助防止数据丢失。
python脚本:连接数据库,检查直播流是否可用
【10月更文挑战第13天】本脚本使用 `mysql-connector-python` 连接MySQL数据库,检查 `live_streams` 表中每个直播流URL的可用性。通过 `requests` 库发送HTTP请求,输出每个URL的检查结果。需安装 `mysql-connector-python` 和 `requests` 库,并配置数据库连接参数。
184 68
MySQL数据库中给表添加字段并设置备注的脚本编写
通过上述步骤,你可以在MySQL数据库中给表成功添加新字段并为其设置备注。这样的操作对于保持数据库结构的清晰和最新非常重要,同时也帮助团队成员理解数据模型的变化和字段的具体含义。在实际操作中,记得调整脚本以适应具体的数据库和表名称,以及字段的详细规范。
276 8
MySQL数据库一键安装脚本,适合任何版本
MySQL数据库一键安装脚本,适合任何版本
446 2

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等

登录插画

登录以查看您的控制台资源

管理云资源
状态一览
快捷访问