From 9fe9187a19a272d4ef7c04688f8bc18bbf2d4bdb Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Fri, 18 Jan 2019 13:51:06 +0000 Subject: [PATCH 1/5] New server command to remove `.gsi` from the email address of all users for a given service. --- app/commands.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/app/commands.py b/app/commands.py index c977f333b..7f7498860 100644 --- a/app/commands.py +++ b/app/commands.py @@ -634,3 +634,29 @@ def update_jobs_archived_flag(start_date, end_date): total_updated += result.rowcount current_app.logger.info('Total archived jobs = {}'.format(total_updated)) + + +@notify_command(name='update-emails-to-remove-gsi') +@click.option('-s', '--service_id', required=True, help="service id. Update all user.email_address to remove .gsi") +@statsd(namespace="tasks") +def update_emails_to_remove_gsi(service_id): + users_to_update = """SELECT u.id user_id, u.name, email_address, s.id, s.name + FROM users u + JOIN user_to_service us on (u.id = us.user_id) + JOIN services s on (s.id = us.service_id) + WHERE s.id = :service_id + AND u.email_address ilike ('%.gsi%') + """ + results = db.session.execute(users_to_update, {'service_id': service_id}) + print("Updating {} users.".format(results.rowcount)) + + for user in results: + print(user) + + update_stmt = """UPDATE users set email_address = replace(email_address, '.gsi', ''), updated_at = now() + WHERE id = :user_id + """ + r = db.session.execute(update_stmt, {'user_id': str(user.user_id)}) + db.session.commit() + + From abc2dbb1472d10e7e4738907988df65dcff03f61 Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Fri, 18 Jan 2019 14:44:12 +0000 Subject: [PATCH 2/5] Fix update statement to be case insensitive. --- app/commands.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/commands.py b/app/commands.py index 7f7498860..5381a55ac 100644 --- a/app/commands.py +++ b/app/commands.py @@ -653,8 +653,10 @@ def update_emails_to_remove_gsi(service_id): for user in results: print(user) - update_stmt = """UPDATE users set email_address = replace(email_address, '.gsi', ''), updated_at = now() - WHERE id = :user_id + update_stmt = """UPDATE users + SET email_address = replace(replace(email_address, '.gsi', ''), '.GSI', ''), + updated_at = now() + WHERE id = :user_id """ r = db.session.execute(update_stmt, {'user_id': str(user.user_id)}) db.session.commit() From 53858473d013887d8318fe90c72678ccf881f2ed Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Fri, 18 Jan 2019 14:51:05 +0000 Subject: [PATCH 3/5] Make the replace more specific --- app/commands.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/app/commands.py b/app/commands.py index 5381a55ac..659037b64 100644 --- a/app/commands.py +++ b/app/commands.py @@ -645,7 +645,7 @@ def update_emails_to_remove_gsi(service_id): JOIN user_to_service us on (u.id = us.user_id) JOIN services s on (s.id = us.service_id) WHERE s.id = :service_id - AND u.email_address ilike ('%.gsi%') + AND u.email_address ilike ('%.gsi.gov.uk%') """ results = db.session.execute(users_to_update, {'service_id': service_id}) print("Updating {} users.".format(results.rowcount)) @@ -653,10 +653,11 @@ def update_emails_to_remove_gsi(service_id): for user in results: print(user) - update_stmt = """UPDATE users - SET email_address = replace(replace(email_address, '.gsi', ''), '.GSI', ''), - updated_at = now() - WHERE id = :user_id + update_stmt = """ + UPDATE users + SET email_address = replace(replace(email_address, '.gsi.gov.uk', '.gov.uk'), '.GSI.GOV.UK', '.GOV.UK'), + updated_at = now() + WHERE id = :user_id """ r = db.session.execute(update_stmt, {'user_id': str(user.user_id)}) db.session.commit() From c36660708f0d68bcc7708284163d7e02a1ccc8c2 Mon Sep 17 00:00:00 2001 From: Katie Smith Date: Wed, 6 Feb 2019 14:13:45 +0000 Subject: [PATCH 4/5] Fix codestyle --- app/commands.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/app/commands.py b/app/commands.py index 659037b64..f8ec87ab4 100644 --- a/app/commands.py +++ b/app/commands.py @@ -641,7 +641,7 @@ def update_jobs_archived_flag(start_date, end_date): @statsd(namespace="tasks") def update_emails_to_remove_gsi(service_id): users_to_update = """SELECT u.id user_id, u.name, email_address, s.id, s.name - FROM users u + FROM users u JOIN user_to_service us on (u.id = us.user_id) JOIN services s on (s.id = us.service_id) WHERE s.id = :service_id @@ -654,12 +654,10 @@ def update_emails_to_remove_gsi(service_id): print(user) update_stmt = """ - UPDATE users - SET email_address = replace(replace(email_address, '.gsi.gov.uk', '.gov.uk'), '.GSI.GOV.UK', '.GOV.UK'), + UPDATE users + SET email_address = replace(replace(email_address, '.gsi.gov.uk', '.gov.uk'), '.GSI.GOV.UK', '.GOV.UK'), updated_at = now() WHERE id = :user_id """ - r = db.session.execute(update_stmt, {'user_id': str(user.user_id)}) + db.session.execute(update_stmt, {'user_id': str(user.user_id)}) db.session.commit() - - From 510e6bd7bfb3593846f8d5dc7b268ae8b75f916b Mon Sep 17 00:00:00 2001 From: Katie Smith Date: Wed, 6 Feb 2019 15:34:34 +0000 Subject: [PATCH 5/5] Update print statement in command --- app/commands.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/commands.py b/app/commands.py index f8ec87ab4..67a35efed 100644 --- a/app/commands.py +++ b/app/commands.py @@ -651,7 +651,7 @@ def update_emails_to_remove_gsi(service_id): print("Updating {} users.".format(results.rowcount)) for user in results: - print(user) + print('User with id {} updated'.format(user.user_id)) update_stmt = """ UPDATE users