سامانه صدور فاکتور و حسابداری
test(auth): reproduce live missing-licence seat failure in disposable PostgreSQL
davoodmehraban89 committed
Oct 12, 2026 at 11:16 UTC
b44e48e2c8964040003308b101b0f2325f88a634
1 file changed
+40
supabase/tests/identity_bootstrap_seat_guard_repro.sql
new
+40
@@ -0,0 +1,40 @@
1
+-- Disposable-DB reconstruction of the LIVE seat limiter (no production data).
2
+create schema if not exists private;
3
+create or replace function private.enforce_organization_member_seat()
4
+returns trigger language plpgsql security definer set search_path='' as $$
5
+declare v_limit integer;v_active integer;
6
+begin
7
+ if new.status<>'active' then return new; end if;
8
+ if tg_op='UPDATE' and old.status='active' and old.organization_id=new.organization_id then return new; end if;
9
+ select l.max_users into v_limit
10
+ from public.licenses l
11
+ where l.organization_id=new.organization_id
12
+ for update;
13
+ if v_limit is null then raise exception 'organization license not found' using errcode='23503';end if;
14
+ select count(*)::integer into v_active
15
+ from public.organization_members m
16
+ where m.organization_id=new.organization_id
17
+ and m.status='active'
18
+ and (tg_op='INSERT' or m.id<>new.id);
19
+ if v_active>=v_limit then raise exception 'named-user seat limit exceeded' using errcode='23514';end if;
20
+ return new;
21
+end $$;
22
+create trigger trg_organization_member_seat before insert or update of status,organization_id
23
+on public.organization_members for each row execute function private.enforce_organization_member_seat();
24
+
25
+-- Reproduce the exact initial failure while original Wave5a function is installed.
26
+-- Nested exception scope rolls back every synthetic Auth/profile/org write.
27
+do $$
28
+begin
29
+ begin
30
+ insert into auth.users(id,email,raw_user_meta_data)
31
+ values ('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa','before-fix@example.invalid','{"full_name":"Before Fix QA"}'::jsonb);
32
+ raise exception 'expected pre-fix bootstrap to fail on missing license';
33
+ exception when sqlstate '23503' then
34
+ if position('organization license not found' in sqlerrm)=0 then raise;end if;
35
+ end;
36
+ if exists(select 1 from auth.users where id='aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa')
37
+ or exists(select 1 from public.profiles where id='aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa') then
38
+ raise exception 'failed transaction left partial identity';
39
+ end if;
40
+end $$;