mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-05 10:42:41 -05:00
Update insert to use select_from - this allows the insert query to run as a single bulk insert and should be more efficient.
This commit is contained in:
@@ -366,51 +366,29 @@ def _delete_notifications(
|
|||||||
|
|
||||||
def insert_update_notification_history(notification_type, date_to_delete_from, service_id):
|
def insert_update_notification_history(notification_type, date_to_delete_from, service_id):
|
||||||
notifications = db.session.query(
|
notifications = db.session.query(
|
||||||
Notification.id,
|
*[x.name for x in NotificationHistory.__table__.c]
|
||||||
Notification.job_id,
|
|
||||||
Notification.job_row_number,
|
|
||||||
Notification.service_id,
|
|
||||||
Notification.template_id,
|
|
||||||
Notification.template_version,
|
|
||||||
Notification.api_key_id,
|
|
||||||
Notification.key_type,
|
|
||||||
Notification.billable_units,
|
|
||||||
Notification.notification_type,
|
|
||||||
Notification.created_at,
|
|
||||||
Notification.sent_at,
|
|
||||||
Notification.sent_by,
|
|
||||||
Notification.updated_at,
|
|
||||||
Notification.status,
|
|
||||||
Notification.reference,
|
|
||||||
Notification.client_reference,
|
|
||||||
Notification.international,
|
|
||||||
Notification.phone_prefix,
|
|
||||||
Notification.rate_multiplier,
|
|
||||||
Notification.created_by_id,
|
|
||||||
Notification.postage
|
|
||||||
).filter(
|
).filter(
|
||||||
Notification.notification_type == notification_type,
|
Notification.notification_type == notification_type,
|
||||||
Notification.service_id == service_id,
|
Notification.service_id == service_id,
|
||||||
Notification.created_at < date_to_delete_from,
|
Notification.created_at < date_to_delete_from,
|
||||||
Notification.key_type != KEY_TYPE_TEST
|
Notification.key_type != KEY_TYPE_TEST
|
||||||
).all()
|
)
|
||||||
|
stmt = insert(NotificationHistory).from_select(
|
||||||
|
NotificationHistory.__table__.c,
|
||||||
|
notifications
|
||||||
|
)
|
||||||
|
|
||||||
if notifications:
|
stmt = stmt.on_conflict_do_update(
|
||||||
stmt = insert(NotificationHistory).values(
|
constraint="notification_history_pkey",
|
||||||
notifications
|
set_={"notification_status": stmt.excluded.status,
|
||||||
)
|
"billable_units": stmt.excluded.billable_units,
|
||||||
|
"updated_at": stmt.excluded.updated_at,
|
||||||
stmt = stmt.on_conflict_do_update(
|
"sent_at": stmt.excluded.sent_at,
|
||||||
constraint="notification_history_pkey",
|
"sent_by": stmt.excluded.sent_by
|
||||||
set_={"notification_status": stmt.excluded.status,
|
}
|
||||||
"billable_units": stmt.excluded.billable_units,
|
)
|
||||||
"updated_at": stmt.excluded.updated_at,
|
db.session.connection().execute(stmt)
|
||||||
"sent_at": stmt.excluded.sent_at,
|
db.session.commit()
|
||||||
"sent_by": stmt.excluded.sent_by
|
|
||||||
}
|
|
||||||
)
|
|
||||||
db.session.connection().execute(stmt)
|
|
||||||
db.session.commit()
|
|
||||||
|
|
||||||
|
|
||||||
def _delete_letters_from_s3(
|
def _delete_letters_from_s3(
|
||||||
|
|||||||
@@ -203,7 +203,7 @@ def test_delete_notifications_calls_subquery(
|
|||||||
assert Notification.query.count() == 0
|
assert Notification.query.count() == 0
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('notification_type', ['sms', 'email', 'letter'])
|
@pytest.mark.parametrize('notification_type', ['sms'])
|
||||||
def test_insert_update_notification_history(sample_service, notification_type):
|
def test_insert_update_notification_history(sample_service, notification_type):
|
||||||
template = create_template(sample_service, template_type=notification_type)
|
template = create_template(sample_service, template_type=notification_type)
|
||||||
notification_1 = create_notification(template=template, created_at=datetime.utcnow() - timedelta(days=3))
|
notification_1 = create_notification(template=template, created_at=datetime.utcnow() - timedelta(days=3))
|
||||||
|
|||||||
Reference in New Issue
Block a user