Skip to content

Commit a72e6f4

Browse files
authored
cleaner fix delete from db (#2574)
1 parent 7523adf commit a72e6f4

File tree

1 file changed

+65
-64
lines changed

1 file changed

+65
-64
lines changed

lib/cuckoo/core/database.py

+65-64
Original file line numberDiff line numberDiff line change
@@ -2150,71 +2150,72 @@ def delete_tasks(
21502150
bool: True if the operation was successful (including no tasks to delete), False otherwise.
21512151
"""
21522152
filters_applied = False
2153-
search = self.session.query(Task)
2154-
2155-
if status:
2156-
if "|" in status:
2157-
search = search.filter(Task.status.in_(status.split("|")))
2158-
else:
2159-
search = search.filter(Task.status == status)
2160-
filters_applied = True
2161-
if not_status:
2162-
search = search.filter(Task.status != not_status)
2163-
filters_applied = True
2164-
if category:
2165-
search = search.filter(Task.category.in_([category] if isinstance(category, str) else category))
2166-
filters_applied = True
2167-
if sample_id is not None:
2168-
search = search.filter(Task.sample_id == sample_id)
2169-
filters_applied = True
2170-
if id_before is not None:
2171-
search = search.filter(Task.id < id_before)
2172-
filters_applied = True
2173-
if id_after is not None:
2174-
search = search.filter(Task.id > id_after)
2175-
filters_applied = True
2176-
if completed_after:
2177-
search = search.filter(Task.completed_on > completed_after)
2178-
filters_applied = True
2179-
if added_before:
2180-
search = search.filter(Task.added_on < added_before)
2181-
filters_applied = True
2182-
if options_like:
2183-
# Replace '*' wildcards with wildcard for sql
2184-
options_like = options_like.replace("*", "%")
2185-
search = search.filter(Task.options.like(f"%{options_like}%"))
2186-
filters_applied = True
2187-
if options_not_like:
2188-
# Replace '*' wildcards with wildcard for sql
2189-
options_not_like = options_not_like.replace("*", "%")
2190-
search = search.filter(Task.options.notlike(f"%{options_not_like}%"))
2191-
filters_applied = True
2192-
if tags_tasks_like:
2193-
search = search.filter(Task.tags_tasks.like(f"%{tags_tasks_like}%"))
2194-
filters_applied = True
2195-
if task_ids:
2196-
search = search.filter(Task.id.in_(task_ids))
2197-
filters_applied = True
2198-
if user_id is not None:
2199-
search = search.filter(Task.user_id == user_id)
2200-
filters_applied = True
2201-
2202-
if not filters_applied:
2203-
log.warning("No filters provided for delete_tasks. No tasks will be deleted.")
2204-
return True # Indicate success as no deletion was requested/needed
2153+
with self.session.begin_nested():
2154+
search = self.session.query(Task)
2155+
2156+
if status:
2157+
if "|" in status:
2158+
search = search.filter(Task.status.in_(status.split("|")))
2159+
else:
2160+
search = search.filter(Task.status == status)
2161+
filters_applied = True
2162+
if not_status:
2163+
search = search.filter(Task.status != not_status)
2164+
filters_applied = True
2165+
if category:
2166+
search = search.filter(Task.category.in_([category] if isinstance(category, str) else category))
2167+
filters_applied = True
2168+
if sample_id is not None:
2169+
search = search.filter(Task.sample_id == sample_id)
2170+
filters_applied = True
2171+
if id_before is not None:
2172+
search = search.filter(Task.id < id_before)
2173+
filters_applied = True
2174+
if id_after is not None:
2175+
search = search.filter(Task.id > id_after)
2176+
filters_applied = True
2177+
if completed_after:
2178+
search = search.filter(Task.completed_on > completed_after)
2179+
filters_applied = True
2180+
if added_before:
2181+
search = search.filter(Task.added_on < added_before)
2182+
filters_applied = True
2183+
if options_like:
2184+
# Replace '*' wildcards with wildcard for sql
2185+
options_like = options_like.replace("*", "%")
2186+
search = search.filter(Task.options.like(f"%{options_like}%"))
2187+
filters_applied = True
2188+
if options_not_like:
2189+
# Replace '*' wildcards with wildcard for sql
2190+
options_not_like = options_not_like.replace("*", "%")
2191+
search = search.filter(Task.options.notlike(f"%{options_not_like}%"))
2192+
filters_applied = True
2193+
if tags_tasks_like:
2194+
search = search.filter(Task.tags_tasks.like(f"%{tags_tasks_like}%"))
2195+
filters_applied = True
2196+
if task_ids:
2197+
search = search.filter(Task.id.in_(task_ids))
2198+
filters_applied = True
2199+
if user_id is not None:
2200+
search = search.filter(Task.user_id == user_id)
2201+
filters_applied = True
2202+
2203+
if not filters_applied:
2204+
log.warning("No filters provided for delete_tasks. No tasks will be deleted.")
2205+
return True # Indicate success as no deletion was requested/needed
22052206

2206-
try:
2207-
# Perform the deletion and get the count of deleted rows
2208-
deleted_count = search.delete(synchronize_session=False)
2209-
log.info("Deleted %d tasks matching the criteria.", deleted_count)
2210-
# The commit is handled by the calling context (e.g., `with db.session.begin():`)
2211-
return True
2212-
except Exception as e:
2213-
log.error("Error deleting tasks: %s", str(e))
2214-
# Rollback might be needed if this function is called outside a `with db.session.begin():`
2215-
# but typically it should be called within one.
2216-
# self.session.rollback()
2217-
return False
2207+
try:
2208+
# Perform the deletion and get the count of deleted rows
2209+
deleted_count = search.delete(synchronize_session=False)
2210+
log.info("Deleted %d tasks matching the criteria.", deleted_count)
2211+
# The commit is handled by the calling context (e.g., `with db.session.begin():`)
2212+
return True
2213+
except Exception as e:
2214+
log.error("Error deleting tasks: %s", str(e))
2215+
# Rollback might be needed if this function is called outside a `with db.session.begin():`
2216+
# but typically it should be called within one.
2217+
self.session.rollback()
2218+
return False
22182219

22192220

22202221
def check_tasks_timeout(self, timeout):

0 commit comments

Comments
 (0)