|
| 1 | +-- Fix org create RPC for Postgres plpgsql.variable_conflict=error environments. |
| 2 | +-- Earlier deployments used an unqualified `org_id` column reference inside |
| 3 | +-- `powergit_create_org`, which collides with the function argument and raises: |
| 4 | +-- column reference "org_id" is ambiguous |
| 5 | + |
| 6 | +create or replace function public.powergit_create_org(org_id text, name text default null) |
| 7 | +returns public.orgs |
| 8 | +language plpgsql |
| 9 | +security definer |
| 10 | +set search_path = public |
| 11 | +as $$ |
| 12 | +declare |
| 13 | + uid uuid; |
| 14 | + normalized text; |
| 15 | + candidate_name text; |
| 16 | + existing public.orgs%rowtype; |
| 17 | + member_count int; |
| 18 | +begin |
| 19 | + uid := auth.uid(); |
| 20 | + if uid is null then |
| 21 | + raise exception 'Not authenticated.' using errcode = '28000'; |
| 22 | + end if; |
| 23 | + |
| 24 | + normalized := lower(trim(org_id)); |
| 25 | + if normalized is null or normalized = '' then |
| 26 | + raise exception 'org_id is required.' using errcode = '22023'; |
| 27 | + end if; |
| 28 | + |
| 29 | + if normalized like 'gh-%' or normalized like 'github-%' then |
| 30 | + raise exception 'Org ids starting with "gh-" or "github-" are reserved.' using errcode = '22023'; |
| 31 | + end if; |
| 32 | + |
| 33 | + if normalized !~ '^[a-z0-9][a-z0-9._-]{0,79}$' then |
| 34 | + raise exception 'Invalid org id "%". Use 1-80 chars: [a-z0-9._-].', normalized using errcode = '22023'; |
| 35 | + end if; |
| 36 | + |
| 37 | + candidate_name := nullif(trim(coalesce(name, '')), ''); |
| 38 | + |
| 39 | + select * into existing from public.orgs where id = normalized limit 1; |
| 40 | + if found then |
| 41 | + -- Qualify org_id to avoid ambiguity with the `org_id` function argument. |
| 42 | + select count(*) into member_count from public.org_members m where m.org_id = normalized; |
| 43 | + if member_count = 0 then |
| 44 | + update public.orgs |
| 45 | + set |
| 46 | + name = coalesce(candidate_name, public.orgs.name), |
| 47 | + created_by = coalesce(public.orgs.created_by, uid), |
| 48 | + updated_at = now() |
| 49 | + where id = normalized; |
| 50 | + insert into public.org_members (org_id, user_id, role) |
| 51 | + values (normalized, uid, 'admin') |
| 52 | + on conflict on constraint org_members_pkey do update set role = 'admin', updated_at = now(); |
| 53 | + select * into existing from public.orgs where id = normalized limit 1; |
| 54 | + return existing; |
| 55 | + end if; |
| 56 | + raise exception 'Org "%" already exists.', normalized using errcode = '23505'; |
| 57 | + end if; |
| 58 | + |
| 59 | + insert into public.orgs (id, name, created_by) |
| 60 | + values (normalized, candidate_name, uid) |
| 61 | + returning * into existing; |
| 62 | + |
| 63 | + insert into public.org_members (org_id, user_id, role) |
| 64 | + values (normalized, uid, 'admin') |
| 65 | + on conflict on constraint org_members_pkey do update set role = 'admin', updated_at = now(); |
| 66 | + |
| 67 | + return existing; |
| 68 | +end; |
| 69 | +$$; |
| 70 | + |
| 71 | +grant execute on function public.powergit_create_org(text, text) to authenticated; |
0 commit comments