From 4dbc190f687747a56020e2c6ef31a8b1ff8180d1 Mon Sep 17 00:00:00 2001 From: marsal Date: Tue, 7 Jul 2026 18:50:26 +0800 Subject: [PATCH] =?UTF-8?q?fix(db):=20V16=20=E8=A1=A5=E5=85=85=20conversat?= =?UTF-8?q?ion=20=E7=A7=9F=E6=88=B7=E6=97=B6=E9=97=B4=E7=B4=A2=E5=BC=95?= =?UTF-8?q?=EF=BC=8C=E8=84=9A=E6=9C=AC=E4=BD=BF=E7=94=A8=20MYSQL=5FPWD=20?= =?UTF-8?q?=E9=81=BF=E5=85=8D=E5=AF=86=E7=A0=81=E6=9A=B4=E9=9C=B2=EF=BC=8C?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=94=9F=E4=BA=A7=E9=83=A8=E7=BD=B2=E6=8F=90?= =?UTF-8?q?=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../db/migration/V15__add_range_partitions.sql | 6 ++++++ .../V16__add_conversation_tenant_time_index.sql | 4 ++++ scripts/extend-mysql-partitions.sh | 12 +++++++++++- 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 backend/mci-server/src/main/resources/db/migration/V16__add_conversation_tenant_time_index.sql 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