From aa1aef33c1e8091e22352a8ef21da3208982f777 Mon Sep 17 00:00:00 2001 From: Michal Konecny Date: Thu, 5 Dec 2024 17:34:47 +0100 Subject: [PATCH] Remove unique constraint from username With authlib we just need to have unique e-mail and we don't care about username anymore. This will prevent crash when the username exists in database but with different e-mail. Let this be handled by third party authentication systems. Signed-off-by: Michal Konecny --- ...3_remove_unique_attribute_from_username.py | 25 +++++++++++++++++++ anitya/db/models.py | 4 ++- anitya/tests/db/test_models.py | 11 -------- 3 files changed, 28 insertions(+), 12 deletions(-) create mode 100644 anitya/db/migrations/versions/ebc827e80373_remove_unique_attribute_from_username.py diff --git a/anitya/db/migrations/versions/ebc827e80373_remove_unique_attribute_from_username.py b/anitya/db/migrations/versions/ebc827e80373_remove_unique_attribute_from_username.py new file mode 100644 index 00000000..44876c14 --- /dev/null +++ b/anitya/db/migrations/versions/ebc827e80373_remove_unique_attribute_from_username.py @@ -0,0 +1,25 @@ +"""Remove unique attribute from username + +Revision ID: ebc827e80373 +Revises: 8ba7d4c42044 +Create Date: 2024-12-05 16:24:01.098473 +""" + +from alembic import op + + +# revision identifiers, used by Alembic. +revision = 'ebc827e80373' +down_revision = '8ba7d4c42044' + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_constraint('ix_users_username', 'users', type_='unique') + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_unique_constraint('ix_users_username', 'users', ['username']) + # ### end Alembic commands ### diff --git a/anitya/db/models.py b/anitya/db/models.py index 4e84cbec..6b000e42 100644 --- a/anitya/db/models.py +++ b/anitya/db/models.py @@ -985,7 +985,7 @@ class User(Base): # SMTP says 256 is the maximum length of a path: # https://tools.ietf.org/html/rfc5321#section-4.5.3 email = sa.Column(sa.String(256), nullable=False, index=True, unique=True) - username = sa.Column(sa.String(256), nullable=False, index=True, unique=True) + username = sa.Column(sa.String(256), nullable=False, index=True) active = sa.Column(sa.Boolean, default=True) admin = sa.Column(sa.Boolean, default=False) @@ -1001,6 +1001,8 @@ def is_admin(self): if not self.admin: if six.text_type(self.id) in anitya_config.get("ANITYA_WEB_ADMINS", []): self.admin = True + if six.text_type(self.email) in anitya_config.get("ANITYA_WEB_ADMINS", []): + self.admin = True return self.admin @property diff --git a/anitya/tests/db/test_models.py b/anitya/tests/db/test_models.py index 32957ab7..0b9849de 100644 --- a/anitya/tests/db/test_models.py +++ b/anitya/tests/db/test_models.py @@ -1083,17 +1083,6 @@ def test_user_email_unique(self): self.session.add(user) self.assertRaises(IntegrityError, self.session.commit) - def test_username_unique(self): - """Assert User usernames have a uniqueness constraint on them.""" - user = models.User(email="user@fedoraproject.org", username="user") - - self.session.add(user) - self.session.commit() - - user = models.User(email="user2@fedoraproject.org", username="user") - self.session.add(user) - self.assertRaises(IntegrityError, self.session.commit) - def test_default_active(self): """Assert User usernames have a uniqueness constraint on them.""" user = models.User(email="user@fedoraproject.org", username="user")