瀏覽代碼

fix(ai-terminal): add migration guard for ai_device_event.project_id backward compat

Uses information_schema pre-check + PREPARE/EXECUTE for environments
that already ran the old CREATE TABLE without project_id. Also adds
idx_project index with the same guarded pattern.
WangKang 1 周之前
父節點
當前提交
8751c5279b
共有 1 個文件被更改,包括 32 次插入0 次删除
  1. 32 0
      script/sql/ai-terminal-mvp.sql

+ 32 - 0
script/sql/ai-terminal-mvp.sql

@@ -271,3 +271,35 @@ INSERT IGNORE INTO ai_device_scene_binding(device_id, scene_code, ui_template, a
 VALUES ('EMOON-KIOSK-001', 'outpatient_kiosk', 'kiosk_home_v1',
         JSON_OBJECT('defaultAgent','opd-guide-agent','allowedAgents',JSON_ARRAY('opd-guide-agent','opd-triage-agent','opd-registration-agent','tongue-diagnosis-agent')),
         JSON_ARRAY('department-selection','doctor-selection','time-slot-selection','confirm-appointment','appointment-success','tongue-capture','tongue-diagnosis-result'));
+
+-- -----------------------------------------------------------
+-- 迁移补丁:为已执行旧版脚本的环境补 project_id
+-- 如果列不存在则添加,存在则跳过(MySQL 5.7+ 不支持 IF NOT EXISTS 语法,
+-- 使用 information_schema 预检)
+-- -----------------------------------------------------------
+SET @col_exists = (SELECT COUNT(*) FROM information_schema.COLUMNS
+    WHERE TABLE_SCHEMA = DATABASE()
+      AND TABLE_NAME = 'ai_device_event'
+      AND COLUMN_NAME = 'project_id');
+
+SET @sql = IF(@col_exists = 0,
+    'ALTER TABLE ai_device_event ADD COLUMN project_id VARCHAR(64) NOT NULL DEFAULT '''' COMMENT ''项目标识'' AFTER device_id',
+    'SELECT ''ai_device_event.project_id already exists, skipping'' AS msg');
+
+PREPARE stmt FROM @sql;
+EXECUTE stmt;
+DEALLOCATE PREPARE stmt;
+
+-- 补索引(幂等:MySQL 8.0+ CREATE INDEX IF NOT EXISTS,低版本靠 PREPARE 预检)
+SET @idx_exists = (SELECT COUNT(*) FROM information_schema.STATISTICS
+    WHERE TABLE_SCHEMA = DATABASE()
+      AND TABLE_NAME = 'ai_device_event'
+      AND INDEX_NAME = 'idx_project');
+
+SET @sql_idx = IF(@idx_exists = 0,
+    'CREATE INDEX idx_project ON ai_device_event(project_id)',
+    'SELECT ''ai_device_event.idx_project already exists, skipping'' AS msg');
+
+PREPARE stmt_idx FROM @sql_idx;
+EXECUTE stmt_idx;
+DEALLOCATE PREPARE stmt_idx;