Advertisement
abhi_madhani

MySQL Database Schema Comparison script

Jun 20th, 2015
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 3.54 KB | None | 0 0
  1. SET @source_db = 'source_db';
  2. SET @target_db = 'target_db';
  3.  
  4. SELECT
  5.   'Only in source' exist_type,
  6.   c1.table_schema, c1.table_name, c1.column_name, c1.ordinal_position, c1.column_default, c1.is_nullable, c1.numeric_precision, c1.numeric_scale, c1.character_set_name, c1.collation_name, c1.column_type, c1.column_key, c1.extra, c1.column_comment
  7. FROM
  8.   (SELECT * FROM information_schema.columns WHERE TABLE_SCHEMA = @source_db) c1
  9.   LEFT JOIN (SELECT * FROM information_schema.columns WHERE TABLE_SCHEMA = @target_db) c2
  10.     ON c1.TABLE_name = c2.TABLE_name AND c1.column_name = c2.column_name
  11. WHERE c2.column_name is null
  12.  
  13. UNION ALL
  14.  
  15. SELECT
  16.   'Only in target' exist_type,
  17.   c2.table_schema, c2.table_name, c2.column_name, c2.ordinal_position, c2.column_default, c2.is_nullable, c2.numeric_precision, c2.numeric_scale, c2.character_set_name, c2.collation_name, c2.column_type, c2.column_key, c2.extra, c2.column_comment
  18. FROM
  19.   (SELECT * FROM information_schema.columns WHERE TABLE_SCHEMA = @source_db) c1
  20.   RIGHT JOIN (SELECT * FROM information_schema.columns WHERE TABLE_SCHEMA = @target_db) c2
  21.     ON c1.TABLE_name = c2.TABLE_name AND c1.column_name = c2.column_name
  22. WHERE c1.column_name is null
  23.  
  24. UNION ALL
  25.  
  26. SELECT
  27.   'In both schemas' exist_type,
  28.   CONCAT(c1.table_schema, '/', c2.table_schema),
  29.   c1.table_name, c1.column_name,
  30.   IF(c1.ordinal_position = c2.ordinal_position OR c1.ordinal_position IS NULL AND c2.ordinal_position IS NULL, NULL, CONCAT_WS('/', IFNULL(c1.ordinal_position, ''), IFNULL(c2.ordinal_position, ''))),
  31.   IF(c1.column_default = c2.column_default OR c1.column_default IS NULL AND c2.column_default IS NULL, NULL, CONCAT_WS('/', IFNULL(c1.column_default, ''), IFNULL(c2.column_default, ''))),
  32.   IF(c1.is_nullable = c2.is_nullable OR c1.is_nullable IS NULL AND c2.is_nullable IS NULL, NULL, CONCAT_WS('/', IFNULL(c1.is_nullable, ''), IFNULL(c2.is_nullable, ''))),
  33.   IF(c1.numeric_precision = c2.numeric_precision OR c1.numeric_precision IS NULL AND c2.numeric_precision IS NULL, NULL, CONCAT_WS('/', IFNULL(c1.numeric_precision, ''), IFNULL(c2.numeric_precision, ''))),
  34.   IF(c1.numeric_scale = c2.numeric_scale OR c1.numeric_scale IS NULL AND c2.numeric_scale IS NULL, NULL, CONCAT_WS('/', IFNULL(c1.numeric_scale, ''), IFNULL(c2.numeric_scale, ''))),
  35.   IF(c1.character_set_name = c2.character_set_name OR c1.character_set_name IS NULL AND c2.character_set_name IS NULL, NULL, CONCAT_WS('/', IFNULL(c1.character_set_name, ''), IFNULL(c2.character_set_name, ''))),
  36.   IF(c1.collation_name = c2.collation_name OR c1.collation_name IS NULL AND c2.collation_name IS NULL, NULL, CONCAT_WS('/', IFNULL(c1.collation_name, ''), IFNULL(c2.collation_name, ''))),
  37.   IF(c1.column_type = c2.column_type OR c1.column_type IS NULL AND c2.column_type IS NULL, NULL, CONCAT_WS('/', IFNULL(c1.column_type, ''), IFNULL(c2.column_type, ''))),
  38.   IF(c1.column_key = c2.column_key OR c1.column_key IS NULL AND c2.column_key IS NULL, NULL, CONCAT_WS('/', IFNULL(c1.column_key, ''), IFNULL(c2.column_key, ''))),
  39.   IF(c1.extra = c2.extra OR c1.extra IS NULL AND c2.extra IS NULL, NULL, CONCAT_WS('/', IFNULL(c1.extra, ''), IFNULL(c2.extra, ''))),
  40.   IF(c1.column_comment = c2.column_comment OR c1.column_comment IS NULL AND c2.column_comment IS NULL, NULL, CONCAT_WS('/', IFNULL(c1.column_comment, ''), IFNULL(c2.column_comment, '')))
  41. FROM
  42.   (SELECT * FROM information_schema.columns WHERE TABLE_SCHEMA = @source_db) c1
  43.   JOIN (SELECT * FROM information_schema.columns WHERE TABLE_SCHEMA = @target_db) c2
  44.     ON c1.TABLE_name = c2.TABLE_name AND c1.column_name = c2.column_name;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement