122 lines
4.8 KiB
PL/PgSQL
122 lines
4.8 KiB
PL/PgSQL
-- ============================================================
|
|
-- 쟁승메이드 Supabase 스키마
|
|
-- Supabase SQL Editor에서 순서대로 실행하세요
|
|
-- ============================================================
|
|
|
|
-- ① profiles (유저 프로필 - auth.users와 연결)
|
|
create table public.profiles (
|
|
id uuid references auth.users(id) on delete cascade primary key,
|
|
email text,
|
|
full_name text,
|
|
avatar_url text,
|
|
created_at timestamptz default now(),
|
|
updated_at timestamptz default now()
|
|
);
|
|
|
|
-- 신규 가입 시 profiles 자동 생성 트리거
|
|
create or replace function public.handle_new_user()
|
|
returns trigger as $$
|
|
begin
|
|
insert into public.profiles (id, email, full_name, avatar_url)
|
|
values (
|
|
new.id,
|
|
new.email,
|
|
new.raw_user_meta_data->>'full_name',
|
|
new.raw_user_meta_data->>'avatar_url'
|
|
);
|
|
return new;
|
|
end;
|
|
$$ language plpgsql security definer;
|
|
|
|
create or replace trigger on_auth_user_created
|
|
after insert on auth.users
|
|
for each row execute procedure public.handle_new_user();
|
|
|
|
-- RLS
|
|
alter table public.profiles enable row level security;
|
|
create policy "본인 프로필 조회" on public.profiles for select using (auth.uid() = id);
|
|
create policy "본인 프로필 수정" on public.profiles for update using (auth.uid() = id);
|
|
|
|
|
|
-- ② saju_records (사주 분석 결과 저장)
|
|
create table public.saju_records (
|
|
id bigint generated by default as identity primary key,
|
|
user_id uuid references public.profiles(id) on delete cascade,
|
|
saju_data jsonb not null, -- SajuData JSON
|
|
interpretation text, -- AI 해석 Markdown
|
|
is_paid boolean default false, -- 유료 결제 여부
|
|
created_at timestamptz default now()
|
|
);
|
|
|
|
alter table public.saju_records enable row level security;
|
|
create policy "본인 사주 기록 조회" on public.saju_records for select using (auth.uid() = user_id);
|
|
create policy "본인 사주 기록 생성" on public.saju_records for insert with check (auth.uid() = user_id);
|
|
|
|
|
|
-- ③ products (판매 상품 정의)
|
|
create table public.products (
|
|
id text primary key, -- 예: 'saju_basic', 'saju_detail'
|
|
name text not null, -- 상품명
|
|
description text,
|
|
price integer not null, -- 원 단위
|
|
category text not null, -- 'saju' | 'lotto' | 'subscription'
|
|
is_active boolean default true,
|
|
created_at timestamptz default now()
|
|
);
|
|
|
|
-- 초기 상품 데이터
|
|
insert into public.products (id, name, description, price, category) values
|
|
('saju_detail', 'AI 사주 상세 리포트', '신강/신약, 용신, 대운, AI 12가지 항목 해석', 4900, 'saju'),
|
|
('lotto_premium', '로또 프리미엄 구독', '매주 프리미엄 번호 5조합 + 통계', 4900, 'lotto');
|
|
|
|
|
|
-- ④ orders (주문 - 결제 전 생성)
|
|
create table public.orders (
|
|
id uuid default gen_random_uuid() primary key,
|
|
user_id uuid references public.profiles(id) on delete set null,
|
|
product_id text references public.products(id),
|
|
amount integer not null,
|
|
status text default 'pending', -- 'pending' | 'paid' | 'failed' | 'cancelled'
|
|
metadata jsonb, -- 추가 정보 (saju_record_id 등)
|
|
created_at timestamptz default now(),
|
|
updated_at timestamptz default now()
|
|
);
|
|
|
|
alter table public.orders enable row level security;
|
|
create policy "본인 주문 조회" on public.orders for select using (auth.uid() = user_id);
|
|
create policy "본인 주문 생성" on public.orders for insert with check (auth.uid() = user_id);
|
|
|
|
|
|
-- ⑤ payments (결제 완료 내역 - 서버에서 검증 후 저장)
|
|
create table public.payments (
|
|
id uuid default gen_random_uuid() primary key,
|
|
user_id uuid references public.profiles(id) on delete set null,
|
|
order_id uuid references public.orders(id),
|
|
product_name text not null,
|
|
amount integer not null,
|
|
status text default 'paid', -- 'paid' | 'refunded' | 'partial_refund'
|
|
pg_provider text, -- 'toss' | 'portone'
|
|
pg_payment_key text unique, -- PG사 결제 키
|
|
created_at timestamptz default now()
|
|
);
|
|
|
|
alter table public.payments enable row level security;
|
|
create policy "본인 결제 내역 조회" on public.payments for select using (auth.uid() = user_id);
|
|
|
|
|
|
-- ⑥ contact_requests (외주/서비스 문의 내역)
|
|
create table public.contact_requests (
|
|
id uuid default gen_random_uuid() primary key,
|
|
user_id uuid references public.profiles(id) on delete set null,
|
|
email text not null,
|
|
name text,
|
|
service text not null, -- 문의 서비스 종류
|
|
message text not null,
|
|
status text default 'pending', -- 'pending' | 'in_progress' | 'completed'
|
|
created_at timestamptz default now()
|
|
);
|
|
|
|
alter table public.contact_requests enable row level security;
|
|
create policy "본인 의뢰 내역 조회" on public.contact_requests for select using (auth.uid() = user_id);
|
|
create policy "누구나 의뢰 생성" on public.contact_requests for insert with check (true);
|