From 31c8a766bee69f3b69013511de1e45ccb0bd4c71 Mon Sep 17 00:00:00 2001 From: Carlos Quintana <74399022+cquintana92@users.noreply.github.com> Date: Wed, 6 Nov 2024 10:16:54 +0100 Subject: [PATCH] feat: preserve user id on alias deleted (#2304) --- app/alias_utils.py | 4 ++- app/models.py | 6 ++++ ...49dde9_preserve_user_id_on_alias_delete.py | 28 +++++++++++++++++++ tests/test_alias_utils.py | 4 ++- 4 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 migrations/versions/2024_110610_4882cc49dde9_preserve_user_id_on_alias_delete.py diff --git a/app/alias_utils.py b/app/alias_utils.py index 8bd273d7..b2a94664 100644 --- a/app/alias_utils.py +++ b/app/alias_utils.py @@ -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}") diff --git a/app/models.py b/app/models.py index 702e8752..f92a6db7 100644 --- a/app/models.py +++ b/app/models.py @@ -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") diff --git a/migrations/versions/2024_110610_4882cc49dde9_preserve_user_id_on_alias_delete.py b/migrations/versions/2024_110610_4882cc49dde9_preserve_user_id_on_alias_delete.py new file mode 100644 index 00000000..df68db15 --- /dev/null +++ b/migrations/versions/2024_110610_4882cc49dde9_preserve_user_id_on_alias_delete.py @@ -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') diff --git a/tests/test_alias_utils.py b/tests/test_alias_utils.py index 507b507a..aef172dd 100644 --- a/tests/test_alias_utils.py +++ b/tests/test_alias_utils.py @@ -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):