سامانه صدور فاکتور و حسابداری
Mirrored from https://github.com/davoodmehraban89/Factor-easy.git
Synced Dec 4, 08:00 UTC
db(test only): add idempotent missing phone identity compatibility bridge
davoodmehraban89 committed
Oct 16, 2026 at 13:14 UTC
211b28a7ad09c2c8b41e38b24b6b3c1a6023b344
1 file changed
+38
supabase/migrations/20261007170000_phase1a_phone_identity_compat_bridge.sql
new
+38
@@ -0,0 +1,38 @@
1
+-- Phase 1A: reconstruct one Production-only phone identity migration, WITHOUT
2
+-- renaming/editing any prior migration, without repairing Production history.
3
+-- This file applies only in disposable CLI tests until a separate cutover approval.
4
+-- Supabase Production already has phone/email identity changes: each statement is
5
+-- idempotent and intentionally does not overwrite the existing identity RPC.
6
+-- Rollback: only in a disposable clone with no dependent user data; drop the
7
+-- newly created function, index, phone column; reset email NOT NULL only if safe.
8
+ALTER TABLE public.profiles ADD COLUMN IF NOT EXISTS phone text;
9
+ALTER TABLE public.profiles ALTER COLUMN email DROP NOT NULL;
10
+CREATE UNIQUE INDEX IF NOT EXISTS profiles_phone_unique
11
+ ON public.profiles(phone) WHERE phone IS NOT NULL;
12
+
13
+DO $phase1a$
14
+BEGIN
15
+ IF NOT EXISTS (
16
+ SELECT 1 FROM pg_proc
17
+ WHERE oid = to_regprocedure('public.update_my_identity_profile(text,text)')
18
+ ) THEN
19
+ EXECUTE $new_fn$
20
+ CREATE FUNCTION public.update_my_identity_profile(
21
+ new_username text, new_full_name text DEFAULT NULL::text
22
+ ) RETURNS void
23
+ LANGUAGE plpgsql SECURITY DEFINER SET search_path = ''
24
+ AS $body$
25
+ declare v_uid uuid:=auth.uid();v_username text:=lower(trim(coalesce(new_username,'')));
26
+ begin
27
+ if v_uid is null then raise exception 'Authentication required';end if;
28
+ if v_username !~ '^[a-z0-9_.-]{3,40}$' then raise exception 'Username must be 3-40 latin letters, digits, dot, underscore or dash';end if;
29
+ if exists(select 1 from public.profiles where lower(username)=v_username and id<>v_uid) then raise exception 'Username already in use';end if;
30
+ update public.profiles set username=v_username,full_name=coalesce(nullif(trim(new_full_name),''),full_name) where id=v_uid;
31
+ end $body$;
32
+ $new_fn$;
33
+ END IF;
34
+END
35
+$phase1a$;
36
+
37
+REVOKE ALL ON FUNCTION public.update_my_identity_profile(text,text) FROM PUBLIC, anon;
38
+GRANT EXECUTE ON FUNCTION public.update_my_identity_profile(text,text) TO authenticated;