graze / morphism
MySQL 数据库迁移
Requires
- php: ^7.2
- doctrine/dbal: ^2.5
- symfony/console: ^2.8 | ^3 | ^4
- symfony/yaml: ^2.6 | ^3 | ^4
Requires (Dev)
- graze/standards: ^2.0
- mockery/mockery: ^1.2
- phpunit/phpunit: ^5.7.21 | ^6 | ^7
- squizlabs/php_codesniffer: ^3.5.0
- symfony/var-dumper: ^4.2
This package is auto-updated.
Last update: 2024-08-31 00:26:35 UTC
README
此软件包提供了一系列工具,用于解析、提取和比较mysqldump文件。
此工具的典型应用是在应用程序开发期间管理模式变更(在切换分支时保持模式与代码同步),以及在部署期间(将模式迁移以匹配已部署的代码)。
使用此工具可以使您将完整的数据库模式存储在代码库中。当一个分支需要更新模式才能正常工作时,您应编辑您的检出模式并运行新的工具,以确定要运行的必要 ALTER
/ CREATE
/ DROP
语句,并应用它们。同样,在切换分支时,您只需运行工具,它将自动应用必要的更改。
这还有一个额外的优点,即模式的完整历史记录存储在版本控制之下,而不是一系列增量更改脚本。如果多个方面更改了同一张表,Git将自动合并更改,或者在无法自动合并时生成冲突以供手动合并。所有常规的Git工具都变得很有用 - 例如,简单的 git annotate schema/catalog/product.sql
可以告诉您谁在 pr_name
上添加了冗余索引。
安装
通过Composer
$ composer require graze/morphism
使用Docker运行
$ docker run --rm graze/morphism
示例
$ docker run --rm -v $PWD/config:/app/config -v $PWD/schema:/app/schema:cached graze/morphism diff config/morphism.yml $ docker run --rm -v $PWD/config:/app/config -v $PWD/schema:/app/schema:delegated graze/morphism dump config/morphism.yml
在开发时附加到现有网络
您可以将morphism添加到您的 docker-compose
文件中,并且可以在本地与数据库通信。或者如果您有一个现有的docker网络,您可以进行以下操作:
$ docker run --rm -v $PWD/config:/app/config -v $PWD/schema:/app/schema:cached --network app_default graze/morphsim diff config/morphism.yml
工具
所有命令都支持 --help
参数,该参数提供有关使用的更多信息。
- morphism extract: 从mysqldump文件中提取模式定义。
- morphism dump: 为命名数据库连接转储数据库模式。
- morphism lint: 检查数据库模式文件的正确性。
- morphism diff: 显示使给定数据库匹配模式文件的必要DDL语句。可选地应用更改。
配置文件
一些morphism工具使用的配置文件使用yaml格式,如下所示
# All connection definitions appear under the 'databases' key
databases:
# name of connection
catalog:
# Connection details - this is just an example, you may want to specify
# different properties, e.g. if connecting to a remote server. You are
# advised to refer to the 'pdo' documentation for further details.
user: 'my-user'
password: 'my-password'
host: 'localhost'
driver: 'pdo_mysql'
unix_socket: '/var/lib/mysql/catalog.sock'
# morphism specific options
morphism:
# morphism diff only operates on connections with 'enable: true'
enable: true
# Path where schema files live.
# Defaults to "schema/<connection-name>"
schemaDefinitionPath:
- schema/catalog
# you may optionally specify one or more regexes matching tables
# to exclude (any changes, creation or deletion of matching tables
# will be ignored). The regex must match the entire table name, i.e.
# it is implicitly anchored with ^...$
exclude:
- temp_.*
- page_load_\d{4}-\d{2}-\d{2}
# similarly, you may optionally specify tables for explicit inclusion.
include:
...
# you may specify more connections
...
# other top level keys are ignored
...
示例用法
此示例使用 morphism dump
从数据库生成模式文件,使用 morphism lint
检查文件,并使用 morphism diff
以交互方式和自动方式应用更改。
(master) $ # create a baseline for the schema
(master) $ mkdir schema
(master) $ bin/morphism dump --write config.yml catalog
(master) $ git add schema/catalog
(master) $ git commit -m "initial checkin of catalog schema"
(master) $
(master) $ # start work on changes to the catalog...
(master) $ git checkout -b catalog-fixes
(catalog-fixes) $ vi schema/catalog/product.sql # edit table definition
(catalog-fixes) $ vi schema/catalog/product_dimensions.sql # add new table
(catalog-fixes) $ bin/morphism lint schema/catalog # check syntax
ERROR schema/catalog/product_dimensions.sql, line 2: unknown datatype 'intt'
1: CREATE TABLE product_dimensions (
2: `pd_id` intt<<HERE>>(10) unsigned NOT NULL AUTO_INCREMENT,
(catalog-fixes) $ vi schema/catalog/product_dimensions.sql # fix table definition
(catalog-fixes) $ bin/morphism lint schema/catalog # check syntax
(catalog-fixes) $ git add schema/catalog
(catalog-fixes) $ git rm schema/catalog/discontinued.sql # delete a table
(catalog-fixes) $ git commit -m "various changes to catalog schema"
(catalog-fixes) $ # alter the database to match the schema files
(catalog-fixes) $ bin/morphism diff --apply-changes=confirm config.yml catalog
-- --------------------------------
-- Connection: catalog
-- --------------------------------
DROP TABLE IF EXISTS `discontinued`;
ALTER TABLE `product`
MODIFY COLUMN `pr_name` varchar(255) NOT NULL,
MODIFY COLUMN `pr_description` text NOT NULL,
ADD COLUMN `pr_modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP AFTER `pr_description`;
CREATE TABLE `product_dimensions` (
`pd_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`pd_width` decimal(10,2) NOT NULL,
`pd_height` decimal(10,2) NOT NULL,
`pd_depth` decimal(10,2) NOT NULL,
PRIMARY KEY (`pd_id`)
) ENGINE=InnoDB;
-- Confirm changes to catalog:
DROP TABLE IF EXISTS `discontinued`;
-- Apply this change? [y]es [n]o [a]ll [q]uit: y
ALTER TABLE `product`
MODIFY COLUMN `pr_name` varchar(255) NOT NULL,
MODIFY COLUMN `pr_description` text NOT NULL,
ADD COLUMN `pr_modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP AFTER `pr_description`;
-- Apply this change? [y]es [n]o [a]ll [q]uit: y
CREATE TABLE `product_dimensions` (
`pd_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`pd_width` decimal(10,2) NOT NULL,
`pd_height` decimal(10,2) NOT NULL,
`pd_depth` decimal(10,2) NOT NULL,
PRIMARY KEY (`pd_id`)
) ENGINE=InnoDB;
-- Apply this change? [y]es [n]o [a]ll [q]uit: y
(catalog-fixes) $ # hack hack hack
(catalog-fixes) $ ...
(catalog-fixes) $ # do some work back on master...
(catalog-fixes) $ git checkout master
(master) $ # restore schema to previous state
(master) $ bin/morphism diff --apply-changes=yes config.yml catalog
测试
$ make test
安全
如果您发现任何与安全相关的问题,请通过电子邮件 security@graze.com 而不是使用问题跟踪器。
致谢
许可
MIT许可(MIT)。请参阅 许可文件 以获取更多信息。