Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

soft deletes #22

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions migrations/versions/0c117913717b_adding_is_deleted_fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""adding_is_deleted_fields

Revision ID: 0c117913717b
Revises: 656228b2d6e0
Create Date: 2024-10-24 06:59:27.285029

"""

import sqlalchemy as sa
from alembic import op


# revision identifiers, used by Alembic.
revision = '0c117913717b'
down_revision = '656228b2d6e0'
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())
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nullable False
надо тогда докинуть update на старые строки в базе, чтоб не упало



def downgrade():
op.drop_column('lecturer_user_comment', 'is_deleted')
op.drop_column('lecturer', 'is_deleted')
op.drop_column('comment', 'is_deleted')
21 changes: 18 additions & 3 deletions rating_api/models/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ class Lecturer(BaseDbModel):
middle_name: Mapped[str] = mapped_column(String, nullable=False)
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")
comments: Mapped[list[Comment]] = relationship(
"Comment",
back_populates="lecturer",
primaryjoin="and_(Lecturer.id == Comment.lecturer_id, not_(Comment.is_deleted))",
)
is_deleted: Mapped[bool] = mapped_column(Boolean, default=False)

@hybrid_method
def search(self, query: str) -> bool:
Expand All @@ -56,14 +61,24 @@ class Comment(BaseDbModel):
mark_kindness: Mapped[int] = mapped_column(Integer, nullable=False)
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_id: Mapped[int] = mapped_column(
Integer,
ForeignKey("lecturer.id"),
primary_join="and_(Comment.lecturer_id==Lecturer.id, not_(Lecturer.is_deleted))",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

не туда вписал)
lecturer_id это не отношение
надо джоин вписать в lecturer который ниже

)
lecturer: Mapped[Lecturer] = relationship("Lecturer", back_populates="comments")
review_status: Mapped[ReviewStatus] = mapped_column(DbEnum(ReviewStatus, native_enum=False), nullable=False)
is_deleted: Mapped[bool] = mapped_column(Boolean, default=False)


class LecturerUserComment(BaseDbModel):
id: Mapped[int] = mapped_column(Integer, primary_key=True)
lecturer_id: Mapped[int] = mapped_column(Integer, ForeignKey("lecturer.id"))
lecturer_id: Mapped[int] = mapped_column(
Integer,
ForeignKey("lecturer.id"),
primary_join="and_(LecturerUserComment.lecturer_id==Lecturer.id),not_(Lecturer.is_deleted))",
)
user_id: Mapped[int] = mapped_column(Integer, nullable=False)
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, default=False)
1 change: 0 additions & 1 deletion rating_api/routes/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ async def delete_comment(
if check_comment is None:
raise ObjectNotFound(Comment, uuid)
Comment.delete(session=db.session, id=uuid)

return StatusResponseModel(
status="Success", message="Comment has been deleted", ru="Комментарий удален из RatingAPI"
)
6 changes: 4 additions & 2 deletions tests/test_routes/test_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ def test_delete_comment(client, dbsession):
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 == None
comment.is_deleted = True
dbsession.delete(comment)
dbsession.delete(lecturer)
dbsession.commit()
9 changes: 6 additions & 3 deletions tests/test_routes/test_lecturer.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ def test_get_lecturer_with_comments(client, dbsession):
dbsession.delete(lecturer)
dbsession.commit()


def test_get_lecturers_by_name(client, dbsession):
body_list = [
{"first_name": 'Алиса', "last_name": 'Селезнёва', "middle_name": 'Ивановна', "timetable_id": 0},
Expand Down Expand Up @@ -174,7 +173,8 @@ def test_update_lecturer(client, dbsession):
assert json_response["first_name"] == 'Иван'
assert json_response["last_name"] == 'Иванов'
assert json_response["middle_name"] == "Иванович"
dbsession.delete(lecturer)
db_lecturer: Lecturer = Lecturer.query(session=dbsession).filter(Lecturer.timetable_id == 0).one_or_none()
dbsession.delete(db_lecturer)
dbsession.commit()


Expand All @@ -195,8 +195,11 @@ def test_delete_lecturer(client, dbsession):
comment: Comment = Comment.create(session=dbsession, **comment)
dbsession.commit()
lecturer = dbsession.query(Lecturer).filter(Lecturer.timetable_id == 0).one_or_none()
assert lecturer is not None
assert not lecturer is None
response = client.delete(f"{url}/{lecturer.id}")
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
dbsession.delete(lecturer)
dbsession.commit()
Loading