-- =====================================================
-- 查找并修复 sys_menu 表中重复的 path 记录
-- PostgreSQL 版本
-- =====================================================

-- 第一步：查看是否有重复的 path
SELECT
    path,
    COUNT(*) as count,
    STRING_AGG(CAST(menu_id AS TEXT), ', ') as menu_ids,
    STRING_AGG(menu_name, ', ') as menu_names
FROM sys_menu
GROUP BY path
HAVING COUNT(*) > 1;

-- 第二步：查看具体重复记录的详细信息
-- (替换下面的重复路径值)
-- SELECT * FROM sys_menu WHERE path = 'your_duplicate_path_value' ORDER BY menu_id;

-- 第三步：自动删除重复的 path 记录（保留 menu_id 最小的）
DELETE FROM sys_menu
WHERE menu_id IN (
    SELECT menu_id FROM (
        SELECT menu_id,
               ROW_NUMBER() OVER (PARTITION BY path ORDER BY menu_id) as rn
        FROM sys_menu
    ) t
    WHERE rn > 1
);

-- 第四步：验证是否还有重复的 path
SELECT
    path,
    COUNT(*) as count
FROM sys_menu
GROUP BY path
HAVING COUNT(*) > 1;
