Skip to content

Latest commit

 

History

History
181 lines (126 loc) · 5.42 KB

README.md

File metadata and controls

181 lines (126 loc) · 5.42 KB

PHP MySQL Diff

MySQL Schema Diff - Comparison / Migration Script Generation

This is a fork of camcima/php-mysql-diff.

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.

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.