diff --git a/src/main/resources/db/migration/V20251020__core_attendance_record.sql b/src/main/resources/db/migration/V20251020__core_attendance_record.sql deleted file mode 100644 index fc56172..0000000 --- a/src/main/resources/db/migration/V20251020__core_attendance_record.sql +++ /dev/null @@ -1,165 +0,0 @@ --- 0) 트랜잭션 (선택) -BEGIN; - --- 1) meetings 테이블 신설 (날짜=PK) -CREATE TABLE IF NOT EXISTS public.meetings -( - meeting_date DATE PRIMARY KEY, - created_at TIMESTAMPTZ NOT NULL DEFAULT NOW() -); - --- 2) attendance_records 구조 보강/정렬 --- 2-1) id 컬럼(IDENTITY) 없으면 추가 -DO -$$ - BEGIN - IF NOT EXISTS (SELECT 1 - FROM information_schema.columns - WHERE table_schema = 'public' - AND table_name = 'attendance_records' - AND column_name = 'id') THEN - ALTER TABLE public.attendance_records - ADD COLUMN id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY; - END IF; - END -$$; - --- 2-2) meeting_date 컬럼 추가 또는 date→meeting_date로 리네임 -DO -$$ - BEGIN - IF EXISTS (SELECT 1 - FROM information_schema.columns - WHERE table_schema = 'public' - AND table_name = 'attendance_records' - AND column_name = 'date') AND NOT EXISTS (SELECT 1 - FROM information_schema.columns - WHERE table_schema = 'public' - AND table_name = 'attendance_records' - AND column_name = 'meeting_date') THEN - ALTER TABLE public.attendance_records - RENAME COLUMN "date" TO meeting_date; - END IF; - - IF NOT EXISTS (SELECT 1 - FROM information_schema.columns - WHERE table_schema = 'public' - AND table_name = 'attendance_records' - AND column_name = 'meeting_date') THEN - ALTER TABLE public.attendance_records - ADD COLUMN meeting_date DATE NOT NULL DEFAULT CURRENT_DATE; - -- 필요 시 DEFAULT 제거 - ALTER TABLE public.attendance_records - ALTER COLUMN meeting_date DROP DEFAULT; - END IF; - END -$$; - --- 2-3) present 컬럼 없으면 추가 -DO -$$ - BEGIN - IF NOT EXISTS (SELECT 1 - FROM information_schema.columns - WHERE table_schema = 'public' - AND table_name = 'attendance_records' - AND column_name = 'present') THEN - ALTER TABLE public.attendance_records - ADD COLUMN present BOOLEAN NOT NULL DEFAULT FALSE; - END IF; - END -$$; - --- 2-4) updated_at / updated_by 컬럼 추가 -DO -$$ - BEGIN - IF NOT EXISTS (SELECT 1 - FROM information_schema.columns - WHERE table_schema = 'public' - AND table_name = 'attendance_records' - AND column_name = 'updated_at') THEN - ALTER TABLE public.attendance_records - ADD COLUMN updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(); - END IF; - - IF NOT EXISTS (SELECT 1 - FROM information_schema.columns - WHERE table_schema = 'public' - AND table_name = 'attendance_records' - AND column_name = 'updated_by') THEN - ALTER TABLE public.attendance_records - ADD COLUMN updated_by BIGINT NULL; - END IF; - END -$$; - --- 2-5) user_id FK 보강 (이미 있으면 스킵) -DO -$$ - BEGIN - IF NOT EXISTS (SELECT 1 - FROM information_schema.constraint_column_usage - WHERE table_name = 'attendance_records' - AND constraint_name = 'fk_attendance_user') THEN - ALTER TABLE public.attendance_records - ADD CONSTRAINT fk_attendance_user - FOREIGN KEY (user_id) REFERENCES public.users (id) ON DELETE CASCADE; - END IF; - END -$$; - --- 2-6) meeting_date FK (meetings) 보강 -DO -$$ - BEGIN - IF NOT EXISTS (SELECT 1 - FROM information_schema.constraint_column_usage - WHERE table_name = 'attendance_records' - AND constraint_name = 'fk_attendance_meeting') THEN - ALTER TABLE public.attendance_records - ADD CONSTRAINT fk_attendance_meeting - FOREIGN KEY (meeting_date) REFERENCES public.meetings (meeting_date) ON DELETE CASCADE; - END IF; - END -$$; - --- 2-7) UNIQUE(meeting_date, user_id) 재정의 -DO -$$ - DECLARE - c TEXT; - BEGIN - FOR c IN - SELECT conname - FROM pg_constraint - WHERE conrelid = 'public.attendance_records'::regclass - AND contype = 'u' - LOOP - EXECUTE format('ALTER TABLE public.attendance_records DROP CONSTRAINT %I', c); - END LOOP; - - ALTER TABLE public.attendance_records - ADD CONSTRAINT uq_attendance UNIQUE (meeting_date, user_id); - END -$$; - --- 3) 인덱스들 -DO -$$ - BEGIN - IF NOT EXISTS (SELECT 1 FROM pg_class WHERE relname = 'idx_attendance_user_date') THEN - CREATE INDEX idx_attendance_user_date ON public.attendance_records (user_id, meeting_date DESC); - END IF; - - IF NOT EXISTS (SELECT 1 FROM pg_class WHERE relname = 'idx_attendance_date_only') THEN - CREATE INDEX idx_attendance_date_only ON public.attendance_records (meeting_date DESC); - END IF; - - IF NOT EXISTS (SELECT 1 FROM pg_class WHERE relname = 'idx_users_team_role') THEN - CREATE INDEX idx_users_team_role ON public.users (team, user_role); - END IF; - END -$$; - -COMMIT; \ No newline at end of file diff --git a/src/main/resources/db/migration/V20251021__user_role_and_team_update.sql b/src/main/resources/db/migration/V20251021__user_role_and_team_update.sql deleted file mode 100644 index 0b0e325..0000000 --- a/src/main/resources/db/migration/V20251021__user_role_and_team_update.sql +++ /dev/null @@ -1,60 +0,0 @@ --- 1) users.user_role 관련 기존 CHECK 제약 전부 제거 -DO $$ DECLARE r record; -BEGIN FOR r IN -SELECT conname -FROM pg_constraint c - JOIN pg_class t ON t.oid = c.conrelid - JOIN pg_namespace n ON n.oid = t.relnamespace -WHERE t.relname = 'users' - AND n.nspname = 'public' - AND c.contype = 'c' - AND pg_get_constraintdef(c.oid) ILIKE '%user_role%' - LOOP -EXECUTE format('ALTER TABLE public.users DROP CONSTRAINT %I', r.conname); -END LOOP; -END $$; - --- 2) user_role 기본값 제거 후 타입을 VARCHAR(32)로 통일 -ALTER TABLE public.users - ALTER COLUMN user_role DROP DEFAULT; -ALTER TABLE public.users - ALTER COLUMN user_role TYPE varchar (32) - USING user_role:: text; - --- 3) user_role 기본값 및 새로운 CHECK 제약 생성 -ALTER TABLE public.users - ALTER COLUMN user_role SET DEFAULT 'GUEST'; - -DO $$ -BEGIN IF NOT EXISTS ( - SELECT 1 - FROM pg_constraint - WHERE conname = 'users_user_role_check' - AND conrelid = 'public.users'::regclass - ) THEN -ALTER TABLE public.users - ADD CONSTRAINT users_user_role_check - CHECK (user_role IN ('GUEST', 'MEMBER', 'CORE', 'LEAD', 'ORGANIZER', 'ADMIN')); -END IF; -END $$; - --- 4) 팀 컬럼 추가(널 허용). 값은 enum name 저장: 'HR','PR_DESIGN','TECH','BD' -ALTER TABLE public.users - ADD COLUMN IF NOT EXISTS team varchar (32); - --- 5) 팀 CHECK 제약 (NULL 허용) -DO $$ -BEGIN IF NOT EXISTS ( - SELECT 1 - FROM pg_constraint - WHERE conname = 'users_team_check' - AND conrelid = 'public.users'::regclass - ) THEN -ALTER TABLE public.users - ADD CONSTRAINT users_team_check - CHECK ( - team IS NULL - OR team IN ('HR', 'PR_DESIGN', 'TECH', 'BD') - ); -END IF; -END $$; \ No newline at end of file