当前位置:   article > 正文

mysql 表结构差异对比小工具_mysql表结构对比工具

mysql表结构对比工具

项目场景:

在进行慢sql评审时,发现测试环境增加了索引生产并没有。然后有些表字段长度也不一样,坑大发了。决定写一个小工具对比一下测试跟生产表结构差异,大致思路连接数据库获取建表语句进行对比,忽略主键自增id。因为测试跟生产主键id自增会有不同,如果建表语句不一致,则需要比对文本高亮显示。在网上copy了一个别人写的高亮算法,生成了一个html预览。直接上代码


  1. package com.xuyw.test.export;
  2. import com.alibaba.druid.pool.DruidDataSource;
  3. import org.apache.commons.collections4.MapUtils;
  4. import org.apache.commons.io.FileUtils;
  5. import org.springframework.jdbc.core.JdbcTemplate;
  6. import java.io.File;
  7. import java.io.IOException;
  8. import java.util.*;
  9. /**
  10. * @author one.xu
  11. * @version v1.0
  12. * @description
  13. * @date 2022/1/5 17:59
  14. */
  15. public class DbComparedTest {
  16. private static JdbcTemplate db1;
  17. private static JdbcTemplate db2;
  18. static {
  19. db1 = new JdbcTemplate();
  20. DruidDataSource dataSource = new DruidDataSource();
  21. dataSource.setUrl("");
  22. dataSource.setUsername("");
  23. dataSource.setPassword("");
  24. db1.setDataSource(dataSource);
  25. db2 = new JdbcTemplate();
  26. DruidDataSource dataSource2 = new DruidDataSource();
  27. dataSource2.setUrl("");
  28. dataSource2.setUsername("");
  29. dataSource2.setPassword("");
  30. db2.setDataSource(dataSource2);
  31. }
  32. //移除AUTO_INCREMENT
  33. private static String handTable(String table) {
  34. return table.replaceAll("AUTO_INCREMENT=\\d+", "").trim();
  35. }
  36. //文本对比,高亮显示
  37. public static String getcompareStr(String char1, String char2) {
  38. String bcolor = "<span style='background-color:yellow;color:red;'>";
  39. String ecolor = "</span>";
  40. StringBuffer sb = new StringBuffer();
  41. char[] a = new char[char1.length()];
  42. for (int i = 0; i < char1.length(); i++) {
  43. a[i] = char1.charAt(i);
  44. }
  45. char[] b = new char[char2.length()];
  46. for (int i = 0; i < char2.length(); i++) {
  47. b[i] = char2.charAt(i);
  48. }
  49. // 不同字符集合
  50. Map<Object, Object> map1 = new HashMap<>();
  51. // 包含字符集合
  52. Map<Object, Object> map2 = new HashMap<>();
  53. for (int i = 0; i < a.length; i++) {
  54. if (i == a.length - 1) {
  55. if (i > 1) {
  56. if (String.valueOf(b).contains(String.valueOf(a[i - 1]) + String.valueOf(a[i]))) {
  57. map2.put(i - 1, a[i - 1]);
  58. map2.put(i, a[i]);
  59. } else {
  60. map1.put(i, a[i]);
  61. }
  62. } else {
  63. map2.put(i, a[i]);
  64. }
  65. } else {
  66. if (String.valueOf(b).contains(String.valueOf(a[i]) + String.valueOf(a[i + 1]))) {
  67. if (i > 1) {
  68. if (String.valueOf(b).contains(String.valueOf(a[i - 1]) + String.valueOf(a[i]))) {
  69. map2.put(i - 1, a[i - 1]);
  70. map2.put(i, a[i]);
  71. }
  72. } else {
  73. map2.put(i, a[i]);
  74. }
  75. } else {
  76. if (i > 0) {
  77. if (String.valueOf(b).contains(String.valueOf(a[i - 1]) + String.valueOf(a[i]))) {
  78. map2.put(i - 1, a[i - 1]);
  79. map2.put(i, a[i]);
  80. } else {
  81. map1.put(i, a[i]);
  82. }
  83. } else {
  84. map1.put(i, a[i]);
  85. }
  86. }
  87. }
  88. }
  89. for (int i = 0; i < a.length; i++) {
  90. if (map1.get(i) != null) {
  91. sb.append(bcolor).append(map1.get(i)).append(ecolor);
  92. } else if (map2.get(i) != null) {
  93. sb.append(map2.get(i));
  94. }
  95. }
  96. return sb.toString();
  97. }
  98. public static void main(String[] args) throws IOException {
  99. //需要比对的数据库名,多个逗号连接
  100. String dbs = "testDb1,testDb1";
  101. String tableSql = "select table_name,table_comment FROM information_schema.tables WHERE table_schema=?";
  102. //忽略比对的表名,多个逗号连接 支持正则
  103. String ignoreTable = "|(msg_log_\\w+)|(hand_log_\\w+)";
  104. String tableCreateSql = "show create table ";
  105. List<Map<String, Object>> tableDiffList = new ArrayList<>();
  106. Map<String, Object> diffMap;
  107. Map<String, Integer> dbTableCountMap = new HashMap<>();
  108. int dbTableCount = 0;
  109. for (String db : dbs.split(",")) {
  110. dbTableCount = 0;
  111. List<Map<String, Object>> tables = db1.queryForList(tableSql, db);
  112. for (int j = 0; j < tables.size(); j++) {
  113. String table = tables.get(j).get("table_name").toString();
  114. if (table.matches(ignoreTable)) {
  115. continue;
  116. }
  117. diffMap = new HashMap<>();
  118. Map<String, Object> sourceTableMap = db1.queryForMap(tableCreateSql + db + "." + table);
  119. String sourceTable = handTable(MapUtils.getString(sourceTableMap, "Create Table"));
  120. diffMap.put("dbName", db);
  121. diffMap.put("sourceTable", sourceTable);
  122. Map<String, Object> targetTableMap;
  123. try {
  124. targetTableMap = db2.queryForMap(tableCreateSql + db + "." + table);
  125. } catch (Exception e) {
  126. dbTableCount = dbTableCount + 1;
  127. diffMap.put("targetTable", "不存在");
  128. tableDiffList.add(diffMap);
  129. continue;
  130. }
  131. String targetTable = handTable(MapUtils.getString(targetTableMap, "Create Table"));
  132. if (sourceTable.equals(targetTable)) {
  133. continue;
  134. }
  135. diffMap.put("targetTable", getcompareStr(targetTable, sourceTable));
  136. diffMap.put("sourceTable", getcompareStr(sourceTable, targetTable));
  137. tableDiffList.add(diffMap);
  138. dbTableCount = dbTableCount + 1;
  139. }
  140. dbTableCountMap.put(db, dbTableCount);
  141. }
  142. StringJoiner html = new StringJoiner("\n");
  143. html.add("<table style=\"font-size:12px;color:#333333;width:100%;border-width: 1px;border-color: #729ea5;border-collapse: collapse;\" border=\"1\">\n" +
  144. "<tr><th >数据库名</th><th>源表</th><th>对比表</th></tr>");
  145. for (Map<String, Object> m : tableDiffList) {
  146. String dbName=m.get("dbName").toString();
  147. Integer dbTabeCount=dbTableCountMap.get(dbName);
  148. if(dbTabeCount!=null){
  149. html.add("<tr> <td rowspan=\""+dbTabeCount+"\">"+dbName+"</td>");
  150. dbTableCountMap.remove(dbName);
  151. }
  152. html.add("<td>\n" +
  153. "<pre> "+m.get("sourceTable").toString()+" <pre>\n" +
  154. "</td>");
  155. html.add("<td>\n" +
  156. "<pre> "+m.get("targetTable").toString()+" <pre>\n" +
  157. "</td>");
  158. html.add("</tr>");
  159. }
  160. html.add("</table>");
  161. FileUtils.writeStringToFile(new File("d:\\DbComparedTest.html"), html.toString());
  162. }
  163. }

对比效果如下

 


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/957629
推荐阅读
相关标签
  

闽ICP备14008679号