ApplyLobsterLearningTables.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.Statement;
  4. /** Ensure Phase2 learning tables exist on tenant DB. */
  5. public class ApplyLobsterLearningTables {
  6. public static void main(String[] args) throws Exception {
  7. String db = args.length > 0 ? args[0] : "fs_tenant_cs1";
  8. String url = "jdbc:mysql://cq-cdb-8fjmemkb.sql.tencentcdb.com:27220/" + db
  9. + "?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8";
  10. String user = "root";
  11. String pass = "Ylrz_1q2w3e4r5t6y";
  12. String[] sqls = {
  13. "CREATE TABLE IF NOT EXISTS lobster_learning_event_log ("
  14. + "id bigint NOT NULL AUTO_INCREMENT, company_id bigint NOT NULL, instance_id bigint DEFAULT NULL,"
  15. + "node_code varchar(100) DEFAULT NULL, event_type varchar(50) DEFAULT NULL,"
  16. + "quality_score double DEFAULT NULL, context_snapshot text,"
  17. + "create_time datetime DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id),"
  18. + "KEY idx_company_event (company_id, event_type)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4",
  19. "CREATE TABLE IF NOT EXISTS lobster_learning_replay_buffer ("
  20. + "id bigint NOT NULL AUTO_INCREMENT, company_id bigint NOT NULL, instance_id bigint DEFAULT NULL,"
  21. + "node_code varchar(100) DEFAULT NULL, customer_message text, ai_reply text,"
  22. + "quality_score double DEFAULT NULL, create_time datetime DEFAULT CURRENT_TIMESTAMP,"
  23. + "PRIMARY KEY (id), KEY idx_company_id (company_id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4",
  24. "CREATE TABLE IF NOT EXISTS lobster_learning_replay_archive ("
  25. + "id bigint NOT NULL AUTO_INCREMENT, company_id bigint NOT NULL, instance_id bigint DEFAULT NULL,"
  26. + "node_code varchar(100) DEFAULT NULL, customer_message text, ai_reply text,"
  27. + "quality_score double DEFAULT NULL, create_time datetime DEFAULT NULL,"
  28. + "archived_time datetime DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id),"
  29. + "KEY idx_company_archived (company_id, archived_time)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4",
  30. "ALTER TABLE lobster_learning_replay_buffer "
  31. + "ADD INDEX idx_company_quality_time (company_id, quality_score, create_time)"
  32. };
  33. try (Connection c = DriverManager.getConnection(url, user, pass); Statement st = c.createStatement()) {
  34. for (String sql : sqls) {
  35. try {
  36. st.execute(sql);
  37. System.out.println("[OK] " + sql.substring(0, Math.min(60, sql.length())) + "...");
  38. } catch (Exception e) {
  39. String msg = e.getMessage() != null ? e.getMessage() : "";
  40. if (msg.contains("Duplicate key name") || msg.contains("1061")) {
  41. System.out.println("[SKIP] index exists");
  42. } else {
  43. System.err.println("[WARN] " + msg);
  44. }
  45. }
  46. }
  47. }
  48. System.out.println("Done " + db);
  49. }
  50. }