ApplyCustomerProfileDomainMigration.java 4.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.Statement;
  5. /**
  6. * Apply customer profile domain tables (V20260701_01) to tenant DB(s).
  7. */
  8. public class ApplyCustomerProfileDomainMigration {
  9. private static final String HOST = "cq-cdb-8fjmemkb.sql.tencentcdb.com:27220";
  10. private static final String USER = "root";
  11. private static final String PWD = "Ylrz_1q2w3e4r5t6y";
  12. private static final String IDENTITY_SQL =
  13. "CREATE TABLE IF NOT EXISTS `customer_profile_identity` ("
  14. + "`id` BIGINT NOT NULL AUTO_INCREMENT,"
  15. + "`company_id` BIGINT NOT NULL,"
  16. + "`channel_type` VARCHAR(8) NOT NULL,"
  17. + "`profile_key` VARCHAR(128) NOT NULL,"
  18. + "`customer_id` BIGINT DEFAULT NULL,"
  19. + "`shard_id` INT NOT NULL,"
  20. + "`create_time` DATETIME DEFAULT CURRENT_TIMESTAMP,"
  21. + "`update_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,"
  22. + "PRIMARY KEY (`id`),"
  23. + "UNIQUE KEY `uk_identity` (`company_id`, `channel_type`, `profile_key`),"
  24. + "KEY `idx_customer` (`company_id`, `customer_id`),"
  25. + "KEY `idx_shard` (`company_id`, `shard_id`)"
  26. + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";
  27. public static void main(String[] args) throws Exception {
  28. String[] dbs = args.length > 0 ? args : new String[]{"fs_tenant_cs1"};
  29. for (String db : dbs) {
  30. apply(db);
  31. }
  32. }
  33. private static void apply(String db) throws Exception {
  34. String url = "jdbc:mysql://" + HOST + "/" + db
  35. + "?useSSL=false&serverTimezone=GMT%2B8&allowMultiQueries=true";
  36. try (Connection c = DriverManager.getConnection(url, USER, PWD);
  37. Statement st = c.createStatement()) {
  38. st.execute(IDENTITY_SQL);
  39. for (int i = 0; i < 32; i++) {
  40. st.execute(projectionSql(i));
  41. st.execute(eventSql(i));
  42. }
  43. try (ResultSet rs = st.executeQuery(
  44. "SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA=DATABASE()"
  45. + " AND TABLE_NAME LIKE 'customer_profile_%'")) {
  46. rs.next();
  47. System.out.println(db + " customer_profile_* tables: " + rs.getInt(1));
  48. }
  49. }
  50. }
  51. private static String projectionSql(int shard) {
  52. return "CREATE TABLE IF NOT EXISTS `customer_profile_projection_" + shard + "` ("
  53. + "`id` BIGINT NOT NULL AUTO_INCREMENT,"
  54. + "`company_id` BIGINT NOT NULL,"
  55. + "`channel_type` VARCHAR(8) NOT NULL,"
  56. + "`profile_key` VARCHAR(128) NOT NULL,"
  57. + "`customer_id` BIGINT DEFAULT NULL,"
  58. + "`shard_id` INT NOT NULL,"
  59. + "`nickname` VARCHAR(128) DEFAULT NULL,"
  60. + "`lifecycle_stage` VARCHAR(32) DEFAULT 'NEW',"
  61. + "`value_score` INT DEFAULT 0,"
  62. + "`total_purchase` DECIMAL(14,2) DEFAULT 0,"
  63. + "`interaction_count` INT DEFAULT 0,"
  64. + "`last_active_time` DATETIME DEFAULT NULL,"
  65. + "`internal_tags` VARCHAR(1024) DEFAULT NULL,"
  66. + "`current_state` VARCHAR(64) DEFAULT NULL,"
  67. + "`profile_ext` JSON DEFAULT NULL,"
  68. + "`create_time` DATETIME DEFAULT CURRENT_TIMESTAMP,"
  69. + "`update_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,"
  70. + "PRIMARY KEY (`id`),"
  71. + "UNIQUE KEY `uk_profile` (`company_id`, `channel_type`, `profile_key`),"
  72. + "KEY `idx_customer` (`company_id`, `customer_id`),"
  73. + "KEY `idx_active` (`company_id`, `last_active_time`)"
  74. + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";
  75. }
  76. private static String eventSql(int shard) {
  77. return "CREATE TABLE IF NOT EXISTS `customer_profile_event_" + shard + "` ("
  78. + "`id` BIGINT NOT NULL AUTO_INCREMENT,"
  79. + "`company_id` BIGINT NOT NULL,"
  80. + "`channel_type` VARCHAR(8) NOT NULL,"
  81. + "`profile_key` VARCHAR(128) NOT NULL,"
  82. + "`source` VARCHAR(64) NOT NULL,"
  83. + "`source_version` VARCHAR(128) NOT NULL,"
  84. + "`event_payload` JSON DEFAULT NULL,"
  85. + "`create_time` DATETIME DEFAULT CURRENT_TIMESTAMP,"
  86. + "PRIMARY KEY (`id`),"
  87. + "UNIQUE KEY `uk_event` (`company_id`, `channel_type`, `profile_key`, `source`, `source_version`),"
  88. + "KEY `idx_profile_time` (`company_id`, `channel_type`, `profile_key`, `create_time`)"
  89. + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";
  90. }
  91. }