- 00-init.sql: 구 01(도메인)+02(learning)+05(anchoring) 통합, 구 04(누적 ALTER)는 01에 기반영되어 폐기 - temp-data.sql: 구 03 시드 + 협상 카드 시드(일반 11장·와일드 5장, 멱등 가드, available=TRUE) - card 테이블: script TEXT 전환 + tone·strategy_type 컬럼 추가, 카드 변수 9종 체계 문서화 - 참조 갱신: 루트 README·docker-compose 주석, schedules/anchoring conftest(00-init 적용)·README·문서 - anchoring 테스트 20건 통과, 신규 DB 초기화·멱등성 검증 완료 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
113 lines
4.8 KiB
PL/PgSQL
113 lines
4.8 KiB
PL/PgSQL
-- ============================================================
|
|
-- 앵커링 이름 전면 개편 마이그레이션 (2026-07-06)
|
|
-- rate_adjustments → adjustments (+컬럼 6종)
|
|
-- sessions 앵커링 컬럼 4종, 뷰 2종(rate_history/current_rates → value_history/current_values)
|
|
-- 대상: 구 이름 스키마가 이미 적용된 기존 DB (신규 DB 는 postgres-init/01~05 적용으로 충분)
|
|
-- 멱등: 각 rename 은 구 이름 존재 시에만 수행 — 재실행·부분 적용 상태에서도 안전
|
|
-- 적용: psql -h <host> -U <user> -d negosium_db -f migrations/20260706_rename_anchoring.sql
|
|
-- ============================================================
|
|
\connect negosium_db
|
|
|
|
BEGIN;
|
|
|
|
-- 1) 구 이름 뷰 제거 (신 이름 뷰는 마지막에 재생성 — 정의는 postgres-init/00-init.sql 과 동일)
|
|
DROP VIEW IF EXISTS anchoring.rate_history;
|
|
DROP VIEW IF EXISTS anchoring.current_rates;
|
|
|
|
-- 2) 테이블·시퀀스·인덱스 rename
|
|
DO $$
|
|
BEGIN
|
|
IF EXISTS (SELECT FROM information_schema.tables
|
|
WHERE table_schema = 'anchoring' AND table_name = 'rate_adjustments') THEN
|
|
ALTER TABLE anchoring.rate_adjustments RENAME TO adjustments;
|
|
END IF;
|
|
IF EXISTS (SELECT FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
WHERE n.nspname = 'anchoring' AND c.relname = 'rate_adjustments_id_seq') THEN
|
|
ALTER SEQUENCE anchoring.rate_adjustments_id_seq RENAME TO adjustments_adjustment_id_seq;
|
|
END IF;
|
|
IF EXISTS (SELECT FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
WHERE n.nspname = 'anchoring' AND c.relname = 'idx_rate_adjustments_cell') THEN
|
|
ALTER INDEX anchoring.idx_rate_adjustments_cell RENAME TO idx_adjustments_cell;
|
|
END IF;
|
|
-- 테이블 rename 은 PK 제약 이름을 바꾸지 않는다 — 함께 정리
|
|
IF EXISTS (SELECT FROM pg_constraint con JOIN pg_namespace n ON n.oid = con.connamespace
|
|
WHERE n.nspname = 'anchoring' AND con.conname = 'rate_adjustments_pkey') THEN
|
|
ALTER TABLE anchoring.adjustments RENAME CONSTRAINT rate_adjustments_pkey TO adjustments_pkey;
|
|
END IF;
|
|
END $$;
|
|
|
|
-- 3) adjustments 컬럼 rename
|
|
DO $$
|
|
DECLARE
|
|
r RECORD;
|
|
BEGIN
|
|
FOR r IN
|
|
SELECT * FROM (VALUES
|
|
('id', 'adjustment_id'),
|
|
('price_bracket_index', 'price_range_index'),
|
|
('nego_count', 'sample_count'),
|
|
('anchor_rate_before', 'anchoring_value_before'),
|
|
('anchor_rate_after', 'anchoring_value_after'),
|
|
('consumed_session_ids', 'used_session_ids')
|
|
) AS m(old_name, new_name)
|
|
LOOP
|
|
IF EXISTS (SELECT FROM information_schema.columns
|
|
WHERE table_schema = 'anchoring' AND table_name = 'adjustments'
|
|
AND column_name = r.old_name) THEN
|
|
EXECUTE format('ALTER TABLE anchoring.adjustments RENAME COLUMN %I TO %I',
|
|
r.old_name, r.new_name);
|
|
END IF;
|
|
END LOOP;
|
|
END $$;
|
|
|
|
-- 4) negotiation.sessions 앵커링 컬럼 rename
|
|
-- (부분 인덱스 idx_sessions_anchoring_pending 술어는 컬럼 rename 을 자동 추종 — 재생성 불필요)
|
|
DO $$
|
|
DECLARE
|
|
r RECORD;
|
|
BEGIN
|
|
FOR r IN
|
|
SELECT * FROM (VALUES
|
|
('target_anchoring_price', 'anchoring_price'),
|
|
('anchor_rate_permille', 'anchoring_value'),
|
|
('last_offered_price', 'last_offer_price'),
|
|
('anchoring_adjustment_id','used_by_adjustment_id')
|
|
) AS m(old_name, new_name)
|
|
LOOP
|
|
IF EXISTS (SELECT FROM information_schema.columns
|
|
WHERE table_schema = 'negotiation' AND table_name = 'sessions'
|
|
AND column_name = r.old_name) THEN
|
|
EXECUTE format('ALTER TABLE negotiation.sessions RENAME COLUMN %I TO %I',
|
|
r.old_name, r.new_name);
|
|
END IF;
|
|
END LOOP;
|
|
END $$;
|
|
|
|
-- 5) 신 이름 뷰 재생성 (postgres-init/00-init.sql §조회용 뷰와 동일 정의)
|
|
CREATE OR REPLACE VIEW anchoring.value_history AS
|
|
SELECT adjustment_id,
|
|
company_id,
|
|
supplier_type,
|
|
price_range_index,
|
|
anchoring_value_before,
|
|
anchoring_value_after,
|
|
anchoring_value_after - anchoring_value_before AS value_change,
|
|
sample_count,
|
|
success_count,
|
|
round(success_count::numeric / sample_count, 3) AS success_rate,
|
|
created_at
|
|
FROM anchoring.adjustments;
|
|
|
|
CREATE OR REPLACE VIEW anchoring.current_values AS
|
|
SELECT DISTINCT ON (company_id, supplier_type, price_range_index)
|
|
company_id,
|
|
supplier_type,
|
|
price_range_index,
|
|
anchoring_value_after AS anchoring_value,
|
|
adjustment_id AS last_adjustment_id,
|
|
created_at AS last_adjusted_at
|
|
FROM anchoring.adjustments
|
|
ORDER BY company_id, supplier_type, price_range_index, adjustment_id DESC;
|
|
|
|
COMMIT;
|