-- 待办首页列表排序索引（用于已存在的 sys_todo_task 表）
-- MySQL 5.7 兼容的幂等写法。

SET @todoIndexExists := (
  SELECT count(1)
  FROM information_schema.statistics
  WHERE table_schema = database()
    AND table_name = 'sys_todo_task'
    AND index_name = 'idx_todo_user_active_sort'
);

SET @todoIndexSql := IF(
  @todoIndexExists = 0,
  'ALTER TABLE sys_todo_task ADD INDEX idx_todo_user_active_sort (assignee_user_id, del_flag, task_status, priority, create_time, todo_id)',
  'SELECT ''idx_todo_user_active_sort already exists'''
);

PREPARE todoIndexStatement FROM @todoIndexSql;
EXECUTE todoIndexStatement;
DEALLOCATE PREPARE todoIndexStatement;
