From de8fc2ee80780245cffdad6de8793bdff84ec144 Mon Sep 17 00:00:00 2001 From: kernelfoxx Date: Mon, 3 Aug 2026 22:48:38 +0300 Subject: [PATCH] migration: drop collectible_usernames_owner_active_idx constraint allowing users to own multiple active/collectible usernames --- ...0001_drop_collectible_usernames_owner_active_idx.down.sql | 2 ++ ...000001_drop_collectible_usernames_owner_active_idx.up.sql | 2 ++ internal/store/postgres/collectible_username.go | 5 +++++ 3 files changed, 9 insertions(+) create mode 100644 deploy/migrations/20260803000001_drop_collectible_usernames_owner_active_idx.down.sql create mode 100644 deploy/migrations/20260803000001_drop_collectible_usernames_owner_active_idx.up.sql diff --git a/deploy/migrations/20260803000001_drop_collectible_usernames_owner_active_idx.down.sql b/deploy/migrations/20260803000001_drop_collectible_usernames_owner_active_idx.down.sql new file mode 100644 index 00000000..1d219504 --- /dev/null +++ b/deploy/migrations/20260803000001_drop_collectible_usernames_owner_active_idx.down.sql @@ -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; diff --git a/deploy/migrations/20260803000001_drop_collectible_usernames_owner_active_idx.up.sql b/deploy/migrations/20260803000001_drop_collectible_usernames_owner_active_idx.up.sql new file mode 100644 index 00000000..436aa0dc --- /dev/null +++ b/deploy/migrations/20260803000001_drop_collectible_usernames_owner_active_idx.up.sql @@ -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; diff --git a/internal/store/postgres/collectible_username.go b/internal/store/postgres/collectible_username.go index 7e705e3c..75cfba90 100644 --- a/internal/store/postgres/collectible_username.go +++ b/internal/store/postgres/collectible_username.go @@ -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)