- postgres-init/05-anchoring-schema.sql 신설(02-learning-schema 스타일) — anchoring 스키마· adjustments·인덱스 2종(부분 인덱스 포함)·뷰 2종. sessions 컬럼은 01/04 소관으로 명시 - 루트 docker-compose.yml 에 anchoring + anchoring-redis 서비스 추가(기존 스타일), 헤더 서비스 목록·DB 준비 절차 갱신, 모듈 compose 의 로그 로테이션(10MB×5) 이관 - 모듈 schema.sql·docker-compose.yml 삭제 — DDL 단일 원본은 postgres-init/05 - tests/conftest 부트스트랩 경로를 05 로 교체, 문서 4종·주석 포인터 갱신 (인수인계의 낡은 'negodata 가 redis 참조' 문구도 무Redis 현실로 교정) - 검증: 모듈 20·negodata 50 테스트 통과, docker compose config 정상 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/05-anchoring-schema.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/05-anchoring-schema.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;
|