Skip to content

Commit

Permalink
Change prune_email_log() to use session.delete() in order to remove r…
Browse files Browse the repository at this point in the history
…ows from any related association tables
  • Loading branch information
ds283 committed Apr 7, 2024
1 parent 94ce34d commit 2cbcced
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions app/tasks/prune_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@
#
# Contributors: David Seery <D.Seery@sussex.ac.uk>
#

from datetime import datetime, timedelta
from typing import List

from flask import current_app

from sqlalchemy.exc import SQLAlchemyError

from ..database import db
from ..models import EmailLog

from datetime import datetime, timedelta


def register_prune_email(celery):
@celery.task(bind=True)
Expand All @@ -34,7 +32,13 @@ def prune_email_log(self, duration=52, interval="weeks"):
limit = now - delta

try:
db.session.query(EmailLog).filter(EmailLog.send_date < limit).delete()
# need to use SQLAlchemy session.delete() if we want to remove rows from the email_log_recipients
# association table
to_delete: List[EmailLog] = db.session.query(EmailLog).filter(EmailLog.send_date < limit)
for email in to_delete:
email: EmailLog
db.session.delete(email)

db.session.commit()

except SQLAlchemyError as e:
Expand Down

0 comments on commit 2cbcced

Please sign in to comment.