Skip to content

Commit 5e6bfc2

Browse files
committed
Persist trimmed profile first_name from DB trigger
Assign NEW.first_name after trim so stored value matches validation. Follow-up migration for branches that already ran the earlier file. Made-with: Cursor
1 parent 3527096 commit 5e6bfc2

2 files changed

Lines changed: 84 additions & 0 deletions

File tree

supabase/migrations/20260420120000_chat_thread_rate_limit_and_profile_first_name.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ BEGIN
6363
END IF;
6464

6565
v := trim(both FROM NEW.first_name);
66+
NEW.first_name := v;
6667

6768
IF length(v) < 2 OR length(v) > 24 THEN
6869
RAISE EXCEPTION 'First name must be between 2 and 24 characters.'
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
-- Environments that already applied 20260420120000 before we assigned back to NEW.first_name
2+
-- get the same trigger body; new installs get it from the original migration, this is idempotent.
3+
4+
CREATE OR REPLACE FUNCTION public.enforce_profile_first_name_rules() RETURNS trigger
5+
LANGUAGE plpgsql
6+
SET search_path TO 'public'
7+
AS $$
8+
DECLARE
9+
v text;
10+
v_lower text;
11+
BEGIN
12+
IF NEW.first_name IS NULL THEN
13+
RETURN NEW;
14+
END IF;
15+
16+
v := trim(both FROM NEW.first_name);
17+
NEW.first_name := v;
18+
19+
IF length(v) < 2 OR length(v) > 24 THEN
20+
RAISE EXCEPTION 'First name must be between 2 and 24 characters.'
21+
USING ERRCODE = '23514';
22+
END IF;
23+
24+
v_lower := lower(v);
25+
26+
IF v ~ '[0-9]' THEN
27+
RAISE EXCEPTION 'First name cannot include numbers.'
28+
USING ERRCODE = '23514';
29+
END IF;
30+
31+
IF v ~ '[@#]' OR v ~* 'www\.' OR v ~* 'https?://' THEN
32+
RAISE EXCEPTION 'First name contains characters that are not allowed.'
33+
USING ERRCODE = '23514';
34+
END IF;
35+
36+
IF v_lower ~ '\.(com|org|net|io|app)([[:space:]]|$)' THEN
37+
RAISE EXCEPTION 'First name contains characters that are not allowed.'
38+
USING ERRCODE = '23514';
39+
END IF;
40+
41+
IF v_lower ~ 'p[e3]els|pe[e3]ls|pe[e3]l[s5]' THEN
42+
RAISE EXCEPTION 'This first name is reserved.'
43+
USING ERRCODE = '23514';
44+
END IF;
45+
46+
IF v_lower = 'peels' OR v_lower LIKE 'peels %' OR v_lower LIKE 'peels-%' OR v_lower LIKE 'peels.%' THEN
47+
RAISE EXCEPTION 'This first name is reserved.'
48+
USING ERRCODE = '23514';
49+
END IF;
50+
51+
IF v_lower ~ '(^|[[:space:]])peels($|[[:space:].-])' THEN
52+
RAISE EXCEPTION 'This first name is reserved.'
53+
USING ERRCODE = '23514';
54+
END IF;
55+
56+
IF v_lower IN (
57+
'support',
58+
'admin',
59+
'administrator',
60+
'moderator',
61+
'team',
62+
'official',
63+
'staff',
64+
'helpdesk',
65+
'help',
66+
'trust',
67+
'safety',
68+
'security',
69+
'system',
70+
'service',
71+
'root',
72+
'moderation'
73+
) OR v_lower LIKE '%peels support%' OR v_lower LIKE '%peels team%' OR v_lower LIKE '%customer service%' THEN
74+
RAISE EXCEPTION 'This first name is reserved.'
75+
USING ERRCODE = '23514';
76+
END IF;
77+
78+
RETURN NEW;
79+
END;
80+
$$;
81+
82+
83+
ALTER FUNCTION public.enforce_profile_first_name_rules() OWNER TO postgres;

0 commit comments

Comments
 (0)