Skip to content

Commit

Permalink
feat: preserve user id on alias deleted (#2304)
Browse files Browse the repository at this point in the history
  • Loading branch information
cquintana92 authored Nov 6, 2024
1 parent 7f01dec commit 31c8a76
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
4 changes: 3 additions & 1 deletion app/alias_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,9 @@ def delete_alias(
)
else:
if not DeletedAlias.get_by(email=alias.email):
deleted_alias = DeletedAlias(email=alias.email, reason=reason)
deleted_alias = DeletedAlias(
email=alias.email, reason=reason, user_id=user.id
)
Session.add(deleted_alias)
Session.commit()
LOG.i(f"Moving {alias} to global trash {deleted_alias}")
Expand Down
6 changes: 6 additions & 0 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2306,6 +2306,12 @@ class DeletedAlias(Base, ModelMixin):
server_default=str(AliasDeleteReason.Unspecified.value),
)

user_id = sa.Column(sa.Integer, nullable=True, default=None, server_default=None)

__table_args__ = (
sa.Index("ix_deleted_alias_user_id_created_at", "user_id", "created_at"),
)

@classmethod
def create(cls, **kw):
raise Exception("should use delete_alias(alias,user) instead")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""Preserve user id on alias delete
Revision ID: 4882cc49dde9
Revises: 32f25cbf12f6
Create Date: 2024-11-06 10:10:40.235991
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '4882cc49dde9'
down_revision = '32f25cbf12f6'
branch_labels = None
depends_on = None


def upgrade():
op.add_column('deleted_alias', sa.Column('user_id', sa.Integer(), server_default=None, nullable=True))
with op.get_context().autocommit_block():
op.create_index('ix_deleted_alias_user_id_created_at', 'deleted_alias', ['user_id', 'created_at'], unique=False, postgresql_concurrently=True)


def downgrade():
with op.get_context().autocommit_block():
op.drop_index('ix_deleted_alias_user_id_created_at', table_name='deleted_alias')
op.drop_column('deleted_alias', 'user_id')
4 changes: 3 additions & 1 deletion tests/test_alias_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ def test_delete_alias(flask_client):

delete_alias(alias, user)
assert Alias.get_by(email=alias.email) is None
assert DeletedAlias.get_by(email=alias.email)
deleted_alias = DeletedAlias.get_by(email=alias.email)
assert deleted_alias is not None
assert deleted_alias.user_id == user.id


def test_delete_alias_already_in_trash(flask_client):
Expand Down

0 comments on commit 31c8a76

Please sign in to comment.