Appearance
15. supabase-withdraw-account.sql
원본 파일: 'C:\Repository\loafacto-hub\docs\web-ui\15. supabase-withdraw-account.sql'
sql
-- =============================================================================
-- 회원 탈퇴: profiles 상태만 변경 (auth.users 삭제 없음)
-- 02. supabase-profiles.sql 적용 후, Supabase SQL Editor에서 실행하세요.
-- =============================================================================
-- 1) profiles에 status, withdrawn_at 컬럼 추가
ALTER TABLE public.profiles
ADD COLUMN IF NOT EXISTS status text NOT NULL DEFAULT 'active' CHECK (status IN ('active', 'withdrawn'));
ALTER TABLE public.profiles
ADD COLUMN IF NOT EXISTS withdrawn_at timestamptz;
-- 탈퇴 시 display_name 없이 INSERT하려면 nullable 허용 (기존 행은 그대로 유지)
ALTER TABLE public.profiles
ALTER COLUMN display_name DROP NOT NULL;
COMMENT ON COLUMN public.profiles.status IS '계정 상태: active(활성) | withdrawn(탈퇴)';
COMMENT ON COLUMN public.profiles.withdrawn_at IS '탈퇴 처리 시각 (status=withdrawn일 때)';
-- 2) RPC: 내 계정 탈퇴 처리 (status만 withdrawn으로 변경)
CREATE OR REPLACE FUNCTION public.withdraw_my_account()
RETURNS void
LANGUAGE plpgsql
SECURITY DEFINER
SET search_path = public
AS $$
BEGIN
INSERT INTO public.profiles (user_id, status, withdrawn_at)
VALUES (auth.uid(), 'withdrawn', now())
ON CONFLICT (user_id) DO UPDATE SET
status = 'withdrawn',
withdrawn_at = now(),
updated_at = now();
END;
$$;
COMMENT ON FUNCTION public.withdraw_my_account() IS '현재 로그인 사용자를 탈퇴 상태로 변경. auth.users는 삭제하지 않음.';
GRANT EXECUTE ON FUNCTION public.withdraw_my_account() TO authenticated;
-- 3) RPC: 내 계정 상태 조회 (앱 초기화 시 탈퇴 여부 확인용)
CREATE OR REPLACE FUNCTION public.get_my_account_status()
RETURNS text
LANGUAGE sql
STABLE
SECURITY DEFINER
SET search_path = public
AS $$
SELECT COALESCE(
(SELECT status FROM public.profiles WHERE user_id = auth.uid() LIMIT 1),
'active'
);
$$;
COMMENT ON FUNCTION public.get_my_account_status() IS '현재 로그인 사용자의 계정 상태(active | withdrawn). 프로필 행이 없으면 active.';
GRANT EXECUTE ON FUNCTION public.get_my_account_status() TO authenticated;