chxj1992/php-database-migration

rake和MyBatis SQL迁移工具

v3.6.10 2017-09-22 07:56 UTC

README

Build Status Packagist Version

SensioLabsInsight

这是一个基于Symfony Console的独立PHP工具,灵感来源于Rails数据库迁移工具和MyBatis。它合并了两者的功能,并设计得尽可能灵活。

使用方法

$ ./bin/migrate
Console Tool

Usage:
  command [options] [arguments]

Options:
  -h, --help            Display this help message
  -q, --quiet           Do not output any message
  -V, --version         Display this application version
      --ansi            Force ANSI output
      --no-ansi         Disable ANSI output
  -n, --no-interaction  Do not ask any interactive question
  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:
  help            Displays help for a command
  list            Lists commands
 migrate
  migrate:addenv  Initialise an environment to work with php db migrate
  migrate:create  Create a SQL migration
  migrate:down    Rollback all waiting migration down to [to] option if precised
  migrate:init    Create the changelog table on your environment database
  migrate:status  Display the current status of the specified environment
  migrate:up      Execute all waiting migration up to [to] option if precised

将安装包添加到你的项目中

只需将其添加到composer.json中(不要忘记指定你的bin目录)注意,所有迁移命令必须在根目录下执行,例如bin/migrate migrate:command...

{
    "name": "jdoe/testproject",
    "authors": [
        {
            "name": "Jhon DOE",
            "email": "jdoe@gmail.com"
        }
    ],
    "require": {
        "php-database-migration/php-database-migration" :"3.6.*"
    },
    "config": {
        "bin-dir": "bin"
    }
}

添加环境

在玩SQL迁移之前,首先要添加一个环境,让我们添加开发环境。

$ ./bin/migrate migrate:addenv

你将需要回答一系列关于你的环境的问题,然后配置文件将被保存在./.php-database-migration/environments/[env].yml中。

初始化

添加环境后,你必须初始化它。这会验证数据库连接是否正常,并创建一个新的数据库表以跟踪当前的数据库更改。

$ ./bin/migrate migrate:init [env]

创建迁移

是时候创建我们的第一个迁移文件了。

$ ./bin/migrate migrate:create

迁移文件是这样的

-- // add table users
-- Migration SQL that makes the change goes here.
create table users (id integer, name text);
-- @UNDO
-- SQL to undo the change goes here.
drop table users;

列出迁移

查看所有可用的迁移及其状态。

$ ./bin/migrate migrate:status [env]
+----------------+---------+------------------+--------------------+
| id             | version | applied at       | description        |
+----------------+---------+------------------+--------------------+
| 14679010838251 |         |                  | create table users |
+----------------+---------+------------------+--------------------+

上和下

你现在可以应用所有挂起的迁移。如果你决定回滚迁移,最后一个迁移将被单独回滚以防止错误。在运行实际的SQL脚本之前,你将被要求确认数据库回滚。

$ ./bin/migrate migrate:up [env]

你可以标记迁移为已应用而不执行SQL(例如,如果你从另一个迁移系统切换过来)

$ ./bin/migrate migrate:up [env] --changelog-only

出于开发目的,也可以单独应用一个迁移而不关心其他迁移

$ ./bin/migrate migrate:up [env] --only=[migrationid]

或迁移到特定的迁移(它将运行包括指定迁移在内的所有迁移)

$ ./bin/migrate migrate:up [env] --to=[migrationid]

同样,对于下

$ ./bin/migrate migrate:down [env] --only=[migrationid]

$ ./bin/migrate migrate:down [env] --to=[migrationid]