네이버 뉴스 검색 결과 Supabase 저장 개발 계획

개요

현재 네이버 뉴스 검색 결과를 실시간으로만 보여주는 구조에서, Supabase를 연동하여 검색 이력과 뉴스 결과를 영구 저장하고 관리하는 기능을 추가한다.


1. 데이터베이스 스키마

1-1. search_history 테이블 (변경 없음)

create table public.search_history (
  id uuid primary key default uuid_generate_v4(),
  keyword text not null,
  searched_at timestamp with time zone not null default now(),
  last_build_date timestamp with time zone,
  total_results integer
);

create index idx_search_history_searched_at on public.search_history (searched_at desc);

1-2. search_results 테이블 (컬럼 1개 추가)

create table public.search_results (
  id uuid primary key default uuid_generate_v4(),
  search_id uuid not null references public.search_history(id) on delete cascade,
  title text not null,
  original_link text,
  naver_link text,
  description text,
  pub_date timestamp with time zone,
  is_permanent boolean not null default false   -- ★ 영구 저장 플래그
);

create index idx_search_results_search_id on public.search_results (search_id);
create index idx_search_results_is_permanent on public.search_results (is_permanent)
  where is_permanent = true;

핵심 동작: search_history 행 삭제 시 ON DELETE CASCADE로 연결된 search_results가 자동 삭제된다. 단, is_permanent = true 인 행은 별도 API에서 cascade 전에 search_id를 NULL로 해제 하거나, 삭제 전 is_permanent 행을 미리 걸러 보호하는 로직을 API 레이어에서 처리한다.

is_permanent 보호 전략

  1. 검색어 삭제 API 호출 시, 먼저 해당 search_id에 속한 is_permanent = true 기사 수를 조회.
  2. 있을 경우 클라이언트에게 "영구 저장 기사 N건은 그대로 유지됩니다" 문구와 함께 삭제 확인 모달 표시.
  3. 삭제 진행 시 Supabase에서 is_permanent = true 행의 search_idNULL로 업데이트 → 이후 search_history 행 삭제 (cascade).
  4. search_id가 NULL인 영구저장 기사는 "저장 기사" 탭에서 별도 표시.

search_results.search_id를 nullable로 변경:

alter table public.search_results
  alter column search_id drop not null;