-- 기존 DB: site 메뉴 "기본정보관리" 아래 "기본코드관리" 하위 메뉴 추가 (bag/code-kinds)
-- idempotent: 동일 링크가 이미 있으면 추가하지 않음

INSERT INTO `menu` (`mt_idx`, `lg_idx`, `mm_name`, `mm_link`, `mm_pidx`, `mm_dep`, `mm_num`, `mm_cnode`, `mm_level`, `mm_is_view`)
SELECT m.mt_idx, m.lg_idx, '기본코드관리', 'bag/code-kinds', m.mm_idx, 1, 0, 0, '', 'Y'
FROM `menu` m
JOIN `menu_type` t ON t.mt_idx = m.mt_idx AND t.mt_code = 'site'
WHERE m.mm_link = 'bag/basic-info' AND m.mm_pidx = 0
  AND NOT EXISTS (
    SELECT 1 FROM `menu` c
    WHERE c.mt_idx = m.mt_idx AND c.lg_idx = m.lg_idx AND c.mm_link = 'bag/code-kinds'
  );

UPDATE `menu` m
JOIN `menu_type` t ON t.mt_idx = m.mt_idx AND t.mt_code = 'site'
SET m.mm_cnode = (
  SELECT COUNT(*) FROM `menu` c WHERE c.mm_pidx = m.mm_idx AND c.mt_idx = m.mt_idx AND c.lg_idx = m.lg_idx
)
WHERE m.mm_link = 'bag/basic-info' AND m.mm_pidx = 0;
