We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents a65883a + c797ecd commit 5d72efeCopy full SHA for 5d72efe
supabase/migrations/20250217023110_update_generate_username_function.sql
@@ -0,0 +1,25 @@
1
+CREATE OR REPLACE FUNCTION public.generate_username()
2
+ RETURNS text
3
+ LANGUAGE plpgsql
4
+ SET search_path TO ''
5
+AS $function$
6
+DECLARE
7
+ generated_username text;
8
+ retry_count int := 0;
9
+BEGIN
10
+ LOOP
11
+ generated_username := 'user' || substring(gen_random_uuid()::text FROM 1 FOR 8);
12
+
13
+ -- Check if username already exists
14
+ IF NOT EXISTS (SELECT 1 FROM public.profiles WHERE username = generated_username) THEN
15
+ RETURN generated_username;
16
+ END IF;
17
18
+ retry_count := retry_count + 1;
19
+ IF retry_count >= 5 THEN
20
+ RAISE EXCEPTION 'Failed to generate a unique username after 5 attempts';
21
22
+ END LOOP;
23
+END;
24
+$function$
25
+;
0 commit comments