Files
tk-factory-services/system3-nonconformance/api/migrations/014_add_user_department.sql
Hyungi Ahn 550633b89d feat: 3-System 분리 프로젝트 초기 코드 작성
TK-FB(공장관리+신고)와 M-Project(부적합관리)를 3개 독립 시스템으로
분리하기 위한 전체 코드 구조 작성.
- SSO 인증 서비스 (bcrypt + pbkdf2 이중 해시 지원)
- System 1: 공장관리 (TK-FB 기반, 신고 코드 제거)
- System 2: 신고 (TK-FB에서 workIssue 코드 추출)
- System 3: 부적합관리 (M-Project 기반)
- Gateway 포털 (path-based 라우팅)
- 통합 docker-compose.yml 및 배포 스크립트

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-09 14:40:11 +09:00

93 lines
4.1 KiB
PL/PgSQL
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- 014_add_user_department.sql
-- 사용자 부서 정보 추가
BEGIN;
-- migration_log 테이블 생성 (멱등성)
CREATE TABLE IF NOT EXISTS migration_log (
id SERIAL PRIMARY KEY,
migration_file VARCHAR(255) NOT NULL UNIQUE,
executed_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
status VARCHAR(50),
notes TEXT,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- 마이그레이션 파일 이름
DO $$
DECLARE
migration_name VARCHAR(255) := '014_add_user_department.sql';
migration_notes TEXT := '사용자 부서 정보 추가: department ENUM 타입 및 users 테이블에 department 컬럼 추가';
current_status VARCHAR(50);
BEGIN
SELECT status INTO current_status FROM migration_log WHERE migration_file = migration_name;
IF current_status IS NULL THEN
RAISE NOTICE '--- 마이그레이션 % 시작 ---', migration_name;
-- department ENUM 타입 생성
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'department_type') THEN
CREATE TYPE department_type AS ENUM (
'production', -- 생산
'quality', -- 품질
'purchasing', -- 구매
'design', -- 설계
'sales' -- 영업
);
RAISE NOTICE '✅ department_type ENUM 타입이 생성되었습니다.';
ELSE
RAISE NOTICE ' department_type ENUM 타입이 이미 존재합니다.';
END IF;
-- users 테이블에 department 컬럼 추가
IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'users' AND column_name = 'department') THEN
ALTER TABLE users ADD COLUMN department department_type;
RAISE NOTICE '✅ users.department 컬럼이 추가되었습니다.';
ELSE
RAISE NOTICE ' users.department 컬럼이 이미 존재합니다.';
END IF;
-- 인덱스 추가 (부서별 조회 성능 향상)
IF NOT EXISTS (SELECT 1 FROM pg_indexes WHERE tablename = 'users' AND indexname = 'idx_users_department') THEN
CREATE INDEX idx_users_department ON users (department) WHERE department IS NOT NULL;
RAISE NOTICE '✅ idx_users_department 인덱스가 생성되었습니다.';
ELSE
RAISE NOTICE ' idx_users_department 인덱스가 이미 존재합니다.';
END IF;
-- 마이그레이션 검증
DECLARE
col_count INTEGER;
enum_count INTEGER;
idx_count INTEGER;
BEGIN
SELECT COUNT(*) INTO col_count FROM information_schema.columns WHERE table_name = 'users' AND column_name = 'department';
SELECT COUNT(*) INTO enum_count FROM pg_type WHERE typname = 'department_type';
SELECT COUNT(*) INTO idx_count FROM pg_indexes WHERE tablename = 'users' AND indexname = 'idx_users_department';
RAISE NOTICE '=== 마이그레이션 검증 결과 ===';
RAISE NOTICE '추가된 컬럼: %/1개', col_count;
RAISE NOTICE '생성된 ENUM: %/1개', enum_count;
RAISE NOTICE '생성된 인덱스: %/1개', idx_count;
IF col_count = 1 AND enum_count = 1 AND idx_count = 1 THEN
RAISE NOTICE '✅ 마이그레이션이 성공적으로 완료되었습니다!';
INSERT INTO migration_log (migration_file, status, notes) VALUES (migration_name, 'SUCCESS', migration_notes);
ELSE
RAISE EXCEPTION '❌ 마이그레이션 검증 실패!';
END IF;
END;
-- 부서 ENUM 값 확인
RAISE NOTICE '=== 부서 ENUM 값 ===';
PERFORM dblink_exec('dbname=' || current_database(), 'SELECT enumlabel FROM pg_enum WHERE enumtypid = ''department_type''::regtype ORDER BY enumsortorder');
ELSIF current_status = 'SUCCESS' THEN
RAISE NOTICE ' 마이그레이션 %는 이미 성공적으로 실행되었습니다. 스킵합니다.', migration_name;
ELSE
RAISE NOTICE '⚠️ 마이그레이션 %는 이전에 실패했습니다. 수동 확인이 필요합니다.', migration_name;
END IF;
END $$;
COMMIT;