ApplyLobsterSchemaSync.java 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import java.nio.charset.StandardCharsets;
  2. import java.nio.file.Files;
  3. import java.nio.file.Path;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.Statement;
  8. /**
  9. * Apply lobster schema sync migration to tenant database(s).
  10. */
  11. public class ApplyLobsterSchemaSync {
  12. private static final String HOST = "cq-cdb-8fjmemkb.sql.tencentcdb.com:27220";
  13. private static final String USER = "root";
  14. private static final String PWD = "Ylrz_1q2w3e4r5t6y";
  15. public static void main(String[] args) throws Exception {
  16. String[] dbs = args.length > 0 ? args : new String[]{"fs_tenant_cs1"};
  17. String sql = Files.readString(
  18. Path.of("d:/ylrz_saas_new/java/fs-service/src/main/resources/db/tenant-initTable-migration-lobster-schema-sync.sql"),
  19. StandardCharsets.UTF_8);
  20. for (String db : dbs) {
  21. String url = "jdbc:mysql://" + HOST + "/" + db
  22. + "?useSSL=false&serverTimezone=GMT%2B8&allowMultiQueries=true&connectTimeout=15000&socketTimeout=120000";
  23. System.out.println("Applying lobster schema sync to " + db + " ...");
  24. try (Connection c = DriverManager.getConnection(url, USER, PWD);
  25. Statement st = c.createStatement()) {
  26. st.execute(sql);
  27. for (String tbl : new String[]{"lobster_execution_config", "lobster_tenant_keywords", "lobster_user_profile"}) {
  28. try (ResultSet rs = st.executeQuery(
  29. "SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='" + tbl + "'")) {
  30. rs.next();
  31. System.out.println(" " + tbl + ": " + (rs.getInt(1) > 0 ? "OK" : "MISSING"));
  32. }
  33. }
  34. try (ResultSet rs = st.executeQuery(
  35. "SELECT company_id FROM lobster_execution_config WHERE company_id=338 LIMIT 1")) {
  36. if (rs.next()) {
  37. System.out.println(" lobster_execution_config row for company 338: exists");
  38. } else {
  39. st.execute("INSERT IGNORE INTO lobster_execution_config (company_id) VALUES (338)");
  40. System.out.println(" lobster_execution_config row for company 338: inserted default");
  41. }
  42. } catch (Exception e) {
  43. System.out.println(" seed company 338 config skipped: " + e.getMessage());
  44. }
  45. st.execute("CREATE TABLE IF NOT EXISTS lobster_tool_config ("
  46. + "id BIGINT AUTO_INCREMENT PRIMARY KEY,"
  47. + "company_id BIGINT NOT NULL DEFAULT 0,"
  48. + "tool_name VARCHAR(128) NOT NULL,"
  49. + "tool_type VARCHAR(64) DEFAULT NULL,"
  50. + "description VARCHAR(500) DEFAULT NULL,"
  51. + "config_json TEXT,"
  52. + "enabled TINYINT NOT NULL DEFAULT 1,"
  53. + "deleted TINYINT NOT NULL DEFAULT 0,"
  54. + "create_time DATETIME DEFAULT CURRENT_TIMESTAMP,"
  55. + "UNIQUE KEY uk_company_tool (company_id, tool_name)"
  56. + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4");
  57. System.out.println(" lobster_tool_config: ensured");
  58. }
  59. System.out.println("Done " + db);
  60. }
  61. }
  62. }