The mysqldiff and mysqldbcompare utilities were designed to produce a difference report for objects and in the case of mysqldbcompare the data. Thus, you can compare two databases and produce a report of the differences in both object definitions and data rows. While that may be very useful, would it not be much more useful to have the ability to produce SQL commands to transform databases? Wait no longer! The latest release of MySQL Utilities has added the ability to generate SQL transformation statements by both the mysqldiff and mysqldbcompare utilities. To generate SQL transformations in either utility, simply use the –sql option to tell the utility to produce the statements.Object Transformations with mysqldiffIf you would like to compare the schema of two databases (the objects and their definitions), mysqldiff can do that for you and produce a difference report in a number of formats including CSV, TAB, GRID, and Vertical (like the mysql client’s \G option). However, its greatest feature is the ability to generate transformation statements to alter the objects so that they conform. Best of all, mysqldiff works on all object types including the ability to recognize renames so you can get a true transformation path for all objects. For even greater flexibility, you can generate the difference in both directions. This means you can generate transformations for db1-to-db2 as well as db2-to-db1 in the same pass. Cool.The following shows an example of running mysqldiff on two servers where some of the objects have diverged. It also shows how you can generate the reverse transformation statements.$ mysqldiff –server1=root@localhost –server2=root@otherhost \–changes-for=server1 –show-reverse util_test:util_test \–force –difftype=SQL# server1 on localhost: … connected.# server2 on localhost: … connected.# WARNING: Objects in server1.util_test but not in server2.util_test:# EVENT: e1# Comparing util_test to util_test [PASS]# Comparing util_test.f1 to util_test.f1 [PASS]# Comparing util_test.p1 to util_test.p1 [PASS]# Comparing util_test.t1 to util_test.t1 [PASS]# Comparing util_test.t2 to util_test.t2 [PASS]# Comparing util_test.t3 to util_test.t3 [FAIL]# Transformation for –changes-for=server1:#ALTER TABLE util_test.t3 DROP COLUMN b, ADD COLUMN d char(30) NULL AFTER a ENGINE=MyISAM;## Transformation for reverse changes (–changes-for=server2):## ALTER TABLE util_test.t3 # DROP COLUMN d, # ADD COLUMN b char(30) NULL AFTER a, # ENGINE=InnoDB;## Comparing util_test.trg to util_test.trg [FAIL]# Transformation for –changes-for=server1:#DROP TRIGGER IF EXISTS `util_test`.`trg`;CREATE DEFINER=root@localhost TRIGGER util_test.trg BEFORE UPDATE ON util_test.t1 FOR EACH ROW INSERT INTO util_test.t1 VALUES(‘Wax on, wax off’);## Transformation for reverse changes (–changes-for=server2):## DROP TRIGGER IF EXISTS `util_test`.`trg`;# CREATE DEFINER=root@localhost TRIGGER util_test.trg AFTER INSERT ON util_test.t1 # FOR EACH ROW INSERT INTO util_test.t2 VALUES(‘Test objects count’);## Comparing util_test.v1 to util_test.v1 [FAIL]# Transformation for –changes-for=server1:#ALTER VIEW util_test.v1 AS select `util_test`.`t2`.`a` AS `a` from `util_test`.`t2`;## Transformation for reverse changes (–changes-for=server2):## ALTER VIEW util_test.v1 AS # select `util_test`.`t1`.`a` AS `a` from `util_test`.`t1`;#Compare failed. One or more differences found.Generating Data Transformation with mysqldbcompareThe mysqldbcompare utility provides all of the object difference functionality included in mysqldiff along with the ability to generate transformation SQL statements for data. This means you can make sure your test or development databases are similar to your production databases or perhaps even your offline, read only databases match your online databases. Like mysqldiff, you can also get the reverse transformations at the same time. Very cool, eh?The following shows an example of running mysqldbcompare to generate differences in data.$ mysqldbcompare –server1=root@localhost –server2=root@otherhost \inventory:inventory -a –difftype=sql –changes-for=server1 \–show-reverse# server1 on localhost: … connected.# server2 on localhost: … connected.# Checking databases inventory on server1 and inventory on server2## WARNING: Objects in server1.inventory but not in server2.inventory:# VIEW: finishing_up# VIEW: cleaning#[…] # TABLE supplier pass FAIL FAIL ## Row counts are not the same among inventory.supplier and inventory.supplier.## Transformation for –changes-for=server1:## Data differences found among rows:UPDATE inventory.supplier SET name = ‘Wesayso Corporation’ WHERE code = ‘2’;INSERT INTO inventory.supplier (code, name) VALUES(‘3’, ‘Never Enough Inc.’);## Transformation for reverse changes (–changes-for=server2):## # Data differences found among rows:# UPDATE inventory.supplier SET name = ‘Never Enough Inc.’ WHERE code = ‘2’;# DELETE FROM inventory.supplier WHERE code = ‘3’;## Database consistency check failed.## …done
via Planet MySQL
Compare and Synchronize Databases with MySQL Utilities