DeleteLegacyLobsterMenu2951Master.java 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import java.sql.*;
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.util.LinkedHashSet;
  5. import java.util.List;
  6. import java.util.Set;
  7. /** Delete legacy menu 2951 tree from master tenant_sys_menu. */
  8. public class DeleteLegacyLobsterMenu2951Master {
  9. private static final String URL = "jdbc:mysql://cq-cdb-8fjmemkb.sql.tencentcdb.com:27220/ylrz_saas?useSSL=false&serverTimezone=GMT%2B8";
  10. private static final String USER = "root";
  11. private static final String PASS = "Ylrz_1q2w3e4r5t6y";
  12. private static final long LEGACY_ROOT = 2951L;
  13. public static void main(String[] args) throws Exception {
  14. Class.forName("com.mysql.cj.jdbc.Driver");
  15. try (Connection c = DriverManager.getConnection(URL, USER, PASS)) {
  16. if (!exists(c, LEGACY_ROOT)) {
  17. System.out.println("2951 not found in tenant_sys_menu, skip");
  18. return;
  19. }
  20. Set<Long> ids = collect(c, LEGACY_ROOT);
  21. System.out.println("delete tenant_sys_menu ids=" + ids);
  22. if (tableExists(c, "sys_role_menu")) {
  23. deleteIn(c, "sys_role_menu", "menu_id", ids);
  24. }
  25. List<Long> ordered = new ArrayList<>(ids);
  26. Collections.sort(ordered, Collections.reverseOrder());
  27. int n = 0;
  28. try (PreparedStatement ps = c.prepareStatement("DELETE FROM tenant_sys_menu WHERE menu_id = ?")) {
  29. for (Long id : ordered) {
  30. ps.setLong(1, id);
  31. n += ps.executeUpdate();
  32. }
  33. }
  34. System.out.println("deleted rows=" + n);
  35. }
  36. }
  37. static Set<Long> collect(Connection c, long root) throws SQLException {
  38. Set<Long> all = new LinkedHashSet<>();
  39. walk(c, root, all);
  40. all.add(root);
  41. return all;
  42. }
  43. static void walk(Connection c, long parent, Set<Long> acc) throws SQLException {
  44. try (PreparedStatement ps = c.prepareStatement("SELECT menu_id FROM tenant_sys_menu WHERE parent_id = ?")) {
  45. ps.setLong(1, parent);
  46. try (ResultSet rs = ps.executeQuery()) {
  47. while (rs.next()) {
  48. long id = rs.getLong(1);
  49. if (acc.add(id)) walk(c, id, acc);
  50. }
  51. }
  52. }
  53. }
  54. static void deleteIn(Connection c, String table, String col, Set<Long> ids) throws SQLException {
  55. StringBuilder sb = new StringBuilder("DELETE FROM ").append(table).append(" WHERE ").append(col).append(" IN (");
  56. int i = 0;
  57. for (Long ignored : ids) {
  58. if (i++ > 0) sb.append(',');
  59. sb.append('?');
  60. }
  61. sb.append(')');
  62. try (PreparedStatement ps = c.prepareStatement(sb.toString())) {
  63. i = 1;
  64. for (Long id : ids) ps.setLong(i++, id);
  65. int n = ps.executeUpdate();
  66. if (n > 0) System.out.println("deleted " + table + " rows=" + n);
  67. }
  68. }
  69. static boolean exists(Connection c, long id) throws SQLException {
  70. try (PreparedStatement ps = c.prepareStatement("SELECT 1 FROM tenant_sys_menu WHERE menu_id=?")) {
  71. ps.setLong(1, id);
  72. try (ResultSet rs = ps.executeQuery()) { return rs.next(); }
  73. }
  74. }
  75. static boolean tableExists(Connection c, String t) throws SQLException {
  76. try (ResultSet rs = c.getMetaData().getTables(null, null, t, null)) { return rs.next(); }
  77. }
  78. }