mirror of
https://github.com/GSA/notifications-api.git
synced 2026-01-26 04:21:52 -05:00
Update subquery to be more efficient.
Update subquery to run again but for test keys. Test data is never inserted in Notifications so they need to be deleted separately now given the join to NotificationHistory.
This commit is contained in:
@@ -336,21 +336,35 @@ def delete_notifications_older_than_retention_by_type(notification_type, qry_lim
|
||||
return deleted
|
||||
|
||||
|
||||
def _delete_notifications(
|
||||
deleted, notification_type, date_to_delete_from, service_id, query_limit):
|
||||
|
||||
def _delete_notifications(deleted, notification_type, date_to_delete_from, service_id, query_limit):
|
||||
subquery = db.session.query(
|
||||
Notification.id
|
||||
).join(NotificationHistory, NotificationHistory.id == Notification.id).filter(
|
||||
Notification.notification_type == notification_type,
|
||||
Notification.service_id == service_id,
|
||||
Notification.created_at < date_to_delete_from,
|
||||
).limit(query_limit).subquery()
|
||||
|
||||
deleted += _delete_for_query(subquery)
|
||||
|
||||
subquery_for_test_keys = db.session.query(
|
||||
Notification.id
|
||||
).filter(
|
||||
Notification.notification_type == notification_type,
|
||||
Notification.service_id == service_id,
|
||||
Notification.created_at < date_to_delete_from,
|
||||
NotificationHistory.id == Notification.id
|
||||
Notification.key_type == KEY_TYPE_TEST
|
||||
).limit(query_limit).subquery()
|
||||
|
||||
deleted += _delete_for_query(subquery_for_test_keys)
|
||||
|
||||
return deleted
|
||||
|
||||
|
||||
def _delete_for_query(subquery):
|
||||
number_deleted = db.session.query(Notification).filter(
|
||||
Notification.id.in_(subquery)).delete(synchronize_session='fetch')
|
||||
deleted += number_deleted
|
||||
deleted = number_deleted
|
||||
db.session.commit()
|
||||
while number_deleted > 0:
|
||||
number_deleted = db.session.query(Notification).filter(
|
||||
|
||||
Reference in New Issue
Block a user