Files
brainfeed/supabase/migrations/20261005010000_nested_notes.sql
Kevin BurchellandClaude Opus 5.5 023f165911 Headings, add-to-note entries, nested notes; fix Archived/To-do filters
- '# ', '## ', '### ' lines render as headings (H button in the composer)
- 'Add to this note' appends a time-stamped entry ('— Oct 4, 8:30 PM')
- Notes can live inside another note (one level): Move into…, Take out,
  '▸ N inside' expander, '+ New note inside', 'in: <parent>' label in
  search/filters; DB trigger enforces same owner, one level, no self
- Fix: the To-do change had made the Archived view empty and let To-do
  include archived notes

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-04 17:52:02 -07:00

29 lines
1.4 KiB
PL/PgSQL

alter table public.notes add column parent_id uuid references public.notes(id) on delete set null;
create index notes_parent on public.notes (parent_id) where parent_id is not null;
-- One level only, same owner, never itself.
create or replace function private.notes_check_parent()
returns trigger language plpgsql security definer set search_path = '' as $$
begin
if new.parent_id is null then return new; end if;
if new.parent_id = new.id then raise exception 'A note cannot contain itself'; end if;
if not exists (select 1 from public.notes p where p.id = new.parent_id and p.user_id = new.user_id and p.parent_id is null) then
raise exception 'That note cannot hold other notes';
end if;
if exists (select 1 from public.notes c where c.parent_id = new.id) then
raise exception 'A note that holds other notes cannot be moved inside another';
end if;
return new;
end $$;
create trigger notes_check_parent before insert or update of parent_id on public.notes
for each row execute function private.notes_check_parent();
-- Child counts for a set of parent notes (RLS-scoped).
create or replace function public.child_counts(p_ids uuid[])
returns table (parent_id uuid, count bigint) language sql stable security invoker set search_path = '' as $$
select n.parent_id, count(*) from public.notes n
where n.parent_id = any (p_ids) and n.user_id = (select auth.uid())
group by n.parent_id;
$$;