Skip to content

Commit

Permalink
Remove unique constraint from username
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Zlopez committed Dec 5, 2024
1 parent d6ecf4a commit aa1aef3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -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 ###
4 changes: 3 additions & 1 deletion anitya/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down
11 changes: 0 additions & 11 deletions anitya/tests/db/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="[email protected]", username="user")

self.session.add(user)
self.session.commit()

user = models.User(email="[email protected]", 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="[email protected]", username="user")
Expand Down

0 comments on commit aa1aef3

Please sign in to comment.