-- 为 rank 为 NULL 的菜单设置默认排序值
-- 按模块分组，模块内按 id 升序分配 rank

UPDATE t_menu t
JOIN (
  SELECT id,
    ROW_NUMBER() OVER (PARTITION BY parent_id ORDER BY id) AS new_rank
  FROM t_menu
  WHERE deleted = 0 AND `rank` IS NULL
) r ON t.id = r.id
SET t.`rank` = r.new_rank
WHERE t.`rank` IS NULL;
