forked from fahrPool/fahrPool
feat: add profiles.username column with backfill and trigger support
This commit is contained in:
parent
4397de9530
commit
a6cbbbbc89
|
|
@ -0,0 +1,83 @@
|
|||
-- Fuegt einen eindeutigen, vom Anzeigenamen unabhaengigen Benutzernamen
|
||||
-- hinzu, damit sich Nutzer zusaetzlich zur E-Mail per Benutzername anmelden
|
||||
-- koennen (siehe docs/superpowers/specs/2026-07-15-benutzername-login-design.md).
|
||||
-- Format wird ausschliesslich app-seitig erzwungen (^[a-z0-9._-]{3,30}$), die
|
||||
-- DB erzwingt nur Eindeutigkeit und Pflicht.
|
||||
|
||||
alter table public.profiles add column username text;
|
||||
|
||||
-- Backfill fuer bereits bestehende Zeilen: aus dem Namen abgeleitet
|
||||
-- (kleingeschrieben, Leerzeichen -> Punkt, unzulaessige Zeichen entfernt),
|
||||
-- bei Kollision numerisches Suffix. Auf einer frischen DB (kein Reset-Seed
|
||||
-- mit vorhandenen Profiles) ist diese Schleife ein No-op.
|
||||
do $$
|
||||
declare
|
||||
r record;
|
||||
base text;
|
||||
candidate text;
|
||||
suffix int;
|
||||
begin
|
||||
for r in select id, name from public.profiles order by created_at loop
|
||||
base := lower(regexp_replace(r.name, '\s+', '.', 'g'));
|
||||
base := regexp_replace(base, '[^a-z0-9._-]', '', 'g');
|
||||
base := substring(base from 1 for 30);
|
||||
if base = '' then
|
||||
base := 'user' || substring(r.id::text from 1 for 8);
|
||||
end if;
|
||||
|
||||
candidate := base;
|
||||
suffix := 2;
|
||||
while exists (select 1 from public.profiles where username = candidate) loop
|
||||
candidate := substring(base from 1 for 30 - length(suffix::text)) || suffix::text;
|
||||
suffix := suffix + 1;
|
||||
end loop;
|
||||
|
||||
update public.profiles set username = candidate where id = r.id;
|
||||
end loop;
|
||||
end $$;
|
||||
|
||||
alter table public.profiles alter column username set not null;
|
||||
alter table public.profiles add constraint profiles_username_unique unique (username);
|
||||
|
||||
-- Ab jetzt muss auch handle_new_user() (siehe 20260704092843_profiles.sql)
|
||||
-- fuer jedes neue auth.users-Insert einen (vorlaeufigen) Benutzernamen
|
||||
-- vergeben, sonst schlaegt das Insert in profiles am NOT NULL fehl -- das
|
||||
-- betrifft sowohl echte Signups als auch auth.admin.createUser() aus der
|
||||
-- Nutzerverwaltung. Die Nutzerverwaltung ueberschreibt diesen Platzhalter
|
||||
-- direkt danach mit dem vom Admin gewaehlten Benutzernamen (genau wie sie
|
||||
-- es heute schon mit der Rolle 'readonly' macht).
|
||||
create or replace function public.handle_new_user()
|
||||
returns trigger
|
||||
language plpgsql
|
||||
security definer
|
||||
set search_path = public
|
||||
as $$
|
||||
declare
|
||||
base text;
|
||||
candidate text;
|
||||
suffix int;
|
||||
begin
|
||||
base := lower(regexp_replace(coalesce(new.raw_user_meta_data ->> 'name', new.email), '\s+', '.', 'g'));
|
||||
base := regexp_replace(base, '[^a-z0-9._-]', '', 'g');
|
||||
base := substring(base from 1 for 30);
|
||||
if base = '' then
|
||||
base := 'user' || substring(new.id::text from 1 for 8);
|
||||
end if;
|
||||
|
||||
candidate := base;
|
||||
suffix := 2;
|
||||
while exists (select 1 from public.profiles where username = candidate) loop
|
||||
candidate := substring(base from 1 for 30 - length(suffix::text)) || suffix::text;
|
||||
suffix := suffix + 1;
|
||||
end loop;
|
||||
|
||||
insert into public.profiles (id, name, role, username)
|
||||
values (
|
||||
new.id,
|
||||
coalesce(new.raw_user_meta_data ->> 'name', new.email),
|
||||
'readonly',
|
||||
candidate
|
||||
);
|
||||
return new;
|
||||
end;
|
||||
$$;
|
||||
Loading…
Reference in New Issue