-- ============================================================
-- M-BOM 운영 sample 데이터 → RPS 이관
-- 운영: 211.115.91.141:11133/waceplm  (mbom_header 3건, mbom_detail 95건)
-- 대상: 211.115.91.141:11134/vexplor_rps
--
-- 함정:
--   1) mbom_header.source_ebom_objid → RPS part_bom_report 에 없는 OBJID 는 NULL 처리
--   2) mbom_detail.part_objid varchar → RPS bigint (FK part_mng.objid bigint 호환)
--   3) RPS part_mng 에 없는 part_objid 도 NULL 처리
--
-- 실행 전: /tmp/mbom_header.csv, /tmp/mbom_detail.csv 준비 (운영DB \copy TO)
-- ============================================================

-- ── mbom_header ───────────────────────────────────────────────
DROP TABLE IF EXISTS mbom_header_stage;
CREATE TABLE mbom_header_stage (LIKE mbom_header INCLUDING DEFAULTS);
ALTER TABLE mbom_header_stage DROP CONSTRAINT IF EXISTS mbom_header_pkey;
ALTER TABLE mbom_header_stage DROP CONSTRAINT IF EXISTS mbom_header_mbom_no_key;
ALTER TABLE mbom_header_stage DROP CONSTRAINT IF EXISTS fk_mbom_source_ebom;
ALTER TABLE mbom_header_stage DROP CONSTRAINT IF EXISTS fk_mbom_source_mbom;

\copy mbom_header_stage FROM '/tmp/mbom_header.csv' WITH CSV HEADER

-- RPS part_bom_report 에 없는 source_ebom_objid 는 NULL
UPDATE mbom_header_stage SET source_ebom_objid = NULL
 WHERE source_ebom_objid IS NOT NULL
   AND NOT EXISTS (SELECT 1 FROM part_bom_report WHERE objid = mbom_header_stage.source_ebom_objid);

-- self FK source_mbom_objid: 자기 자신 OBJID 셋에 있으면 OK, 아니면 NULL
UPDATE mbom_header_stage SET source_mbom_objid = NULL
 WHERE source_mbom_objid IS NOT NULL
   AND source_mbom_objid NOT IN (SELECT objid FROM mbom_header_stage);

INSERT INTO mbom_header SELECT * FROM mbom_header_stage;
DROP TABLE mbom_header_stage;

-- ── mbom_detail ────────────────────────────────────────────────
DROP TABLE IF EXISTS mbom_detail_stage;
CREATE TABLE mbom_detail_stage (LIKE mbom_detail INCLUDING DEFAULTS);
ALTER TABLE mbom_detail_stage DROP CONSTRAINT IF EXISTS mbom_detail_pkey;
ALTER TABLE mbom_detail_stage DROP CONSTRAINT IF EXISTS fk_mbom_detail_header;
ALTER TABLE mbom_detail_stage DROP CONSTRAINT IF EXISTS fk_mbom_detail_part;
ALTER TABLE mbom_detail_stage ALTER COLUMN part_objid TYPE varchar(64) USING part_objid::text;

\copy mbom_detail_stage FROM '/tmp/mbom_detail.csv' WITH CSV HEADER

-- RPS part_mng 에 없는 part_objid 는 NULL (bigint 형변환 안전)
UPDATE mbom_detail_stage SET part_objid = NULL
 WHERE part_objid IS NOT NULL
   AND NOT EXISTS (SELECT 1 FROM part_mng WHERE objid::text = mbom_detail_stage.part_objid);

-- RPS mbom_header 에 없는 mbom_header_objid 행은 import 제외
DELETE FROM mbom_detail_stage
 WHERE NOT EXISTS (SELECT 1 FROM mbom_header WHERE objid = mbom_detail_stage.mbom_header_objid);

-- bigint 형변환 후 본 테이블 INSERT
ALTER TABLE mbom_detail_stage ALTER COLUMN part_objid TYPE bigint USING part_objid::bigint;
INSERT INTO mbom_detail SELECT * FROM mbom_detail_stage;
DROP TABLE mbom_detail_stage;

-- ── 결과 ──────────────────────────────────────────────────────
SELECT 'mbom_header' AS t, COUNT(*) FROM mbom_header
UNION ALL SELECT 'mbom_detail', COUNT(*) FROM mbom_detail;
