ScanMasterAndAllTenantsLobster.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import java.sql.*;
  2. public class ScanMasterAndAllTenantsLobster {
  3. private static final String HOST = "cq-cdb-8fjmemkb.sql.tencentcdb.com";
  4. private static final int PORT = 27220;
  5. private static final String USER = "root";
  6. private static final String PWD = "Ylrz_1q2w3e4r5t6y";
  7. private static final String MASTER = "ylrz_saas";
  8. private static final String[] COLS = {
  9. "execution_mode", "enable_content_personalization", "enable_flow_personalization",
  10. "strict_fixed_workflow", "auto_start_instance_on_tag", "template_kind", "source_template_id"
  11. };
  12. public static void main(String[] a) throws Exception {
  13. checkDb("MASTER", MASTER, USER, PWD);
  14. String masterUrl = url(MASTER);
  15. try (Connection master = DriverManager.getConnection(masterUrl, USER, PWD)) {
  16. try (Statement st = master.createStatement();
  17. ResultSet rs = st.executeQuery("SELECT id, db_url, db_account, db_pwd, status FROM tenant_info ORDER BY id")) {
  18. while (rs.next()) {
  19. long id = rs.getLong("id");
  20. int status = rs.getInt("status");
  21. TenantDb t = parseJdbcUrl(rs.getString("db_url"), rs.getString("db_account"), rs.getString("db_pwd"));
  22. try {
  23. checkDb("tenantId=" + id + " status=" + status, t.database, t.user, t.password);
  24. } catch (Exception e) {
  25. System.out.println("[ERROR] tenantId=" + id + " db=" + t.database + " " + e.getMessage());
  26. }
  27. }
  28. }
  29. }
  30. }
  31. private static void checkDb(String label, String db, String user, String pwd) throws SQLException {
  32. try (Connection c = DriverManager.getConnection(url(db), user, pwd)) {
  33. if (!tableExists(c, "company_workflow_lobster")) {
  34. System.out.println("[NO_TABLE] " + label + " db=" + db);
  35. return;
  36. }
  37. StringBuilder missing = new StringBuilder();
  38. for (String col : COLS) {
  39. if (!columnExists(c, "company_workflow_lobster", col)) {
  40. if (missing.length() > 0) missing.append(',');
  41. missing.append(col);
  42. }
  43. }
  44. if (missing.length() > 0) {
  45. System.out.println("[MISSING] " + label + " db=" + db + " cols=" + missing);
  46. }
  47. }
  48. }
  49. private static boolean tableExists(Connection c, String table) throws SQLException {
  50. try (PreparedStatement ps = c.prepareStatement(
  51. "SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME=?")) {
  52. ps.setString(1, table);
  53. try (ResultSet rs = ps.executeQuery()) {
  54. return rs.next() && rs.getInt(1) > 0;
  55. }
  56. }
  57. }
  58. private static boolean columnExists(Connection c, String table, String column) throws SQLException {
  59. try (PreparedStatement ps = c.prepareStatement(
  60. "SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME=? AND COLUMN_NAME=?")) {
  61. ps.setString(1, table);
  62. ps.setString(2, column);
  63. try (ResultSet rs = ps.executeQuery()) {
  64. return rs.next() && rs.getInt(1) > 0;
  65. }
  66. }
  67. }
  68. private static String url(String db) {
  69. return "jdbc:mysql://" + HOST + ":" + PORT + "/" + db
  70. + "?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&allowMultiQueries=true";
  71. }
  72. private static TenantDb parseJdbcUrl(String jdbcUrl, String user, String password) {
  73. String body = jdbcUrl.substring("jdbc:mysql://".length());
  74. int slash = body.indexOf('/');
  75. String hostPort = body.substring(0, slash);
  76. String rest = body.substring(slash + 1);
  77. int q = rest.indexOf('?');
  78. String database = q >= 0 ? rest.substring(0, q) : rest;
  79. int colon = hostPort.indexOf(':');
  80. TenantDb t = new TenantDb();
  81. t.database = database;
  82. t.user = user != null ? user : USER;
  83. t.password = password != null ? password : PWD;
  84. return t;
  85. }
  86. private static final class TenantDb {
  87. String database;
  88. String user;
  89. String password;
  90. }
  91. }