From f2928e6c5535b2e0c6a4ca0615e9a654b2884977 Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Fri, 22 Sep 2017 16:27:23 +0100 Subject: [PATCH] Migration server command to create one row in Service_sms_senders for each service. --- app/commands.py | 37 +++++++++++++++++++++++++++++++++++++ application.py | 1 + 2 files changed, 38 insertions(+) diff --git a/app/commands.py b/app/commands.py index 3dc3a3d37..16724f395 100644 --- a/app/commands.py +++ b/app/commands.py @@ -248,3 +248,40 @@ class PopulateServiceEmailReplyTo(Command): db.session.commit() print("Populated email reply to adderesses for {}".format(result.rowcount)) + + +class PopulateServiceSmsSender(Command): + + def run(self): + services_to_update = """ + INSERT INTO service_sms_senders(id, service_id, sms_sender, inbound_number_id, is_default, created_at) + SELECT uuid_in(md5(random()::text || now()::text)::cstring), service_id, number, id, true, '{}' + FROM inbound_numbers + WHERE service_id NOT IN( + SELECT service_id + FROM service_sms_senders + ) + """.format(datetime.utcnow()) + + services_to_update_from_services = """ + INSERT INTO service_sms_senders(id, service_id, sms_sender, inbound_number_id, is_default, created_at) + SELECT uuid_in(md5(random()::text || now()::text)::cstring), id, sms_sender, null, true, '{}' + FROM services + WHERE id NOT IN( + SELECT service_id + FROM service_sms_senders + ) + """.format(datetime.utcnow()) + + result = db.session.execute(services_to_update) + second_result = db.session.execute(services_to_update_from_services) + db.session.commit() + + services_count_query = db.session.execute("Select count(*) from services").fetchall()[0][0] + + service_sms_sender_count_query = db.session.execute("Select count(*) from service_sms_senders").fetchall()[0][0] + + print("Populated sms sender {} services from inbound_numbers".format(result.rowcount)) + print("Populated sms sender {} services from services".format(second_result.rowcount)) + print("{} services in table".format(services_count_query)) + print("{} service_sms_senders".format(service_sms_sender_count_query)) diff --git a/application.py b/application.py index ab062583b..d6876a85e 100644 --- a/application.py +++ b/application.py @@ -19,6 +19,7 @@ manager.add_command('custom_db_script', commands.CustomDbScript) manager.add_command('populate_monthly_billing', commands.PopulateMonthlyBilling) manager.add_command('backfill_processing_time', commands.BackfillProcessingTime) manager.add_command('populate_service_email_reply_to', commands.PopulateServiceEmailReplyTo) +manager.add_command('populate_service_sms_sender', commands.PopulateServiceSmsSender) @manager.command