Add unique constraint for person(name, instance) (fixes #5045) (#5858)

* Add unique constraint for person(name, instance) (fixes #5045)

* fix

* not local

Co-authored-by: dullbananas <dull.bananas0@gmail.com>

---------

Co-authored-by: dullbananas <dull.bananas0@gmail.com>
This commit is contained in:
Nutomic 2025-07-14 21:14:05 +00:00 committed by GitHub
parent 21c1784a01
commit cf0e021129
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 22 additions and 5 deletions

View file

@ -37,7 +37,7 @@ lemmy_db_views_person_content_combined = { workspace = true, features = [
lemmy_db_views_search_combined = { workspace = true, features = ["full"] }
lemmy_utils = { workspace = true, features = ["full"] }
lemmy_db_schema = { workspace = true, features = ["full"] }
lemmy_api_utils = { workspace = true }
lemmy_api_utils = { workspace = true, features = ["full"] }
lemmy_apub_objects = { workspace = true }
activitypub_federation = { workspace = true }
lemmy_db_schema_file = { workspace = true }

View file

@ -91,12 +91,14 @@ mod tests {
let pool = &mut context.pool();
let data = TestData::create(pool).await?;
let name = "test_local_user_name";
let bio = "test_local_user_bio";
let creator = LocalUserView::create_test_user(pool, name, bio, false).await?;
let regular_user = LocalUserView::create_test_user(pool, name, bio, false).await?;
let admin_user = LocalUserView::create_test_user(pool, name, bio, true).await?;
let creator =
LocalUserView::create_test_user(pool, "test_local_user_name_1", bio, false).await?;
let regular_user =
LocalUserView::create_test_user(pool, "test_local_user_name_2", bio, false).await?;
let admin_user =
LocalUserView::create_test_user(pool, "test_local_user_name_3", bio, true).await?;
let community = Community::create(
pool,

View file

@ -0,0 +1,3 @@
ALTER TABLE person
DROP CONSTRAINT person_name_instance_unique;

View file

@ -0,0 +1,12 @@
-- lemmy requires (username + instance_id) to be unique
-- delete any existing duplicates
DELETE FROM person p1 USING person p2
WHERE p1.id > p2.id
AND p1.name = p2.name
AND p1.instance_id = p2.instance_id
AND NOT (p1.local
OR p2.local);
ALTER TABLE person
ADD CONSTRAINT person_name_instance_unique UNIQUE (name, instance_id);