byjg / migration-cli
PHP 用于数据库版本控制的简单库。支持 Sqlite、MySql、Sql Server 和 Postgres。
4.9.1
2024-01-11 16:14 UTC
Requires
- php: >=7.4
- byjg/migration: 4.9.*
- symfony/console: ^5.4|^6.0|^7.0
README
这是一个用于数据库版本控制的简单 PHP 库。目前支持 Sqlite、MySql、Sql Server 和 Postgres。
数据库迁移可以用作
- 命令行界面
- PHP 库,可集成到您的功能测试中
- 无论您的编程语言或框架如何,均可集成到您的 CI/CD 中。
重要提示
此软件包是 ByJG PHP 迁移的命令行界面。有关项目和如何使用的信息,请访问:https://github.com/byjg/migration
安装
composer require 'byjg/migration-cli'
在命令行中运行
迁移库创建了一个名为 'migrate' 的脚本。它具有以下语法
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:
create Create the directory structure FROM a pre-existing database
down Migrate down the database version.
help Displays help for a command
install Install or upgrade the migrate version in a existing database
list Lists commands
reset Create a fresh new database
up Migrate Up the database version
update Migrate Up or Down the database version based on the current database version and the migration scripts available
version Get the current database version
命令
基本用法
基本用法如下
vendor/bin/migrate <COMMAND> --path=<scripts> uri://connection
--path
指定 base.sql 和迁移脚本的存放位置。如果您省略了 --path
,则默认为当前目录。您还可以使用 MIGRATE_PATH
环境变量设置基本路径
uri://connection 是表示数据库连接的 uri。您可以在 此处 了解有关连接字符串的更多信息。
如果您在 MIGRATE_CONNECTION
环境变量中定义了它,并且使用 MIGRATE_PATH
环境变量设置参数路径,则可以省略 uri 参数
export MIGRATE_CONNECTION=sqlite:///path/to/my.db export MIGRATE_PATH=/path/to/migrate_files
命令:create
创建一个带有 base.sql 和 migrations/up 及 migrations/down 的空目录结构,用于迁移。这对于从头开始创建迁移方案非常有用。
示例。
migrate create /path/to/sql
命令:install
如果您已经有了数据库,但不是由迁移系统控制的,您可以使用此方法安装迁移所需的表。
migrate install mysql://server/database
命令:update
将应用所有必要的迁移以保持数据库更新。
migrate update mysql://server/database
更新命令可以选择根据您的当前数据库版本上移或下移数据库。您也可以指定一个版本
migrate update --up-to=34
命令:reset
使用 "base.sql" 创建/替换数据库并应用所有迁移
migrate reset # reset the database and apply all migrations scripts. migrate reset --up-to=5 # reset the database and apply the migration from the # start up to the version 5. migrate reset --yes # reset the database without ask anything. Be careful!!
关于重置的说明:您可以通过将环境变量 MIGRATE_DISABLE_RESET
设置为 true 来禁用重置命令
export MIGRATE_DISABLE_RESET=true