| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- import java.sql.*;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * Add lobster_workflow_instance.binding_id on tenant DB(s).
- * Usage:
- * java -cp ".;mysql-connector-j.jar" ApplyLobsterBindingIdMigration all
- * java -cp ".;mysql-connector-j.jar" ApplyLobsterBindingIdMigration 33 32
- */
- public class ApplyLobsterBindingIdMigration {
- private static final String MASTER_HOST = getenv("DB_HOST", "cq-cdb-8fjmemkb.sql.tencentcdb.com");
- private static final int MASTER_PORT = Integer.parseInt(getenv("DB_PORT", "27220"));
- private static final String MASTER_USER = getenv("DB_USER", "root");
- private static final String MASTER_PASSWORD = getenv("DB_PASSWORD", "Ylrz_1q2w3e4r5t6y");
- private static final String MASTER_DB = getenv("DB_NAME", "ylrz_saas");
- public static void main(String[] args) throws Exception {
- List<Long> tenantIds = resolveTenantIds(args);
- if (tenantIds.isEmpty()) {
- System.err.println("Usage: ApplyLobsterBindingIdMigration all | tenantId ...");
- System.exit(1);
- }
- try (Connection master = open(MASTER_HOST, MASTER_PORT, MASTER_DB, MASTER_USER, MASTER_PASSWORD)) {
- for (Long tenantId : tenantIds) {
- TenantDb tenant = loadTenant(master, tenantId);
- if (tenant == null) {
- System.err.println("[SKIP] tenant " + tenantId + " not found");
- continue;
- }
- System.out.println("[RUN] tenant " + tenantId + " -> " + tenant.host + ":" + tenant.port + "/" + tenant.database);
- try (Connection tenantConn = open(tenant.host, tenant.port, tenant.database, tenant.user, tenant.password)) {
- applyBindingIdColumn(tenantConn);
- }
- System.out.println("[OK] tenant " + tenantId);
- }
- }
- }
- private static List<Long> resolveTenantIds(String[] args) throws SQLException {
- List<Long> ids = new ArrayList<>();
- if (args.length == 0) {
- ids.add(33L);
- return ids;
- }
- if ("all".equalsIgnoreCase(args[0].trim())) {
- try (Connection master = open(MASTER_HOST, MASTER_PORT, MASTER_DB, MASTER_USER, MASTER_PASSWORD);
- Statement st = master.createStatement();
- ResultSet rs = st.executeQuery("SELECT id FROM tenant_info WHERE status = 1 ORDER BY id")) {
- while (rs.next()) {
- ids.add(rs.getLong(1));
- }
- }
- return ids;
- }
- for (String arg : args) {
- ids.add(Long.parseLong(arg.trim()));
- }
- return ids;
- }
- private static void applyBindingIdColumn(Connection conn) throws SQLException {
- if (!tableExists(conn, "lobster_workflow_instance")) {
- System.out.println(" skip: table lobster_workflow_instance not found");
- return;
- }
- addColumnIfMissing(conn, "lobster_workflow_instance", "binding_id",
- "ALTER TABLE lobster_workflow_instance ADD COLUMN binding_id BIGINT NULL "
- + "COMMENT 'tag binding id' AFTER workflow_id");
- addIndexIfMissing(conn, "lobster_workflow_instance", "idx_lobster_instance_binding",
- "CREATE INDEX idx_lobster_instance_binding "
- + "ON lobster_workflow_instance (company_id, binding_id, del_flag)");
- }
- private static void addColumnIfMissing(Connection conn, String table, String column, String ddl) throws SQLException {
- if (columnExists(conn, table, column)) {
- System.out.println(" column exists: " + table + "." + column);
- return;
- }
- try (Statement st = conn.createStatement()) {
- st.execute(ddl);
- System.out.println(" added column: " + table + "." + column);
- }
- }
- private static void addIndexIfMissing(Connection conn, String table, String indexName, String ddl) throws SQLException {
- if (indexExists(conn, table, indexName)) {
- System.out.println(" index exists: " + table + "." + indexName);
- return;
- }
- try (Statement st = conn.createStatement()) {
- st.execute(ddl);
- System.out.println(" added index: " + table + "." + indexName);
- }
- }
- private static boolean tableExists(Connection conn, String table) throws SQLException {
- String sql = "SELECT COUNT(*) FROM information_schema.TABLES "
- + "WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ?";
- try (PreparedStatement ps = conn.prepareStatement(sql)) {
- ps.setString(1, table);
- try (ResultSet rs = ps.executeQuery()) {
- return rs.next() && rs.getInt(1) > 0;
- }
- }
- }
- private static boolean columnExists(Connection conn, String table, String column) throws SQLException {
- String sql = "SELECT COUNT(*) FROM information_schema.COLUMNS "
- + "WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ? AND COLUMN_NAME = ?";
- try (PreparedStatement ps = conn.prepareStatement(sql)) {
- ps.setString(1, table);
- ps.setString(2, column);
- try (ResultSet rs = ps.executeQuery()) {
- return rs.next() && rs.getInt(1) > 0;
- }
- }
- }
- private static boolean indexExists(Connection conn, String table, String indexName) throws SQLException {
- String sql = "SELECT COUNT(*) FROM information_schema.STATISTICS "
- + "WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ? AND INDEX_NAME = ?";
- try (PreparedStatement ps = conn.prepareStatement(sql)) {
- ps.setString(1, table);
- ps.setString(2, indexName);
- try (ResultSet rs = ps.executeQuery()) {
- return rs.next() && rs.getInt(1) > 0;
- }
- }
- }
- private static TenantDb loadTenant(Connection master, long tenantId) throws SQLException {
- String sql = "SELECT db_url, db_account, db_pwd FROM tenant_info WHERE id = ? AND status = 1 LIMIT 1";
- try (PreparedStatement ps = master.prepareStatement(sql)) {
- ps.setLong(1, tenantId);
- try (ResultSet rs = ps.executeQuery()) {
- if (!rs.next()) {
- return null;
- }
- return parseJdbcUrl(rs.getString("db_url"), rs.getString("db_account"), rs.getString("db_pwd"));
- }
- }
- }
- private static TenantDb parseJdbcUrl(String jdbcUrl, String user, String password) {
- if (jdbcUrl == null || !jdbcUrl.startsWith("jdbc:mysql://")) {
- throw new IllegalArgumentException("Unsupported db_url: " + jdbcUrl);
- }
- String body = jdbcUrl.substring("jdbc:mysql://".length());
- int slash = body.indexOf('/');
- if (slash < 0) {
- throw new IllegalArgumentException("Invalid db_url: " + jdbcUrl);
- }
- String hostPort = body.substring(0, slash);
- String rest = body.substring(slash + 1);
- int q = rest.indexOf('?');
- String database = q >= 0 ? rest.substring(0, q) : rest;
- String host;
- int port;
- int colon = hostPort.indexOf(':');
- if (colon >= 0) {
- host = hostPort.substring(0, colon);
- port = Integer.parseInt(hostPort.substring(colon + 1));
- } else {
- host = hostPort;
- port = 3306;
- }
- TenantDb t = new TenantDb();
- t.host = host;
- t.port = port;
- t.database = database;
- t.user = user != null ? user : MASTER_USER;
- t.password = password != null ? password : MASTER_PASSWORD;
- return t;
- }
- private static Connection open(String host, int port, String database, String user, String password)
- throws SQLException {
- String url = "jdbc:mysql://" + host + ":" + port + "/" + database
- + "?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&allowMultiQueries=true";
- return DriverManager.getConnection(url, user, password);
- }
- private static String getenv(String key, String defaultValue) {
- String v = System.getenv(key);
- return v != null && !v.isEmpty() ? v : defaultValue;
- }
- private static final class TenantDb {
- String host;
- int port;
- String database;
- String user;
- String password;
- }
- }
|