Update ft_notification_status now deletes old version of data

instead of overwriting on top of it
This commit is contained in:
Pea Tyczynska
2018-11-08 10:42:01 +00:00
parent 987445f1bf
commit ca2db56b9d
2 changed files with 8 additions and 18 deletions

View File

@@ -49,12 +49,9 @@ def fetch_notification_status_for_day(process_day, service_id=None):
def update_fact_notification_status(data, process_day): def update_fact_notification_status(data, process_day):
table = FactNotificationStatus.__table__ table = FactNotificationStatus.__table__
''' FactNotificationStatus.query.filter(
This uses the Postgres upsert to avoid race conditions when two threads try to insert FactNotificationStatus.bst_date == process_day.date()
at the same row. The excluded object refers to values that we tried to insert but were ).delete()
rejected.
http://docs.sqlalchemy.org/en/latest/dialects/postgresql.html#insert-on-conflict-upsert
'''
for row in data: for row in data:
stmt = insert(table).values( stmt = insert(table).values(
bst_date=process_day.date(), bst_date=process_day.date(),
@@ -66,13 +63,6 @@ def update_fact_notification_status(data, process_day):
notification_status=row.status, notification_status=row.status,
notification_count=row.notification_count, notification_count=row.notification_count,
) )
stmt = stmt.on_conflict_do_update(
constraint="ft_notification_status_pkey",
set_={"notification_count": stmt.excluded.notification_count,
"updated_at": datetime.utcnow()
}
)
db.session.connection().execute(stmt) db.session.connection().execute(stmt)
db.session.commit() db.session.commit()

View File

@@ -460,24 +460,24 @@ def test_create_nightly_notification_status(notify_db_session):
create_notification(template=first_template, status='delivered') create_notification(template=first_template, status='delivered')
create_notification(template=first_template, status='delivered', created_at=datetime.utcnow() - timedelta(days=1)) create_notification(template=first_template, status='delivered', created_at=datetime.utcnow() - timedelta(days=1))
create_notification(template=first_template, status='delivered', created_at=datetime.utcnow() - timedelta(days=2)) create_notification(template=first_template, status='delivered', created_at=datetime.utcnow() - timedelta(days=2))
create_notification(template=first_template, status='delivered', created_at=datetime.utcnow() - timedelta(days=3))
create_notification(template=first_template, status='delivered', created_at=datetime.utcnow() - timedelta(days=4)) create_notification(template=first_template, status='delivered', created_at=datetime.utcnow() - timedelta(days=4))
create_notification(template=first_template, status='delivered', created_at=datetime.utcnow() - timedelta(days=5))
create_notification(template=second_template, status='temporary-failure') create_notification(template=second_template, status='temporary-failure')
create_notification(template=second_template, status='temporary-failure', create_notification(template=second_template, status='temporary-failure',
created_at=datetime.utcnow() - timedelta(days=1)) created_at=datetime.utcnow() - timedelta(days=1))
create_notification(template=second_template, status='temporary-failure', create_notification(template=second_template, status='temporary-failure',
created_at=datetime.utcnow() - timedelta(days=2)) created_at=datetime.utcnow() - timedelta(days=2))
create_notification(template=second_template, status='temporary-failure',
created_at=datetime.utcnow() - timedelta(days=3))
create_notification(template=second_template, status='temporary-failure', create_notification(template=second_template, status='temporary-failure',
created_at=datetime.utcnow() - timedelta(days=4)) created_at=datetime.utcnow() - timedelta(days=4))
create_notification(template=second_template, status='temporary-failure',
created_at=datetime.utcnow() - timedelta(days=5))
create_notification(template=third_template, status='created') create_notification(template=third_template, status='created')
create_notification(template=third_template, status='created', created_at=datetime.utcnow() - timedelta(days=1)) create_notification(template=third_template, status='created', created_at=datetime.utcnow() - timedelta(days=1))
create_notification(template=third_template, status='created', created_at=datetime.utcnow() - timedelta(days=2)) create_notification(template=third_template, status='created', created_at=datetime.utcnow() - timedelta(days=2))
create_notification(template=third_template, status='created', created_at=datetime.utcnow() - timedelta(days=3))
create_notification(template=third_template, status='created', created_at=datetime.utcnow() - timedelta(days=4)) create_notification(template=third_template, status='created', created_at=datetime.utcnow() - timedelta(days=4))
create_notification(template=third_template, status='created', created_at=datetime.utcnow() - timedelta(days=5))
assert len(FactNotificationStatus.query.all()) == 0 assert len(FactNotificationStatus.query.all()) == 0
@@ -487,6 +487,6 @@ def test_create_nightly_notification_status(notify_db_session):
FactNotificationStatus.notification_type FactNotificationStatus.notification_type
).all() ).all()
assert len(new_data) == 9 assert len(new_data) == 9
assert str(new_data[0].bst_date) == datetime.strftime(datetime.utcnow() - timedelta(days=3), "%Y-%m-%d") assert str(new_data[0].bst_date) == datetime.strftime(datetime.utcnow() - timedelta(days=4), "%Y-%m-%d")
assert str(new_data[3].bst_date) == datetime.strftime(datetime.utcnow() - timedelta(days=2), "%Y-%m-%d") assert str(new_data[3].bst_date) == datetime.strftime(datetime.utcnow() - timedelta(days=2), "%Y-%m-%d")
assert str(new_data[6].bst_date) == datetime.strftime(datetime.utcnow() - timedelta(days=1), "%Y-%m-%d") assert str(new_data[6].bst_date) == datetime.strftime(datetime.utcnow() - timedelta(days=1), "%Y-%m-%d")