-- TD-015 项目完成校验：按 project_id 数非终态子对象需要的索引
-- MySQL 无 CREATE INDEX IF NOT EXISTS，用 information_schema 守卫实现幂等（可重复执行）；与演示库补丁 docs/sql/patches/2026-06-05-TD015-完成校验-01.sql 写法一致

-- 需求表：原 idx_rdms_project_requirement_project_status_deleted 实际列为 (status_code, deleted)，缺 project_id 前导，补齐
SET @idx := (SELECT COUNT(*) FROM information_schema.STATISTICS
             WHERE TABLE_SCHEMA = DATABASE()
               AND TABLE_NAME = 'rdms_project_requirement'
               AND INDEX_NAME = 'idx_rdms_project_requirement_proj_status');
SET @sql := IF(@idx = 0,
    'CREATE INDEX idx_rdms_project_requirement_proj_status ON rdms_project_requirement (project_id, status_code, deleted)',
    'SELECT 1');
PREPARE s FROM @sql; EXECUTE s; DEALLOCATE PREPARE s;

-- 任务表（rdms_task）：与执行表 idx_rdms_pe_proj_status 对齐，让按项目数非终态任务走覆盖前缀
SET @idx := (SELECT COUNT(*) FROM information_schema.STATISTICS
             WHERE TABLE_SCHEMA = DATABASE()
               AND TABLE_NAME = 'rdms_task'
               AND INDEX_NAME = 'idx_rdms_task_proj_status');
SET @sql := IF(@idx = 0,
    'CREATE INDEX idx_rdms_task_proj_status ON rdms_task (project_id, status_code, deleted)',
    'SELECT 1');
PREPARE s FROM @sql; EXECUTE s; DEALLOCATE PREPARE s;

-- 执行表 rdms_project_execution 已有 idx_rdms_pe_proj_status(project_id, status_code, deleted)，无需新增
