From bd9d3bb5075b12028f698e6024258768e04b1d75 Mon Sep 17 00:00:00 2001 From: Imdad Ahad Date: Thu, 29 Jun 2017 15:44:40 +0100 Subject: [PATCH 1/2] Updates: * Refactor custom db script to allow passing in a specific command function * Add script to update new Notification status --- app/commands.py | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/app/commands.py b/app/commands.py index 04b5186ab..359c516d5 100644 --- a/app/commands.py +++ b/app/commands.py @@ -66,14 +66,40 @@ class PurgeFunctionalTestDataCommand(Command): class CustomDbScript(Command): - def run(self): - self.update_notification_international_flag() + + option_list = ( + Option('-n', '-name-of-db-function', dest='name_of_db_function', help="Function name of the DB script to run"), + ) + + def run(self, name_of_db_function): + db_function = getattr(self, name_of_db_function, None) + if callable(db_function): + db_function() + else: + print('The specified function does not exist.') + + def backfill_notification_statuses(self): + """ + This will be used to populate the new `Notification._status_fkey` with the old + `Notification._status_enum` + """ + LIMIT = 250000 + subq = "SELECT id FROM notification_history WHERE notification_status is NULL LIMIT {}".format(LIMIT) + update = "UPDATE notification_history SET notification_status = status WHERE id in ({})".format(subq) + result = db.session.execute(subq).fetchall() + + while len(result) > 0: + db.session.execute(update) + print('commit {} updates at {}'.format(LIMIT, datetime.utcnow())) + db.session.commit() + result = db.session.execute(subq).fetchall() def update_notification_international_flag(self): # 250,000 rows takes 30 seconds to update. subq = "select id from notifications where international is null limit 250000" update = "update notifications set international = False where id in ({})".format(subq) result = db.session.execute(subq).fetchall() + while len(result) > 0: db.session.execute(update) print('commit 250000 updates at {}'.format(datetime.utcnow())) From f78ff4daac0850e1b779743596ab0b60ed9d77c1 Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Mon, 3 Jul 2017 13:40:13 +0100 Subject: [PATCH 2/2] Refactor order of delete statements --- app/dao/services_dao.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/dao/services_dao.py b/app/dao/services_dao.py index 7429a1e10..41a0af27c 100644 --- a/app/dao/services_dao.py +++ b/app/dao/services_dao.py @@ -220,9 +220,9 @@ def delete_service_and_all_associated_db_objects(service): _delete_commit(Permission.query.filter_by(service=service)) _delete_commit(ApiKey.query.filter_by(service=service)) _delete_commit(ApiKey.get_history_model().query.filter_by(service_id=service.id)) - _delete_commit(Job.query.filter_by(service=service)) _delete_commit(NotificationHistory.query.filter_by(service=service)) _delete_commit(Notification.query.filter_by(service=service)) + _delete_commit(Job.query.filter_by(service=service)) _delete_commit(Template.query.filter_by(service=service)) _delete_commit(TemplateHistory.query.filter_by(service_id=service.id)) _delete_commit(ServicePermission.query.filter_by(service_id=service.id))