ApplyLobsterInstanceColumns.java 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.Statement;
  5. /** Add missing lobster_workflow_instance columns for validation. */
  6. public class ApplyLobsterInstanceColumns {
  7. private static final String URL = "jdbc:mysql://cq-cdb-8fjmemkb.sql.tencentcdb.com:27220/ylrz_saas"
  8. + "?useSSL=false&serverTimezone=GMT%2B8&allowMultiQueries=true";
  9. private static final String USER = "root";
  10. private static final String PWD = "Ylrz_1q2w3e4r5t6y";
  11. public static void main(String[] args) throws Exception {
  12. String[][] cols = {
  13. {"binding_id", "ALTER TABLE lobster_workflow_instance ADD COLUMN binding_id bigint DEFAULT NULL AFTER workflow_id"},
  14. {"dept_id", "ALTER TABLE lobster_workflow_instance ADD COLUMN dept_id bigint DEFAULT NULL AFTER channel_type"},
  15. {"company_user_id", "ALTER TABLE lobster_workflow_instance ADD COLUMN company_user_id bigint DEFAULT NULL AFTER dept_id"},
  16. {"qw_user_id", "ALTER TABLE lobster_workflow_instance ADD COLUMN qw_user_id bigint DEFAULT NULL AFTER company_user_id"},
  17. {"wx_account_id", "ALTER TABLE lobster_workflow_instance ADD COLUMN wx_account_id bigint DEFAULT NULL AFTER qw_user_id"},
  18. };
  19. try (Connection c = DriverManager.getConnection(URL, USER, PWD);
  20. Statement st = c.createStatement()) {
  21. for (String[] col : cols) {
  22. if (columnExists(st, col[0])) {
  23. System.out.println("skip " + col[0]);
  24. } else {
  25. st.execute(col[1]);
  26. System.out.println("added " + col[0]);
  27. }
  28. }
  29. }
  30. }
  31. private static boolean columnExists(Statement st, String col) throws Exception {
  32. try (ResultSet rs = st.executeQuery(
  33. "SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='ylrz_saas'"
  34. + " AND TABLE_NAME='lobster_workflow_instance' AND COLUMN_NAME='" + col + "'")) {
  35. rs.next();
  36. return rs.getInt(1) > 0;
  37. }
  38. }
  39. }