Populate inbound sms history when deleting inbound sms

This commit is contained in:
Pea Tyczynska
2019-12-17 18:04:22 +00:00
parent de9d1a41eb
commit a6b4675ae7
2 changed files with 136 additions and 7 deletions

View File

@@ -2,10 +2,11 @@ from flask import current_app
from notifications_utils.statsd_decorators import statsd
from sqlalchemy import desc, and_
from sqlalchemy.orm import aliased
from sqlalchemy.dialects.postgresql import insert
from app import db
from app.dao.dao_utils import transactional
from app.models import InboundSms, Service, ServiceDataRetention, SMS_TYPE
from app.models import InboundSms, InboundSmsHistory, Service, ServiceDataRetention, SMS_TYPE
from app.utils import midnight_n_days_ago
@@ -78,6 +79,33 @@ def _delete_inbound_sms(datetime_to_delete_from, query_filter):
# set to nonzero just to enter the loop
number_deleted = 1
while number_deleted > 0:
offset = 0
inbound_sms_query = db.session.query(
*[x.name for x in InboundSmsHistory.__table__.c]
).filter(InboundSms.id.in_(subquery))
inbound_sms_count = inbound_sms_query.count()
while offset < inbound_sms_count:
statement = insert(InboundSmsHistory).from_select(
InboundSmsHistory.__table__.c,
inbound_sms_query.limit(query_limit).offset(offset)
)
statement = statement.on_conflict_do_update(
constraint="inbound_sms_history_pkey",
set_={
"created_at": statement.excluded.created_at,
"service_id": statement.excluded.service_id,
"notify_number": statement.excluded.notify_number,
"provider_date": statement.excluded.provider_date,
"provider_reference": statement.excluded.provider_reference,
"provider": statement.excluded.provider
}
)
db.session.connection().execute(statement)
offset += query_limit
number_deleted = InboundSms.query.filter(InboundSms.id.in_(subquery)).delete(synchronize_session='fetch')
deleted += number_deleted
@@ -95,8 +123,8 @@ def delete_inbound_sms_older_than_retention():
).filter(
ServiceDataRetention.notification_type == SMS_TYPE
).all()
deleted = 0
for f in flexible_data_retention:
n_days_ago = midnight_n_days_ago(f.days_of_retention)
@@ -110,12 +138,42 @@ def delete_inbound_sms_older_than_retention():
deleted += _delete_inbound_sms(seven_days_ago, query_filter=[
InboundSms.service_id.notin_(x.service_id for x in flexible_data_retention),
])
current_app.logger.info('Deleted {} inbound sms'.format(deleted))
return deleted
def insert_update_inbound_sms_history(date_to_delete_from, service_id, query_limit=10000):
offset = 0
inbound_sms_query = db.session.query(
*[x.name for x in InboundSmsHistory.__table__.c]
).filter(
InboundSms.service_id == service_id,
InboundSms.created_at < date_to_delete_from,
)
inbound_sms_count = inbound_sms_query.count()
while offset < inbound_sms_count:
statement = insert(InboundSmsHistory).from_select(
InboundSmsHistory.__table__.c,
inbound_sms_query.limit(query_limit).offset(offset)
)
statement = statement.on_conflict_do_update(
constraint="inbound_sms_history_pkey",
set_={
"created_at": statement.excluded.created_at,
"service_id": statement.excluded.service_id,
"notify_number": statement.excluded.notify_number,
"provider_date": statement.excluded.provider_date,
"provider_reference": statement.excluded.provider_reference,
"provider": statement.excluded.provider
}
)
db.session.connection().execute(statement)
offset += query_limit
def dao_get_inbound_sms_by_id(service_id, inbound_id):
return InboundSms.query.filter_by(
id=inbound_id,