migration: drop collectible_usernames_owner_active_idx constraint allowing users to own multiple active/collectible usernames

This commit is contained in:
kernelfoxx
2026-08-03 22:48:38 +03:00
Unverified
parent f5166b2209
commit de8fc2ee80
3 changed files with 9 additions and 0 deletions
@@ -0,0 +1,2 @@
DROP INDEX IF EXISTS public.collectible_usernames_owner_active_nonunique_idx;
CREATE UNIQUE INDEX IF NOT EXISTS collectible_usernames_owner_active_idx ON public.collectible_usernames (owner_user_id) WHERE active AND owner_user_id IS NOT NULL;
@@ -0,0 +1,2 @@
DROP INDEX IF EXISTS public.collectible_usernames_owner_active_idx;
CREATE INDEX IF NOT EXISTS collectible_usernames_owner_active_nonunique_idx ON public.collectible_usernames (owner_user_id) WHERE owner_user_id IS NOT NULL;
@@ -119,6 +119,11 @@ func (s *CollectibleUsernameStore) Issue(ctx context.Context, cu domain.Collecti
if found && !owner.matches(peerUsernameTypeUser, cu.OwnerUserID) {
return domain.ErrUsernameOccupied
}
if cu.Active {
if _, err := tx.Exec(ctx, `UPDATE public.collectible_usernames SET active = false, updated_at = now() WHERE owner_user_id = $1 AND username != $2 AND active`, cu.OwnerUserID, normalized); err != nil {
return err
}
}
if _, err := tx.Exec(ctx, `
INSERT INTO peer_usernames (username_lower, peer_type, peer_id)
VALUES ($1, $2, $3)