From f9b75d3f22d2716e651b3455af67952bd880cc91 Mon Sep 17 00:00:00 2001 From: "gal.dahan" <963gal963@gmail.com> Date: Tue, 13 Aug 2024 21:47:48 +0300 Subject: [PATCH 1/3] Make `is_deleted` field optional and configurable in soft delete logic --- fastcrud/crud/fast_crud.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/fastcrud/crud/fast_crud.py b/fastcrud/crud/fast_crud.py index a9333a6..4acd4d5 100644 --- a/fastcrud/crud/fast_crud.py +++ b/fastcrud/crud/fast_crud.py @@ -1,3 +1,4 @@ +import logging from typing import Any, Dict, Generic, Union, Optional, Callable from datetime import datetime, timezone @@ -2343,16 +2344,22 @@ async def delete( f"Expected exactly one record to delete, found {total_count}." ) + update_values = {} + if self.deleted_at_column in self.model_col_names: + update_values[self.deleted_at_column] = datetime.now(timezone.utc) if self.is_deleted_column in self.model_col_names: + update_values[self.is_deleted_column] = True + + if update_values: update_stmt = ( update(self.model) .filter(*filters) - .values(is_deleted=True, deleted_at=datetime.now(timezone.utc)) + .values(**update_values) ) await db.execute(update_stmt) + else: - delete_stmt = delete(self.model).filter(*filters) + delete_stmt = self.model.__table__.delete().where(*filters) await db.execute(delete_stmt) - if commit: - await db.commit() + await db.commit() \ No newline at end of file From ade1d260346753ae87d3718a94ff4389d4ff6026 Mon Sep 17 00:00:00 2001 From: Gal Dahan <963gal963@gmail.com> Date: Tue, 13 Aug 2024 22:05:17 +0300 Subject: [PATCH 2/3] Update fast_crud.py Make `is_deleted` field optional and configurable in soft delete logic --- fastcrud/crud/fast_crud.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/fastcrud/crud/fast_crud.py b/fastcrud/crud/fast_crud.py index 4acd4d5..fde9924 100644 --- a/fastcrud/crud/fast_crud.py +++ b/fastcrud/crud/fast_crud.py @@ -1,4 +1,3 @@ -import logging from typing import Any, Dict, Generic, Union, Optional, Callable from datetime import datetime, timezone @@ -2362,4 +2361,4 @@ async def delete( delete_stmt = self.model.__table__.delete().where(*filters) await db.execute(delete_stmt) if commit: - await db.commit() \ No newline at end of file + await db.commit() From cda7c735f155795c145e878ae88fbed8b79e1536 Mon Sep 17 00:00:00 2001 From: Gal Dahan <963gal963@gmail.com> Date: Sat, 31 Aug 2024 11:16:20 +0300 Subject: [PATCH 3/3] Update fast_crud.py --- fastcrud/crud/fast_crud.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fastcrud/crud/fast_crud.py b/fastcrud/crud/fast_crud.py index fde9924..bb82d64 100644 --- a/fastcrud/crud/fast_crud.py +++ b/fastcrud/crud/fast_crud.py @@ -2343,7 +2343,7 @@ async def delete( f"Expected exactly one record to delete, found {total_count}." ) - update_values = {} + update_values: dict[str, Union[bool, datetime]] = {} if self.deleted_at_column in self.model_col_names: update_values[self.deleted_at_column] = datetime.now(timezone.utc) if self.is_deleted_column in self.model_col_names: