more files

This commit is contained in:
Kenneth Kehl
2023-07-18 12:21:14 -07:00
parent a6c76f7969
commit 8546475bc3
2 changed files with 83 additions and 57 deletions

View File

@@ -10,6 +10,7 @@ Create Date: 2016-06-01 14:17:01.963181
from datetime import datetime from datetime import datetime
from alembic import op from alembic import op
from sqlalchemy import text
from app.hashing import hashpw from app.hashing import hashpw
import uuid import uuid
@@ -23,42 +24,45 @@ service_id = 'd6aa2c68-a2d9-4437-ab19-3ae8eb202553'
def upgrade(): def upgrade():
password = hashpw(str(uuid.uuid4())) password = hashpw(str(uuid.uuid4()))
op.get_bind() conn = op.get_bind()
user_insert = """INSERT INTO users (id, name, email_address, created_at, failed_login_count, _password, mobile_number, state, platform_admin) user_insert = """INSERT INTO users (id, name, email_address, created_at, failed_login_count, _password, mobile_number, state, platform_admin)
VALUES ('{}', 'Notify service user', 'testsender@dispostable.com', '{}', 0,'{}', '+441234123412', 'active', False) VALUES (:user_id, 'Notify service user', 'testsender@dispostable.com', :time_now, 0,:password, '+441234123412', 'active', False)
""" """
op.execute(user_insert.format(user_id, datetime.utcnow(), password)) conn.execute(text(user_insert), user_id=user_id, time_now=datetime.utcnow(), password=password)
service_history_insert = """INSERT INTO services_history (id, name, created_at, active, message_limit, restricted, research_mode, email_from, created_by_id, reply_to_email_address, version) service_history_insert = """INSERT INTO services_history (id, name, created_at, active, message_limit, restricted, research_mode, email_from, created_by_id, reply_to_email_address, version)
VALUES ('{}', 'Notify service', '{}', True, 1000, False, False, 'testsender@dispostable.com', VALUES (:service_id, 'Notify service', :time_now, True, 1000, False, False, 'testsender@dispostable.com',
'{}', 'testsender@dispostable.com', 1) :user_id, 'testsender@dispostable.com', 1)
""" """
op.execute(service_history_insert.format(service_id, datetime.utcnow(), user_id)) conn.execute(text(service_history_insert), service_id=service_id, time_now=datetime.utcnow(), user_id=user_id)
service_insert = """INSERT INTO services (id, name, created_at, active, message_limit, restricted, research_mode, email_from, created_by_id, reply_to_email_address, version) service_insert = """INSERT INTO services (id, name, created_at, active, message_limit, restricted, research_mode, email_from, created_by_id, reply_to_email_address, version)
VALUES ('{}', 'Notify service', '{}', True, 1000, False, False, 'testsender@dispostable.com', VALUES (:service_id, 'Notify service', :time_now, True, 1000, False, False, 'testsender@dispostable.com',
'{}', 'testsender@dispostable.com', 1) :user_id, 'testsender@dispostable.com', 1)
""" """
op.execute(service_insert.format(service_id, datetime.utcnow(), user_id)) conn.execute(text(service_insert), service_id=service_id, time_now=datetime.utcnow(), user_id=user_id)
user_to_service_insert = """INSERT INTO user_to_service (user_id, service_id) VALUES ('{}', '{}')""" user_to_service_insert = """INSERT INTO user_to_service (user_id, service_id) VALUES (:user_id, :service_id)"""
op.execute(user_to_service_insert.format(user_id, service_id)) conn.execute(text(user_to_service_insert), user_id=user_id, service_id=service_id)
template_history_insert = """INSERT INTO templates_history (id, name, template_type, created_at, template_history_insert = """INSERT INTO templates_history (id, name, template_type, created_at,
content, archived, service_id, content, archived, service_id,
subject, created_by_id, version) subject, created_by_id, version)
VALUES ('{}', '{}', '{}', '{}', '{}', False, '{}', '{}', '{}', 1) VALUES (:template_id, :template_name, :template_type, :time_now,
:content, False, :service_id, :subject, :user_id, 1)
""" """
template_insert = """INSERT INTO templates (id, name, template_type, created_at, template_insert = """INSERT INTO templates (id, name, template_type, created_at,
content, archived, service_id, subject, created_by_id, version) content, archived, service_id, subject, created_by_id, version)
VALUES ('{}', '{}', '{}', '{}', '{}', False, '{}', '{}', '{}', 1) VALUES (:template_id, :template_name, :template_type, :time_now,
:content, False, :service_id, :subject, :user_id, 1)
""" """
email_verification_content = \ email_verification_content = \
"""Hi ((name)),\n\nTo complete your registration for GOV.UK Notify please click the link below\n\n((url))""" """Hi ((name)),\n\nTo complete your registration for GOV.UK Notify please click the link below\n\n((url))"""
op.execute(template_history_insert.format(uuid.uuid4(), 'Notify email verification code', 'email', conn.execute(text(template_history_insert), template_id=uuid.uuid4(), template_name='Notify email verification code',
datetime.utcnow(), email_verification_content, service_id, template_type='email', time_now=datetime.utcnow(), content=email_verification_content, service_id=service_id,
'Confirm GOV.UK Notify registration', user_id)) subject='Confirm GOV.UK Notify registration', user_id=user_id)
op.execute(template_insert.format('ece42649-22a8-4d06-b87f-d52d5d3f0a27', 'Notify email verification code', 'email', conn.execute(text(template_insert), template_id='ece42649-22a8-4d06-b87f-d52d5d3f0a27',
datetime.utcnow(), email_verification_content, service_id, template_name='Notify email verification code', template_type='email',
'Confirm GOV.UK Notify registration', user_id)) time_now=datetime.utcnow(), content=email_verification_content, service_id=service_id,
subject='Confirm GOV.UK Notify registration', user_id=user_id)
invitation_subject = "((user_name)) has invited you to collaborate on ((service_name)) on GOV.UK Notify" invitation_subject = "((user_name)) has invited you to collaborate on ((service_name)) on GOV.UK Notify"
invitation_content = """((user_name)) has invited you to collaborate on ((service_name)) on GOV.UK Notify.\n\n invitation_content = """((user_name)) has invited you to collaborate on ((service_name)) on GOV.UK Notify.\n\n
@@ -66,19 +70,23 @@ def upgrade():
Click this link to create an account on GOV.UK Notify:\n((url))\n\n Click this link to create an account on GOV.UK Notify:\n((url))\n\n
This invitation will stop working at midnight tomorrow. This is to keep ((service_name)) secure. This invitation will stop working at midnight tomorrow. This is to keep ((service_name)) secure.
""" """
op.execute(template_history_insert.format('4f46df42-f795-4cc4-83bb-65ca312f49cc', 'Notify invitation email', conn.execute(text(template_history_insert), template_id='4f46df42-f795-4cc4-83bb-65ca312f49cc',
'email', datetime.utcnow(), invitation_content, service_id, template_name='Notify invitation email', template_type='email',
invitation_subject, user_id)) time_now=datetime.utcnow(), content=invitation_content, service_id=service_id,
op.execute(template_insert.format('4f46df42-f795-4cc4-83bb-65ca312f49cc', 'Notify invitation email', subject=invitation_subject, user_id=user_id)
'email', datetime.utcnow(), invitation_content, service_id, conn.execute(text(template_insert), template_id='4f46df42-f795-4cc4-83bb-65ca312f49cc',
invitation_subject, user_id)) template_name='Notify invitation email', template_type='email',
time_now=datetime.utcnow(), content=invitation_content, service_id=service_id,
subject=invitation_subject, user_id=user_id)
sms_code_content = '((verify_code)) is your US Notify authentication code' sms_code_content = '((verify_code)) is your US Notify authentication code'
op.execute(template_history_insert.format('36fb0730-6259-4da1-8a80-c8de22ad4246', 'Notify SMS verify code', conn.execute(text(template_history_insert), template_id='36fb0730-6259-4da1-8a80-c8de22ad4246',
'sms', datetime.utcnow(), sms_code_content, service_id, None, user_id)) template_name='Notify SMS verify code', template_type='sms',
time_now=datetime.utcnow(), content=sms_code_content, service_id=service_id, subject=None, user_id=user_id)
op.execute(template_insert.format('36fb0730-6259-4da1-8a80-c8de22ad4246', 'Notify SMS verify code', conn.execute(text(template_insert), template_id='36fb0730-6259-4da1-8a80-c8de22ad4246',
'sms', datetime.utcnow(), sms_code_content, service_id, None, user_id)) template_name='Notify SMS verify code', template_type='sms', time_now=datetime.utcnow(),
content=sms_code_content, service_id=service_id, subject=None, user_id=user_id)
password_reset_content = "Hi ((user_name)),\n\n" \ password_reset_content = "Hi ((user_name)),\n\n" \
"We received a request to reset your password on GOV.UK Notify.\n\n" \ "We received a request to reset your password on GOV.UK Notify.\n\n" \
@@ -87,20 +95,23 @@ def upgrade():
"To reset your password, click this link:\n\n" \ "To reset your password, click this link:\n\n" \
"((url))" "((url))"
op.execute(template_history_insert.format('474e9242-823b-4f99-813d-ed392e7f1201', 'Notify password reset email', conn.execute(text(template_history_insert), template_id='474e9242-823b-4f99-813d-ed392e7f1201',
'email', datetime.utcnow(), password_reset_content, service_id, template_name='Notify password reset email', template_type='email', time_now=datetime.utcnow(),
'Reset your GOV.UK Notify password', user_id)) content=password_reset_content, service_id=service_id,
op.execute(template_insert.format('474e9242-823b-4f99-813d-ed392e7f1201', 'Notify password reset email', subject='Reset your GOV.UK Notify password', user_id=user_id)
'email', datetime.utcnow(), password_reset_content, service_id, conn.execute(text(template_insert), template_id='474e9242-823b-4f99-813d-ed392e7f1201',
'Reset your GOV.UK Notify password', user_id)) template_name='Notify password reset email',
template_type='email', time_now=datetime.utcnow(),
content=password_reset_content, service_id=service_id,
subject='Reset your GOV.UK Notify password', user_id=user_id)
def downgrade(): def downgrade():
op.get_bind() conn = op.get_bind()
op.execute("delete from templates where service_id = '{}'".format(service_id)) conn.execute(text("delete from templates where service_id = :service_id"), service_id=service_id)
op.execute("delete from templates_history where service_id = '{}'".format(service_id)) conn.execute(text("delete from templates_history where service_id = :service_id"), service_id=service_id)
op.execute("delete from user_to_service where service_id = '{}'".format(service_id)) conn.execute(text("delete from user_to_service where service_id = :service_id"), service_id=service_id)
op.execute("delete from services_history where id = '{}'".format(service_id)) conn.execute(text("delete from services_history where id = :service_id"), service_id=service_id)
op.execute("delete from services where id = '{}'".format(service_id)) conn.execute(text("delete from services where id = :service_id"), service_id=service_id)
op.execute("delete from users where id = '{}'".format(user_id)) conn.execute(text("delete from users where id = :service_id"), service_id=service_id)

View File

@@ -7,7 +7,7 @@ Create Date: 2016-08-02 16:36:42.455838
""" """
# revision identifiers, used by Alembic. # revision identifiers, used by Alembic.
from sqlalchemy import text from sqlalchemy import text, bindparam
revision = '0045_billable_units' revision = '0045_billable_units'
down_revision = '0044_jobs_to_notification_hist' down_revision = '0044_jobs_to_notification_hist'
@@ -38,11 +38,11 @@ def upgrade():
SELECT id FROM services_history WHERE id not in (select id from services_history where research_mode) SELECT id FROM services_history WHERE id not in (select id from services_history where research_mode)
''') ''')
# set to 'null' if there are no billable services so we don't get a syntax error in the update statement # set to 'null' if there are no billable services so we don't get a syntax error in the update statement
service_ids = ','.join("'{}'".format(service.id) for service in billable_services) or 'null' service_ids = ','.join(f"{service.id}" for service in billable_services) or 'null'
update_statement = ''' update_statement_n = '''
UPDATE {} UPDATE notifications
SET billable_units = ( SET billable_units = (
CASE CASE
WHEN content_char_count <= 160 THEN 1 WHEN content_char_count <= 160 THEN 1
@@ -50,13 +50,29 @@ def upgrade():
END END
) )
WHERE content_char_count is not null WHERE content_char_count is not null
AND service_id in ({}) AND service_id in (:service_ids)
AND notification_type = 'sms'
'''
update_statement_nh = '''
UPDATE notification_history
SET billable_units = (
CASE
WHEN content_char_count <= 160 THEN 1
ELSE ceil(content_char_count::float / 153::float)
END
)
WHERE content_char_count is not null
AND service_id in (:service_ids)
AND notification_type = 'sms' AND notification_type = 'sms'
''' '''
conn = op.get_bind() conn = op.get_bind()
conn.execute(update_statement.format('notifications', service_ids))
conn.execute(update_statement.format('notification_history', service_ids)) query = text(update_statement_n).bindparams(bindparam("service_ids", expanding=False))
conn.execute(query, service_ids=service_ids)
query = text(update_statement_nh).bindparams(bindparam("service_ids", expanding=False))
conn.execute(query, service_ids=service_ids)
op.drop_column('notifications', 'content_char_count') op.drop_column('notifications', 'content_char_count')
op.drop_column('notification_history', 'content_char_count') op.drop_column('notification_history', 'content_char_count')
@@ -84,7 +100,7 @@ def downgrade():
SELECT id FROM services_history WHERE id not in (select id from services_history where research_mode) SELECT id FROM services_history WHERE id not in (select id from services_history where research_mode)
''') ''')
# set to 'null' if there are no billable services so we don't get a syntax error in the update statement # set to 'null' if there are no billable services so we don't get a syntax error in the update statement
service_ids = ','.join("'{}'".format(service.id) for service in billable_services) or 'null' service_ids = ','.join(f"{service.id}" for service in billable_services) or 'null'
# caveats: # caveats:
# only approximates character counts - billable * 153 to get at least a decent ballpark # only approximates character counts - billable * 153 to get at least a decent ballpark
@@ -92,23 +108,22 @@ def downgrade():
update_statement_n = ''' update_statement_n = '''
UPDATE notifications UPDATE notifications
SET content_char_count = GREATEST(billable_units, 1) * 150 SET content_char_count = GREATEST(billable_units, 1) * 150
WHERE service_id in :service_ids WHERE service_id in (:service_ids)
AND notification_type = 'sms' AND notification_type = 'sms'
''' '''
update_statement_nh = ''' update_statement_nh = '''
UPDATE notification_history UPDATE notification_history
SET content_char_count = GREATEST(billable_units, 1) * 150 SET content_char_count = GREATEST(billable_units, 1) * 150
WHERE service_id in :service_ids WHERE service_id in (:service_ids)
AND notification_type = 'sms' AND notification_type = 'sms'
''' '''
conn = op.get_bind() conn = op.get_bind()
input_params = { query = text(update_statement_n).bindparams(bindparam("service_ids", expanding=False))
"service_ids": service_ids conn.execute(query, service_ids=service_ids)
} query = text(update_statement_nh).bindparams(bindparam("service_ids", expanding=False))
conn.execute(text(update_statement_n), input_params) conn.execute(query, service_ids=service_ids)
conn.execute(text(update_statement_nh), input_params)
op.drop_column('notifications', 'billable_units') op.drop_column('notifications', 'billable_units')
op.drop_column('notification_history', 'billable_units') op.drop_column('notification_history', 'billable_units')