diff --git a/backend/mci-server/src/main/resources/db/migration/V15__add_range_partitions.sql b/backend/mci-server/src/main/resources/db/migration/V15__add_range_partitions.sql index abf76b8..eff80a0 100644 --- a/backend/mci-server/src/main/resources/db/migration/V15__add_range_partitions.sql +++ b/backend/mci-server/src/main/resources/db/migration/V15__add_range_partitions.sql @@ -1,5 +1,11 @@ -- Phase 1.5: 为高频表添加 MySQL RANGE 月分区(分区键 create_time) -- 架构设计文档:docs/architecture/2026-07-06-message-customer-interaction-design.md 第 2.6.2 节 +-- +-- 生产部署注意: +-- 1. 本迁移对主键/唯一键和外键做了破坏性调整,上线前请先在预发布环境用真实数据量验证。 +-- 2. 大表(message_event / send_record)执行 DROP/ADD PRIMARY KEY 可能锁表,建议使用 pt-online-schema-change +-- 或预留低峰窗口;当前脚本为开发/MVP 阶段使用。 +-- 3. 未提供 down migration:取消分区需要重建表并恢复外键,操作复杂,请在上线前自行备份 schema。 -- ============================ -- 1. mci_message_event(消息事件表) diff --git a/backend/mci-server/src/main/resources/db/migration/V16__add_conversation_tenant_time_index.sql b/backend/mci-server/src/main/resources/db/migration/V16__add_conversation_tenant_time_index.sql new file mode 100644 index 0000000..9ecc74e --- /dev/null +++ b/backend/mci-server/src/main/resources/db/migration/V16__add_conversation_tenant_time_index.sql @@ -0,0 +1,4 @@ +-- Phase 1.5 补充:为 mci_conversation 添加架构设计要求的 (tenant_code, create_time) 索引 +-- 注意:V15 中为了满足 MySQL 分区规则,唯一键已包含 create_time,导致唯一性约束弱化; +-- 业务上应通过 event_id / task_id 等自然业务键保证唯一性。 +ALTER TABLE mci_conversation ADD KEY idx_tenant_time (tenant_code, create_time); diff --git a/scripts/extend-mysql-partitions.sh b/scripts/extend-mysql-partitions.sh index e711f9c..b9fe59f 100755 --- a/scripts/extend-mysql-partitions.sh +++ b/scripts/extend-mysql-partitions.sh @@ -17,6 +17,7 @@ MYSQL_USER="${MYSQL_USER:-root}" MYSQL_PASSWORD="${MYSQL_PASSWORD:-root}" MYSQL_DB="${MYSQL_DB:-sino_mci}" MONTHS_AHEAD="${MONTHS_AHEAD:-3}" +DRY_RUN="${DRY_RUN:-0}" TABLES=( mci_message_event @@ -24,12 +25,21 @@ TABLES=( mci_conversation ) +if ! command -v mysql >/dev/null 2>&1; then + echo "Error: mysql client not found in PATH" >&2 + exit 1 +fi + echo "Extending monthly partitions for database: ${MYSQL_DB}" echo "Host: ${MYSQL_HOST}:${MYSQL_PORT}, User: ${MYSQL_USER}, Months ahead: ${MONTHS_AHEAD}" for table in "${TABLES[@]}"; do echo " -> ${table}" - mysql -h "${MYSQL_HOST}" -P "${MYSQL_PORT}" -u "${MYSQL_USER}" -p"${MYSQL_PASSWORD}" "${MYSQL_DB}" \ + if [ "${DRY_RUN}" = "1" ]; then + echo " CALL ExtendMonthlyPartitions('${table}', ${MONTHS_AHEAD});" + continue + fi + MYSQL_PWD="${MYSQL_PASSWORD}" mysql -h "${MYSQL_HOST}" -P "${MYSQL_PORT}" -u "${MYSQL_USER}" "${MYSQL_DB}" \ -e "CALL ExtendMonthlyPartitions('${table}', ${MONTHS_AHEAD});" done