47 lines
1.7 KiB
Python
47 lines
1.7 KiB
Python
"""Add the conversation table already specified by docs/02 section 8.1.
|
|
|
|
Only CREATE TABLE; existing tables and columns are unchanged.
|
|
"""
|
|
from alembic import op
|
|
|
|
revision = "20260909_session"
|
|
down_revision = "20260909_outbox_delivery"
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
SESSION_DDL = """CREATE TABLE svc_conversation_session (
|
|
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
|
|
session_id VARCHAR(64) NOT NULL,
|
|
user_id BIGINT UNSIGNED NOT NULL,
|
|
portal VARCHAR(32) NOT NULL,
|
|
agent_type VARCHAR(32) NULL,
|
|
status VARCHAR(16) NOT NULL DEFAULT 'active',
|
|
clarification_round TINYINT UNSIGNED NOT NULL DEFAULT 0,
|
|
message_count INT UNSIGNED NOT NULL DEFAULT 0,
|
|
last_intent VARCHAR(32) NULL,
|
|
config_version VARCHAR(64) NULL,
|
|
started_at DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
|
|
last_active_at DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
|
|
ended_at DATETIME(6) NULL,
|
|
created_at DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
|
|
updated_at DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6)
|
|
ON UPDATE CURRENT_TIMESTAMP(6),
|
|
PRIMARY KEY (id),
|
|
UNIQUE KEY uk_session_id (session_id),
|
|
KEY idx_session_user_active (user_id, last_active_at),
|
|
KEY idx_session_status_active (status, last_active_at),
|
|
CONSTRAINT fk_session_user FOREIGN KEY (user_id) REFERENCES sys_user(id),
|
|
CONSTRAINT chk_session_status
|
|
CHECK (status IN ('active', 'ended', 'transferred', 'expired')),
|
|
CONSTRAINT chk_session_clarification CHECK (clarification_round <= 10)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
|
|
COMMENT='Agent会话状态与澄清轮次';"""
|
|
|
|
|
|
def upgrade() -> None:
|
|
op.execute(SESSION_DDL)
|
|
|
|
|
|
def downgrade() -> None:
|
|
raise RuntimeError("会话数据禁止自动删除,请使用受控的前向兼容迁移")
|