3.8 KiB
Soft Deletion Commit Fix
Issue Description
The soft deletion functionality for shots and assets was not properly setting the deleted_by and deleted_at fields in the database. This was happening because the soft deletion services were using db.flush() to send SQL statements to the database but were not calling db.commit() to actually commit the transaction.
Root Cause
The issue was in the following services and their corresponding router endpoints:
-
ShotSoftDeletionService (
backend/services/shot_soft_deletion.py)- Method:
soft_delete_shot_cascade() - Used
db.flush()but nodb.commit()
- Method:
-
AssetSoftDeletionService (
backend/services/asset_soft_deletion.py)- Method:
soft_delete_asset_cascade() - Used
db.flush()but nodb.commit()
- Method:
-
RecoveryService (
backend/services/recovery_service.py)- Methods:
recover_shot(),recover_asset(),bulk_recover_shots(),bulk_recover_assets() - Used
db.flush()but nodb.commit()
- Methods:
-
BatchOperationsService (
backend/services/batch_operations.py)- Methods:
batch_delete_shots(),batch_delete_assets(),batch_recover_shots(),batch_recover_assets() - Used
db.flush()but nodb.commit()
- Methods:
The Difference Between flush() and commit()
db.flush(): Sends pending SQL statements to the database within the current transaction, but doesn't commit the transactiondb.commit(): Actually commits the transaction, making the changes permanent in the database
Without db.commit(), the changes were only visible within the current transaction and would be lost when the transaction ended.
Files Fixed
Router Endpoints (Added db.commit() calls):
-
backend/routers/shots.py
delete_shot()endpoint - Added commit after successful soft deletion
-
backend/routers/assets.py
delete_asset()endpoint - Added commit after successful soft deletion
-
backend/routers/admin.py
recover_shot()endpoint - Added commit after successful recoveryrecover_asset()endpoint - Added commit after successful recoverybulk_recover_shots()endpoint - Added commit after successful bulk recoverybulk_recover_assets()endpoint - Added commit after successful bulk recoverybatch_delete_shots()endpoint - Added commit after successful batch deletionbatch_delete_assets()endpoint - Added commit after successful batch deletionbatch_recover_shots_enhanced()endpoint - Added commit after successful batch recoverybatch_recover_assets_enhanced()endpoint - Added commit after successful batch recovery
Pattern Applied:
# Before (incorrect):
result = service.soft_delete_shot_cascade(shot_id, db, current_user)
if not result.success:
raise HTTPException(...)
return response_data
# After (correct):
result = service.soft_delete_shot_cascade(shot_id, db, current_user)
if not result.success:
db.rollback() # Also added rollback on failure
raise HTTPException(...)
# Commit the transaction
db.commit()
return response_data
Testing
Created backend/test_shot_deletion_fix.py to verify the fix works correctly. The test confirms that both deleted_at and deleted_by fields are now properly set when performing soft deletion operations.
Impact
This fix ensures that:
- Soft deletion operations properly set
deleted_byanddeleted_atfields - Recovery operations properly clear these fields
- Batch operations work correctly
- Database transactions are properly committed
- Failed operations are properly rolled back
Prevention
To prevent similar issues in the future:
- Always pair
db.flush()withdb.commit()in router endpoints - Add
db.rollback()calls in error handling - Consider adding database transaction tests to verify commits work correctly
- Review all service methods that modify database state to ensure proper transaction handling