diff --git a/migrations/versions/edcc1a448ffb_soft_deletes.py b/migrations/versions/edcc1a448ffb_soft_deletes.py new file mode 100644 index 0000000..627d5bd --- /dev/null +++ b/migrations/versions/edcc1a448ffb_soft_deletes.py @@ -0,0 +1,31 @@ +"""soft-deletes + +Revision ID: edcc1a448ffb +Revises: 7388a2c219d2 +Create Date: 2024-11-10 09:23:32.307828 + +""" + +import sqlalchemy as sa +from alembic import op + + +# revision identifiers, used by Alembic. +revision = 'edcc1a448ffb' +down_revision = '7388a2c219d2' +branch_labels = None +depends_on = None + + +def upgrade(): + op.add_column('comment', sa.Column('is_deleted', sa.Boolean(), nullable=False, server_default=sa.false())) + op.add_column('lecturer', sa.Column('is_deleted', sa.Boolean(), nullable=False, server_default=sa.false())) + op.add_column( + 'lecturer_user_comment', sa.Column('is_deleted', sa.Boolean(), nullable=False, server_default=sa.false()) + ) + + +def downgrade(): + op.drop_column('lecturer_user_comment', 'is_deleted') + op.drop_column('lecturer', 'is_deleted') + op.drop_column('comment', 'is_deleted') diff --git a/rating_api/models/db.py b/rating_api/models/db.py index 747a983..a25a820 100644 --- a/rating_api/models/db.py +++ b/rating_api/models/db.py @@ -5,7 +5,7 @@ import uuid from enum import Enum -from sqlalchemy import UUID, DateTime +from sqlalchemy import UUID, Boolean, DateTime from sqlalchemy import Enum as DbEnum from sqlalchemy import ForeignKey, Integer, String, and_, func, or_, true from sqlalchemy.ext.hybrid import hybrid_method @@ -34,6 +34,7 @@ class Lecturer(BaseDbModel): avatar_link: Mapped[str] = mapped_column(String, nullable=True) timetable_id: Mapped[int] = mapped_column(Integer, unique=True, nullable=False) comments: Mapped[list[Comment]] = relationship("Comment", back_populates="lecturer") + is_deleted: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False) @hybrid_method def search_by_name(self, query: str) -> bool: @@ -65,8 +66,13 @@ class Comment(BaseDbModel): mark_freebie: Mapped[int] = mapped_column(Integer, nullable=False) mark_clarity: Mapped[int] = mapped_column(Integer, nullable=False) lecturer_id: Mapped[int] = mapped_column(Integer, ForeignKey("lecturer.id")) - lecturer: Mapped[Lecturer] = relationship("Lecturer", back_populates="comments") + lecturer: Mapped[Lecturer] = relationship( + "Lecturer", + back_populates="comments", + primaryjoin="and_(Comment.lecturer_id == Lecturer.id, not_(Lecturer.is_deleted))", + ) review_status: Mapped[ReviewStatus] = mapped_column(DbEnum(ReviewStatus, native_enum=False), nullable=False) + is_deleted: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False) class LecturerUserComment(BaseDbModel): @@ -75,3 +81,4 @@ class LecturerUserComment(BaseDbModel): lecturer_id: Mapped[int] = mapped_column(Integer, ForeignKey("lecturer.id")) create_ts: Mapped[datetime.datetime] = mapped_column(DateTime, default=datetime.datetime.utcnow, nullable=False) update_ts: Mapped[datetime.datetime] = mapped_column(DateTime, default=datetime.datetime.utcnow, nullable=False) + is_deleted: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False) diff --git a/rating_api/schemas/models.py b/rating_api/schemas/models.py index 3a6f1b0..de510a8 100644 --- a/rating_api/schemas/models.py +++ b/rating_api/schemas/models.py @@ -20,7 +20,6 @@ class CommentGet(Base): lecturer_id: int - class CommentPost(Base): subject: str text: str diff --git a/tests/test_routes/test_comment.py b/tests/test_routes/test_comment.py index e933026..d31c912 100644 --- a/tests/test_routes/test_comment.py +++ b/tests/test_routes/test_comment.py @@ -122,10 +122,14 @@ def test_delete_comment(client, dbsession): dbsession.commit() response = client.delete(f'{url}/{comment.uuid}') assert response.status_code == status.HTTP_200_OK + response = client.get(f'{url}/{comment.uuid}') + assert response.status_code == status.HTTP_404_NOT_FOUND random_uuid = uuid.uuid4() response = client.delete(f'{url}/{random_uuid}') assert response.status_code == status.HTTP_404_NOT_FOUND - comment = Comment.query(session=dbsession).filter(Comment.uuid == comment.uuid).one_or_none() - assert comment is None + comment1 = Comment.query(session=dbsession).filter(Comment.uuid == comment.uuid).one_or_none() + assert comment1 is None + comment.is_deleted = True + dbsession.delete(comment) dbsession.delete(lecturer) dbsession.commit() diff --git a/tests/test_routes/test_lecturer.py b/tests/test_routes/test_lecturer.py index e98858a..39027d0 100644 --- a/tests/test_routes/test_lecturer.py +++ b/tests/test_routes/test_lecturer.py @@ -200,3 +200,8 @@ def test_delete_lecturer(client, dbsession): assert response.status_code == status.HTTP_200_OK response = client.delete(f"{url}/{lecturer.id}") assert response.status_code == status.HTTP_404_NOT_FOUND + lecturer.is_deleted = True + comment.is_deleted = True + dbsession.delete(comment) + dbsession.delete(lecturer) + dbsession.commit()