turanct/migraine

资助包维护!
turanct

0.1.0 2021-07-25 12:23 UTC

This package is auto-updated.

Last update: 2024-08-26 03:21:05 UTC


README

Build Status PHPUnit tests Linters

为您的项目提供数据库迁移的一种简单方法

免责声明

自行承担使用此包的风险。

目标

  • 将迁移编写为简单的SQL语句
  • 框架无关
  • 在多个数据库上运行迁移
  • 在不同的数据库组上运行不同的迁移
  • 允许数据库的播种
  • 在SQL中保留日志
  • 具有撤销迁移的能力

用法

使用Composer安装

composer require "turanct/migraine"

提供配置文件

在此示例中,我们有两个组,main是主数据库,shards是一组分片数据库。在生产的mainshards之间的区别在于shards包含多个数据库,这些数据库都需要看起来相同。这意味着如果我们要进行迁移,我们将在所有这些数据库上运行它。

migrations.json

{
    "directory": "migrations",
    "groups": {
        "main": {
            "connection": "mysql:host=127.0.0.1;port=3306;dbname=main",
            "user": "user",
            "password": "password"
        },
        "shards": {
            "user": "user",
            "password": "password",
            "shard1": {
                "connection": "mysql:host=127.0.0.1;port=3306;dbname=shard1"
            },
            "shard2": {
                "connection": "mysql:host=127.0.0.1;port=3306;dbname=shard2"
            },
            "shard3": {
                "connection": "mysql:host=127.0.0.1;port=3306;dbname=shard3"
            },
            "shard4": {
                "connection": "mysql:host=127.0.0.1;port=3306;dbname=shard4"
            }
        }
    }
}

默认情况下,migraine将迁移记录在您的工作目录中的logs.json中,但这是可配置的

在不同文件中记录日志

migrations.json

{
    "directory": "migrations",
    "logs": {
        "type": "json",
        "file": "alternative-file.json"
    },
    "groups": {
        ...
    }
}

在SQL中记录日志

migrations.json

{
    "directory": "migrations",
    "logs": {
        "type": "sql",
        "connection": "mysql:host=127.0.0.1;port=3306;dbname=main",
        "user": "user",
        "password": "password",
        "table": "logs"
    },
    "groups": {
        ...
    }
}

准备迁移目录

mkdir migrations
mkdir migrations/main
mkdir migrations/shards

添加一些迁移

您可以使用new命令创建一个新的迁移

vendor/bin/migraine new main "create users tabel"

它将创建此文件:migrations/main/20200426195623000-create-users-table.sql

我们将填写我们想要运行的迁移

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  `lastupdate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

对于shards

vendor/bin/migraine new shards "create data tabel"

将创建此文件:migrations/shards/20200426195959000-create-data-table.sql

我们将填写我们想要运行的迁移

CREATE TABLE IF NOT EXISTS `data` (
  `id` int(11) NOT NULL,
  `userId` int(11) NOT NULL,
  `data` text,
  `lastupdate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

运行迁移

这就是您如何运行所有迁移。我们将自动进行迁移的干运行

vendor/bin/migraine migrate

如果您想要提交您刚刚进行了干运行的迁移,请提交

vendor/bin/migraine migrate --commit

如果您只想迁移给定的组,请指定它

vendor/bin/migraine migrate --group shards --commit

如果您只想运行特定的迁移,请指定它

vendor/bin/migraine migrate --migration 20200426195959000-create-data-table.sql --commit

如果您想跳过迁移(例如,因为您知道它已经手动完成)

vendor/bin/migraine skip --migration 20200426195959000-create-data-table.sql --commit

小心使用跳过功能。它将跳过的记录写入日志文件,并且将永远不会再次运行此迁移,就像迁移实际上已经执行一样。

播种

您可以通过在组的目录中添加一个/seeds目录来创建种子。在那个目录中,您可以添加与任何其他迁移相同的文件。

mkdir main/seeds

touch main/seeds/20200426195623000-seed-users.sql

它将创建此文件:migrations/main/seeds/20200426195623000-seed-users.sql

填写您想要运行的迁移

INSERT INTO `users` (`id`, `name`, `email`)
VALUES ('1', 'admin', 'admin@example.com');

如果您想要应用特定的种子,请指定它

vendor/bin/migraine seed --seed seeds/20200426195623000-seed-users

贡献

请参阅CONTRIBUTINGCODE_OF_CONDUCT以获取详细信息。

安全性

如果您发现任何与安全相关的问题,请通过电子邮件spinnewebber_toon@hotmail.com发送,而不是使用问题跟踪器。

许可

MIT许可证(MIT)。有关更多信息,请参阅许可文件