Seed the public catalog from domain/library.ts

- Catalog table ids (device_categories, port_types, cable_types,
  device_templates) switch from uuid to text so the existing stable,
  human-readable ids (pt-hdmi, dt-display, ...) survive the move
  instead of every diagram's references silently orphaning.
- supabase/seed.sql is generated (scripts/generate-seed.mjs), not
  hand-written, so the seed data can't drift from the actual source of
  truth in domain/library.ts. Re-run the script after editing the
  built-in library.
- Also committing ideas.md/organized-ideas.md, which have been driving
  every backend decision this whole project but were never actually
  checked in.

RLS test suite re-run clean (23/23) after both the schema change and
the seed.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017DUU6CnxECCDeqDNYJgr5x
This commit is contained in:
2026-09-07 23:02:12 -05:00
co-authored by Claude Sonnet 5
parent 2f1b83e0a9
commit 93cb4a9617
5 changed files with 495 additions and 0 deletions
@@ -0,0 +1,96 @@
-- Switch catalog table ids from uuid to text.
--
-- The app's built-in library (domain/library.ts) has always used stable,
-- human-readable string ids ('pt-hdmi', 'dt-display', 'ct-xlr', ...), and
-- existing diagrams (including everything created locally so far) reference
-- port/cable/category/device types by exactly those ids. Seeding the public
-- catalog with fresh uuids instead would silently orphan every one of those
-- references. Tables are empty at this point (catalog data hasn't been
-- seeded yet), so this is a pure schema change, not a data migration.
--
-- manufacturers and device_template_ports keep uuid ids — nothing
-- referenced manufacturers before now (it's a brand new table), and a
-- template's ports were never independently referenced by id (placing a
-- device already copies ports with a fresh id, discarding the template
-- port's original id — see createDeviceFromTemplate).
-- Drop FKs that depend on the columns we're about to retype.
alter table public.device_template_ports drop constraint device_template_ports_device_template_id_fkey;
alter table public.device_template_ports drop constraint device_template_ports_port_type_id_fkey;
alter table public.device_templates drop constraint device_templates_category_id_fkey;
-- device_template_ports' policies join back to device_templates.id, which
-- Postgres treats as a dependency that blocks ALTER TYPE on that column —
-- drop them here, recreate (verbatim) at the end.
drop policy "device_template_ports_select" on public.device_template_ports;
drop policy "device_template_ports_insert" on public.device_template_ports;
drop policy "device_template_ports_update" on public.device_template_ports;
drop policy "device_template_ports_delete" on public.device_template_ports;
-- Retype the catalog primary keys.
alter table public.device_categories alter column id drop default;
alter table public.device_categories alter column id type text;
alter table public.port_types alter column id drop default;
alter table public.port_types alter column id type text;
alter table public.cable_types alter column id drop default;
alter table public.cable_types alter column id type text;
alter table public.device_templates alter column id drop default;
alter table public.device_templates alter column id type text;
-- Retype the columns that reference them.
alter table public.device_templates alter column category_id type text;
alter table public.device_template_ports alter column device_template_id type text;
alter table public.device_template_ports alter column port_type_id type text;
-- catalog_submissions.entity_id is polymorphic (device_template/port_type/
-- cable_type/device_category all now text ids; manufacturer stays uuid, but
-- a uuid string is still valid text) — no FK constraint on it to begin with.
alter table public.catalog_submissions alter column entity_id type text;
-- Recreate the FKs.
alter table public.device_templates
add constraint device_templates_category_id_fkey foreign key (category_id) references public.device_categories (id);
alter table public.device_template_ports
add constraint device_template_ports_device_template_id_fkey foreign key (device_template_id) references public.device_templates (id) on delete cascade;
alter table public.device_template_ports
add constraint device_template_ports_port_type_id_fkey foreign key (port_type_id) references public.port_types (id);
-- Recreate the four policies dropped above, verbatim.
create policy "device_template_ports_select" on public.device_template_ports for select
using (
exists (
select 1 from public.device_templates dt
where dt.id = device_template_id
and (dt.is_public or dt.owner_id = auth.uid() or public.is_admin())
)
);
create policy "device_template_ports_insert" on public.device_template_ports for insert
with check (
exists (
select 1 from public.device_templates dt
where dt.id = device_template_id
and ((dt.owner_id = auth.uid() and not dt.is_public) or public.is_admin())
)
);
create policy "device_template_ports_update" on public.device_template_ports for update
using (
exists (
select 1 from public.device_templates dt
where dt.id = device_template_id
and ((dt.owner_id = auth.uid() and not dt.is_public) or public.is_admin())
)
);
create policy "device_template_ports_delete" on public.device_template_ports for delete
using (
exists (
select 1 from public.device_templates dt
where dt.id = device_template_id
and ((dt.owner_id = auth.uid() and not dt.is_public) or public.is_admin())
)
);