ApplyBindingTargetAccountsMigration.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.Statement;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. public class ApplyBindingTargetAccountsMigration {
  8. private static final String DEFAULT_HOST = "cq-cdb-8fjmemkb.sql.tencentcdb.com";
  9. private static final int DEFAULT_PORT = 27220;
  10. private static final String DEFAULT_USER = "root";
  11. private static final String DEFAULT_PASSWORD = "Ylrz_1q2w3e4r5t6y";
  12. private static final String MASTER_DB = "ylrz_saas";
  13. private static final String[][] COLUMNS = {
  14. {"target_qw_user_ids", "VARCHAR(2000) NULL COMMENT '限定企微员工ID' AFTER target_channels"},
  15. {"target_wx_account_ids", "VARCHAR(2000) NULL COMMENT '限定个微账号ID' AFTER target_qw_user_ids"}
  16. };
  17. public static void main(String[] args) throws Exception {
  18. Class.forName("com.mysql.cj.jdbc.Driver");
  19. List<TenantDb> tenants = new ArrayList<>();
  20. try (Connection master = connect(DEFAULT_HOST, DEFAULT_PORT, MASTER_DB, DEFAULT_USER, DEFAULT_PASSWORD)) {
  21. try (Statement st = master.createStatement();
  22. ResultSet rs = st.executeQuery(
  23. "SELECT id, db_name, db_url, db_account, db_pwd, status FROM tenant_info ORDER BY id")) {
  24. while (rs.next()) {
  25. if (rs.getInt("status") != 1) continue;
  26. TenantDb t = new TenantDb();
  27. t.id = rs.getLong("id");
  28. t.dbUrl = rs.getString("db_url");
  29. t.dbName = rs.getString("db_name");
  30. t.user = rs.getString("db_account");
  31. t.password = rs.getString("db_pwd");
  32. parseJdbc(t);
  33. tenants.add(t);
  34. }
  35. }
  36. }
  37. for (TenantDb tenant : tenants) {
  38. apply(tenant);
  39. }
  40. System.out.println("Done.");
  41. }
  42. private static void apply(TenantDb tenant) throws Exception {
  43. try (Connection conn = connect(tenant.host, tenant.port, tenant.database, tenant.user, tenant.password);
  44. Statement st = conn.createStatement()) {
  45. for (String[] col : COLUMNS) {
  46. String name = col[0];
  47. String ddl = col[1];
  48. boolean exists;
  49. try (ResultSet rs = st.executeQuery(
  50. "SELECT COUNT(*) FROM information_schema.COLUMNS "
  51. + "WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'company_tag_template_binding' "
  52. + "AND COLUMN_NAME = '" + name + "'")) {
  53. rs.next();
  54. exists = rs.getInt(1) > 0;
  55. }
  56. if (exists) {
  57. System.out.println("[SKIP] tenantId=" + tenant.id + " col=" + name);
  58. continue;
  59. }
  60. st.execute("ALTER TABLE company_tag_template_binding ADD COLUMN " + name + " " + ddl);
  61. System.out.println("[OK] tenantId=" + tenant.id + " col=" + name + " db=" + tenant.database);
  62. }
  63. }
  64. }
  65. private static void parseJdbc(TenantDb t) {
  66. if (t.dbUrl != null && t.dbUrl.startsWith("jdbc:mysql://")) {
  67. String body = t.dbUrl.substring("jdbc:mysql://".length());
  68. int slash = body.indexOf('/');
  69. String hostPort = slash >= 0 ? body.substring(0, slash) : body;
  70. String rest = slash >= 0 ? body.substring(slash + 1) : t.dbName;
  71. int q = rest.indexOf('?');
  72. t.database = q >= 0 ? rest.substring(0, q) : rest;
  73. int colon = hostPort.indexOf(':');
  74. if (colon >= 0) {
  75. t.host = hostPort.substring(0, colon);
  76. t.port = Integer.parseInt(hostPort.substring(colon + 1));
  77. } else {
  78. t.host = hostPort;
  79. t.port = 3306;
  80. }
  81. } else {
  82. t.host = DEFAULT_HOST;
  83. t.port = DEFAULT_PORT;
  84. t.database = t.dbName;
  85. }
  86. if (t.user == null || t.user.isEmpty()) t.user = DEFAULT_USER;
  87. if (t.password == null) t.password = DEFAULT_PASSWORD;
  88. }
  89. private static Connection connect(String host, int port, String db, String user, String password) throws Exception {
  90. String url = "jdbc:mysql://" + host + ":" + port + "/" + db
  91. + "?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=true";
  92. return DriverManager.getConnection(url, user, password);
  93. }
  94. private static class TenantDb {
  95. long id;
  96. String dbUrl;
  97. String dbName;
  98. String host;
  99. int port;
  100. String database;
  101. String user;
  102. String password;
  103. }
  104. }