Skip to content

Commit

Permalink
Merge pull request #152 from gal-dahan/main
Browse files Browse the repository at this point in the history
Make `is_deleted` Field Optional and Configurable in Soft Delete Logic
  • Loading branch information
igorbenav authored Sep 6, 2024
2 parents a5458a8 + dc3042a commit 4862832
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions fastcrud/crud/fast_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -2359,16 +2359,22 @@ async def delete(
f"Expected exactly one record to delete, found {total_count}."
)

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:
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()

0 comments on commit 4862832

Please sign in to comment.