my6uot9/php-mysql-diff 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

my6uot9/php-mysql-diff

Composer 安装命令:

composer create-project my6uot9/php-mysql-diff

包简介

README 文档

README

MySQL Schema Diff - Comparison / Migration Script Generation

Travis-CI

Why not mysqldiff from MySQL Utilities?

MySQL Utilities includes a similar tool, mysqldiff, that is absolutely horrible! The purpose of this project is to provide a tool that is simple to use, reliable and fast.

Installation

To install PHP MySQL Diff, install Composer and issue the following command:

$ ./composer.phar global require camcima/php-mysql-diff

Then, make sure you have ~/.composer/vendor/bin in your PATH, and you're good to go:

export PATH="$PATH:$HOME/.composer/vendor/bin"

Update

You can update PHP MySQL Diff through this command:

$ ./composer.phar global update camcima/php-mysql-diff

Usage

Database Creation Scripts

PHP MySQL Diff works with database creations scripts created by mysqldump, which is part of the MySQL distribution. In order to generate a database creation script, use the following command:

$ mysqldump -h hostname -u username -p -d dbname > outputfile.sql

This tool may not work with creation scripts generated by other means because it relies on finely tuned regular expressions that could not work if the file format is different.

I chose to work with database creation scripts instead of working by connecting to the databases directly because it's more portable that way and you can work offline. In the future, I might develop the option to fetch the information directly from the database INFORMATION_SCHEMA table.

Diff

$ php-mysql-diff diff <from> <to> [-i <ignore-tables-file>]

where from is the path to the initial database creation script and to is the path to the target database creation script.

Ignore Tables

Use the -i option to ignore tables during comparison. The file format is a list of regular expressions to match the table names to be ignored, one per line.

Example:

/^employee.+/
/^catalog$/
/^test[\d]$/

Output File

The output will be like this:

PHP MySQL Diff 1.0.0
----------------------------------------

• Parsing initial database ...... ✓
• Parsing target database ....... ✓
• Comparing databases ........... ✓

FROM tests\fixtures\sakila.sql
  TO tests\fixtures\sakila_new.sql

▲ table "test3" is in the TO database but not in the FROM database
▼ table "test1" is in the FROM database but not in the TO database
► table "test2" has a different schema
    ▲ column "new_field" is in the TO database but not in the FROM database
    ► column "id" has a different definition
        FROM `id` int(11) NOT NULL AUTO_INCREMENT
          TO `id` int(10) NOT NULL AUTO_INCREMENT
    ► column "fk" has a different definition
        FROM `fk` int(10) NOT NULL
          TO `fk` int(10)
    ► column "val" has a different definition
        FROM `val` decimal(10,2) NOT NULL
          TO `val` decimal(11,3) NOT NULL
    ► column "texto" has a different definition
        FROM `texto` varchar(60) DEFAULT NULL
          TO `texto` char(60) NOT NULL DEFAULT 'default'
    ► primary key has a different definition
        FROM PRIMARY KEY (`id`)
          TO PRIMARY KEY (`id`,`new_field`)
    ▲ foreign key "FK__test3" is in the TO database but not in the FROM database
    ▼ foreign key "FK__test1" is in the FROM database but not in the TO database
    ► index "FK__test1" has a different definition
        FROM KEY `FK__test1` (`fk`)
          TO UNIQUE KEY `FK__test1` (`datade`)

Diff completed!

▲ = only present in the TO database

► = different definitions between FROM and TO databases

▼ = only present in the FROM database

Migration Script

$ php-mysql-diff migrate <from> <to> [-o <output-file>] [-i <ignore-tables-file>] [-p]

where from is the path to the initial database creation script and to is the path to the target database creation script.

Ignore Tables

Use the -i option to ignore tables during comparison. The file format is a list of regular expressions to match the table names to be ignored, one per line.

Example:

/^employee.+/
/^catalog$/
/^test[\d]$/

Output File

If the -o option is not used, the migration script will be output to the stdout.

The output (with the -o option) will be like this:

PHP MySQL Diff 1.0.0
----------------------------------------

• Parsing initial database ...... ✓
• Parsing target database ....... ✓
• Comparing databases ........... ✓
• Generating migration script ... ✓
• Writing output file ........... ✓

Migration script generated!

and the migration script will look like this:

# Disable Foreign Keys Check
SET FOREIGN_KEY_CHECKS = 0;
SET SQL_MODE = '';

# Deleted Tables

-- deleted table `test1`

DROP TABLE `test1`;

# Changed Tables

-- changed table `test2`

ALTER TABLE `test2`
  DROP PRIMARY KEY,
  DROP FOREIGN KEY `FK__test1`,
  DROP INDEX `FK__test1`,
  CHANGE COLUMN `id` `id` int(10) NOT NULL AUTO_INCREMENT FIRST,
  CHANGE COLUMN `fk` `fk` int(10) AFTER `id`,
  CHANGE COLUMN `val` `val` decimal(11,3) NOT NULL AFTER `fk`,
  CHANGE COLUMN `texto` `texto` char(60) NOT NULL DEFAULT 'default' AFTER `val`,
  ADD COLUMN `new_field` int(10) AFTER `datade`,
  ADD PRIMARY KEY (`id`,`new_field`),
  ADD UNIQUE KEY `FK__test1` (`datade`),
  ADD CONSTRAINT `FK__test3` FOREIGN KEY (`fk`) REFERENCES `test3` (`id`);

# New Tables

-- new table `test3`

CREATE TABLE `test3` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

# Disable Foreign Keys Check
SET FOREIGN_KEY_CHECKS = 1;

Display Progress

For long running migrations, it is recommended to use the -p option to display the progress of the running migration.

Contribute

Feel free to send your contributions as PR. Make sure you update/write new tests to support your contribution. Please follow PSR-2.

my6uot9/php-mysql-diff 适用场景与选型建议

my6uot9/php-mysql-diff 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 05 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「database」 「generator」 「mysql」 「migration」 「diff」 「script」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 my6uot9/php-mysql-diff 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 my6uot9/php-mysql-diff 我们能提供哪些服务?
定制开发 / 二次开发

基于 my6uot9/php-mysql-diff 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 12
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 48
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-05-10