mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-20 14:29:25 -04:00
Initial commit for sqlalchemy2.0 migration
This commit is contained in:
@@ -12,7 +12,7 @@ def autocommit(func):
|
|||||||
try:
|
try:
|
||||||
res = func(*args, **kwargs)
|
res = func(*args, **kwargs)
|
||||||
|
|
||||||
if not db.session.registry().transaction.nested:
|
if not db.session.is_active:
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
return res
|
return res
|
||||||
@@ -30,7 +30,7 @@ def transaction():
|
|||||||
yield
|
yield
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
if not db.session.registry().transaction.nested:
|
if not db.session.is_active:
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
except Exception:
|
except Exception:
|
||||||
db.session.rollback()
|
db.session.rollback()
|
||||||
|
|||||||
@@ -17,8 +17,7 @@ session events.
|
|||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
from sqlalchemy import Column, ForeignKeyConstraint, Integer, Table, util
|
from sqlalchemy import Column, ForeignKeyConstraint, Integer, Table, util
|
||||||
from sqlalchemy.ext.declarative import declared_attr
|
from sqlalchemy.orm import attributes, class_mapper, object_mapper, registry
|
||||||
from sqlalchemy.orm import attributes, mapper, object_mapper
|
|
||||||
from sqlalchemy.orm.properties import ColumnProperty, RelationshipProperty
|
from sqlalchemy.orm.properties import ColumnProperty, RelationshipProperty
|
||||||
|
|
||||||
|
|
||||||
@@ -103,10 +102,10 @@ def _history_mapper(local_mapper): # noqa (C901 too complex)
|
|||||||
)
|
)
|
||||||
versioned_cls = type.__new__(type, "%sHistory" % cls.__name__, bases, {})
|
versioned_cls = type.__new__(type, "%sHistory" % cls.__name__, bases, {})
|
||||||
|
|
||||||
m = mapper(
|
m = mapper_registry.map_imperatively(
|
||||||
versioned_cls,
|
versioned_cls,
|
||||||
table,
|
table,
|
||||||
inherits=super_history_mapper,
|
with_polymorphic=("*", super_history_mapper),
|
||||||
polymorphic_on=polymorphic_on,
|
polymorphic_on=polymorphic_on,
|
||||||
polymorphic_identity=local_mapper.polymorphic_identity,
|
polymorphic_identity=local_mapper.polymorphic_identity,
|
||||||
properties=properties,
|
properties=properties,
|
||||||
@@ -170,9 +169,8 @@ def _add_version_for_non_super_history_mapper(super_history_mapper, local_mapper
|
|||||||
|
|
||||||
def _col_copy(col):
|
def _col_copy(col):
|
||||||
orig = col
|
orig = col
|
||||||
col = col.copy()
|
col = Column(col.name, col.type, nullable=col.nullable, unique=False)
|
||||||
orig.info["history_copy"] = col
|
orig.info["history_copy"] = col
|
||||||
col.unique = False
|
|
||||||
|
|
||||||
# if the column is nullable, we could end up overwriting an on-purpose null value with a default.
|
# if the column is nullable, we could end up overwriting an on-purpose null value with a default.
|
||||||
# if it's not nullable, however, the default may be relied upon to correctly set values within the database,
|
# if it's not nullable, however, the default may be relied upon to correctly set values within the database,
|
||||||
@@ -182,15 +180,16 @@ def _col_copy(col):
|
|||||||
return col
|
return col
|
||||||
|
|
||||||
|
|
||||||
class Versioned(object):
|
mapper_registry = registry()
|
||||||
@declared_attr
|
|
||||||
def __mapper_cls__(cls):
|
|
||||||
def map(cls, *arg, **kw):
|
|
||||||
mp = mapper(cls, *arg, **kw)
|
|
||||||
_history_mapper(mp)
|
|
||||||
return mp
|
|
||||||
|
|
||||||
return map
|
|
||||||
|
@mapper_registry.mapped
|
||||||
|
class Versioned(object):
|
||||||
|
__abstract__ = True
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def __declare_last__(cls):
|
||||||
|
_history_mapper(class_mapper(cls))
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_history_model(cls):
|
def get_history_model(cls):
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ def upgrade():
|
|||||||
"jobs", sa.Column("notifications_failed", sa.Integer(), nullable=True)
|
"jobs", sa.Column("notifications_failed", sa.Integer(), nullable=True)
|
||||||
)
|
)
|
||||||
conn = op.get_bind()
|
conn = op.get_bind()
|
||||||
results = conn.execute("select distinct job_id from notifications")
|
results = conn.execute(text("select distinct job_id from notifications"))
|
||||||
res = results.fetchall()
|
res = results.fetchall()
|
||||||
for x in res:
|
for x in res:
|
||||||
if x.job_id:
|
if x.job_id:
|
||||||
|
|||||||
@@ -32,9 +32,7 @@ def upgrade():
|
|||||||
"""
|
"""
|
||||||
conn.execute(
|
conn.execute(
|
||||||
text(user_insert),
|
text(user_insert),
|
||||||
user_id=user_id,
|
{"user_id": user_id, "time_now": datetime.utcnow(), "password": password},
|
||||||
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 (:service_id, 'Notify service', :time_now, True, 1000, False, False, 'testsender@dispostable.com',
|
VALUES (:service_id, 'Notify service', :time_now, True, 1000, False, False, 'testsender@dispostable.com',
|
||||||
@@ -43,9 +41,7 @@ def upgrade():
|
|||||||
"""
|
"""
|
||||||
conn.execute(
|
conn.execute(
|
||||||
text(service_history_insert),
|
text(service_history_insert),
|
||||||
service_id=service_id,
|
{"service_id": service_id, "time_now": datetime.utcnow(), "user_id": user_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 (:service_id, 'Notify service', :time_now, True, 1000, False, False, 'testsender@dispostable.com',
|
VALUES (:service_id, 'Notify service', :time_now, True, 1000, False, False, 'testsender@dispostable.com',
|
||||||
@@ -53,12 +49,12 @@ def upgrade():
|
|||||||
"""
|
"""
|
||||||
conn.execute(
|
conn.execute(
|
||||||
text(service_insert),
|
text(service_insert),
|
||||||
service_id=service_id,
|
{"service_id": service_id, "time_now": datetime.utcnow(), "user_id": user_id},
|
||||||
time_now=datetime.utcnow(),
|
|
||||||
user_id=user_id,
|
|
||||||
)
|
)
|
||||||
user_to_service_insert = """INSERT INTO user_to_service (user_id, service_id) VALUES (:user_id, :service_id)"""
|
user_to_service_insert = """INSERT INTO user_to_service (user_id, service_id) VALUES (:user_id, :service_id)"""
|
||||||
conn.execute(text(user_to_service_insert), user_id=user_id, service_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,
|
||||||
@@ -74,25 +70,29 @@ def upgrade():
|
|||||||
email_verification_content = """Hi ((name)),\n\nTo complete your registration for GOV.UK Notify please click the link below\n\n((url))"""
|
email_verification_content = """Hi ((name)),\n\nTo complete your registration for GOV.UK Notify please click the link below\n\n((url))"""
|
||||||
conn.execute(
|
conn.execute(
|
||||||
text(template_history_insert),
|
text(template_history_insert),
|
||||||
template_id=uuid.uuid4(),
|
{
|
||||||
template_name="Notify email verification code",
|
"template_id": uuid.uuid4(),
|
||||||
template_type="email",
|
"template_name": "Notify email verification code",
|
||||||
time_now=datetime.utcnow(),
|
"template_type": "email",
|
||||||
content=email_verification_content,
|
"time_now": datetime.utcnow(),
|
||||||
service_id=service_id,
|
"content": email_verification_content,
|
||||||
subject="Confirm GOV.UK Notify registration",
|
"service_id": service_id,
|
||||||
user_id=user_id,
|
"subject": "Confirm GOV.UK Notify registration",
|
||||||
|
"user_id": user_id,
|
||||||
|
},
|
||||||
)
|
)
|
||||||
conn.execute(
|
conn.execute(
|
||||||
text(template_insert),
|
text(template_insert),
|
||||||
template_id="ece42649-22a8-4d06-b87f-d52d5d3f0a27",
|
{
|
||||||
template_name="Notify email verification code",
|
"template_id": "ece42649-22a8-4d06-b87f-d52d5d3f0a27",
|
||||||
template_type="email",
|
"template_name": "Notify email verification code",
|
||||||
time_now=datetime.utcnow(),
|
"template_type": "email",
|
||||||
content=email_verification_content,
|
"time_now": datetime.utcnow(),
|
||||||
service_id=service_id,
|
"content": email_verification_content,
|
||||||
subject="Confirm GOV.UK Notify registration",
|
"service_id": service_id,
|
||||||
user_id=user_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"
|
||||||
@@ -103,50 +103,58 @@ def upgrade():
|
|||||||
"""
|
"""
|
||||||
conn.execute(
|
conn.execute(
|
||||||
text(template_history_insert),
|
text(template_history_insert),
|
||||||
template_id="4f46df42-f795-4cc4-83bb-65ca312f49cc",
|
{
|
||||||
template_name="Notify invitation email",
|
"template_id": "4f46df42-f795-4cc4-83bb-65ca312f49cc",
|
||||||
template_type="email",
|
"template_name": "Notify invitation email",
|
||||||
time_now=datetime.utcnow(),
|
"template_type": "email",
|
||||||
content=invitation_content,
|
"time_now": datetime.utcnow(),
|
||||||
service_id=service_id,
|
"content": invitation_content,
|
||||||
subject=invitation_subject,
|
"service_id": service_id,
|
||||||
user_id=user_id,
|
"subject": invitation_subject,
|
||||||
|
"user_id": user_id,
|
||||||
|
},
|
||||||
)
|
)
|
||||||
conn.execute(
|
conn.execute(
|
||||||
text(template_insert),
|
text(template_insert),
|
||||||
template_id="4f46df42-f795-4cc4-83bb-65ca312f49cc",
|
{
|
||||||
template_name="Notify invitation email",
|
"template_id": "4f46df42-f795-4cc4-83bb-65ca312f49cc",
|
||||||
template_type="email",
|
"template_name": "Notify invitation email",
|
||||||
time_now=datetime.utcnow(),
|
"template_type": "email",
|
||||||
content=invitation_content,
|
"time_now": datetime.utcnow(),
|
||||||
service_id=service_id,
|
"content": invitation_content,
|
||||||
subject=invitation_subject,
|
"service_id": service_id,
|
||||||
user_id=user_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"
|
||||||
conn.execute(
|
conn.execute(
|
||||||
text(template_history_insert),
|
text(template_history_insert),
|
||||||
template_id="36fb0730-6259-4da1-8a80-c8de22ad4246",
|
{
|
||||||
template_name="Notify SMS verify code",
|
"template_id": "36fb0730-6259-4da1-8a80-c8de22ad4246",
|
||||||
template_type="sms",
|
"template_name": "Notify SMS verify code",
|
||||||
time_now=datetime.utcnow(),
|
"template_type": "sms",
|
||||||
content=sms_code_content,
|
"time_now": datetime.utcnow(),
|
||||||
service_id=service_id,
|
"content": sms_code_content,
|
||||||
subject=None,
|
"service_id": service_id,
|
||||||
user_id=user_id,
|
"subject": None,
|
||||||
|
"user_id": user_id,
|
||||||
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
conn.execute(
|
conn.execute(
|
||||||
text(template_insert),
|
text(template_insert),
|
||||||
template_id="36fb0730-6259-4da1-8a80-c8de22ad4246",
|
{
|
||||||
template_name="Notify SMS verify code",
|
"template_id": "36fb0730-6259-4da1-8a80-c8de22ad4246",
|
||||||
template_type="sms",
|
"template_name": "Notify SMS verify code",
|
||||||
time_now=datetime.utcnow(),
|
"template_type": "sms",
|
||||||
content=sms_code_content,
|
"time_now": datetime.utcnow(),
|
||||||
service_id=service_id,
|
"content": sms_code_content,
|
||||||
subject=None,
|
"service_id": service_id,
|
||||||
user_id=user_id,
|
"subject": None,
|
||||||
|
"user_id": user_id,
|
||||||
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
password_reset_content = (
|
password_reset_content = (
|
||||||
@@ -160,25 +168,30 @@ def upgrade():
|
|||||||
|
|
||||||
conn.execute(
|
conn.execute(
|
||||||
text(template_history_insert),
|
text(template_history_insert),
|
||||||
template_id="474e9242-823b-4f99-813d-ed392e7f1201",
|
{
|
||||||
template_name="Notify password reset email",
|
"template_id": "474e9242-823b-4f99-813d-ed392e7f1201",
|
||||||
template_type="email",
|
"template_name": "Notify password reset email",
|
||||||
time_now=datetime.utcnow(),
|
"template_type": "email",
|
||||||
content=password_reset_content,
|
"time_now": datetime.utcnow(),
|
||||||
service_id=service_id,
|
"content": password_reset_content,
|
||||||
subject="Reset your GOV.UK Notify password",
|
"service_id": service_id,
|
||||||
user_id=user_id,
|
"subject": "Reset your GOV.UK Notify password",
|
||||||
|
"user_id": user_id,
|
||||||
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
conn.execute(
|
conn.execute(
|
||||||
text(template_insert),
|
text(template_insert),
|
||||||
template_id="474e9242-823b-4f99-813d-ed392e7f1201",
|
{
|
||||||
template_name="Notify password reset email",
|
"template_id": "474e9242-823b-4f99-813d-ed392e7f1201",
|
||||||
template_type="email",
|
"template_name": "Notify password reset email",
|
||||||
time_now=datetime.utcnow(),
|
"template_type": "email",
|
||||||
content=password_reset_content,
|
"time_now": datetime.utcnow(),
|
||||||
service_id=service_id,
|
"content": password_reset_content,
|
||||||
subject="Reset your GOV.UK Notify password",
|
"service_id": service_id,
|
||||||
user_id=user_id,
|
"subject": "Reset your GOV.UK Notify password",
|
||||||
|
"user_id": user_id,
|
||||||
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -26,7 +26,9 @@ def upgrade():
|
|||||||
"sms_requested = 0, sms_delivered = 0, sms_failed=0 where day > '2016-06-30'"
|
"sms_requested = 0, sms_delivered = 0, sms_failed=0 where day > '2016-06-30'"
|
||||||
)
|
)
|
||||||
op.execute(reset_counts)
|
op.execute(reset_counts)
|
||||||
all_notifications = "select * from notifications where date(created_at) > '2016-06-30' order by created_at;"
|
all_notifications = text(
|
||||||
|
"select * from notifications where date(created_at) > '2016-06-30' order by created_at;"
|
||||||
|
)
|
||||||
|
|
||||||
results = conn.execute(all_notifications)
|
results = conn.execute(all_notifications)
|
||||||
res = results.fetchall()
|
res = results.fetchall()
|
||||||
|
|||||||
@@ -34,11 +34,12 @@ def upgrade():
|
|||||||
# caveats
|
# caveats
|
||||||
# only adjusts notifications for services that have never been in research mode. On live, research mode was
|
# only adjusts notifications for services that have never been in research mode. On live, research mode was
|
||||||
# limited to only services that we have set up ourselves so deemed this acceptable.
|
# limited to only services that we have set up ourselves so deemed this acceptable.
|
||||||
billable_services = conn.execute(
|
billable_services_query = text(
|
||||||
"""
|
"""
|
||||||
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)
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
|
billable_services = conn.execute(billable_services_query)
|
||||||
# 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(f"{service.id}" for service in billable_services) or "null"
|
service_ids = ",".join(f"{service.id}" for service in billable_services) or "null"
|
||||||
|
|
||||||
@@ -73,11 +74,11 @@ def upgrade():
|
|||||||
query = text(update_statement_n).bindparams(
|
query = text(update_statement_n).bindparams(
|
||||||
bindparam("service_ids", expanding=False)
|
bindparam("service_ids", expanding=False)
|
||||||
)
|
)
|
||||||
conn.execute(query, service_ids=service_ids)
|
conn.execute(query, {"service_ids": service_ids})
|
||||||
query = text(update_statement_nh).bindparams(
|
query = text(update_statement_nh).bindparams(
|
||||||
bindparam("service_ids", expanding=False)
|
bindparam("service_ids", expanding=False)
|
||||||
)
|
)
|
||||||
conn.execute(query, service_ids=service_ids)
|
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")
|
||||||
|
|
||||||
@@ -130,11 +131,11 @@ def downgrade():
|
|||||||
query = text(update_statement_n).bindparams(
|
query = text(update_statement_n).bindparams(
|
||||||
bindparam("service_ids", expanding=False)
|
bindparam("service_ids", expanding=False)
|
||||||
)
|
)
|
||||||
conn.execute(query, service_ids=service_ids)
|
conn.execute(query, {"service_ids": service_ids})
|
||||||
query = text(update_statement_nh).bindparams(
|
query = text(update_statement_nh).bindparams(
|
||||||
bindparam("service_ids", expanding=False)
|
bindparam("service_ids", expanding=False)
|
||||||
)
|
)
|
||||||
conn.execute(query, service_ids=service_ids)
|
conn.execute(query, {"service_ids": service_ids})
|
||||||
|
|
||||||
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")
|
||||||
|
|||||||
@@ -38,40 +38,46 @@ def upgrade():
|
|||||||
)
|
)
|
||||||
|
|
||||||
conn = op.get_bind()
|
conn = op.get_bind()
|
||||||
conn.execute(
|
|
||||||
|
query = text(
|
||||||
"""
|
"""
|
||||||
INSERT INTO service_permissions (service_id, permission, created_at)
|
INSERT INTO service_permissions (service_id, permission, created_at)
|
||||||
SELECT id, 'sms', '2017-05-26 17:30:00.000000' FROM services
|
SELECT id, 'sms', '2017-05-26 17:30:00.000000' FROM services
|
||||||
WHERE id NOT IN (SELECT service_id FROM service_permissions
|
WHERE id NOT IN (SELECT service_id FROM service_permissions
|
||||||
WHERE service_id=id AND permission='sms')
|
WHERE service_id=id AND permission='sms')
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
|
conn.execute(query)
|
||||||
|
|
||||||
conn.execute(
|
query = text(
|
||||||
"""
|
"""
|
||||||
INSERT INTO service_permissions (service_id, permission, created_at)
|
INSERT INTO service_permissions (service_id, permission, created_at)
|
||||||
SELECT id, 'email', '2017-05-26 17:30:00.000000' FROM services
|
SELECT id, 'email', '2017-05-26 17:30:00.000000' FROM services
|
||||||
WHERE id NOT IN (SELECT service_id FROM service_permissions
|
WHERE id NOT IN (SELECT service_id FROM service_permissions
|
||||||
WHERE service_id=id AND permission='email')
|
WHERE service_id=id AND permission='email')
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
|
conn.execute(query)
|
||||||
|
|
||||||
conn.execute(
|
query = text(
|
||||||
"""
|
"""
|
||||||
INSERT INTO service_permissions (service_id, permission, created_at)
|
INSERT INTO service_permissions (service_id, permission, created_at)
|
||||||
SELECT id, 'letter', '2017-05-26 17:30:00.000000' FROM services
|
SELECT id, 'letter', '2017-05-26 17:30:00.000000' FROM services
|
||||||
WHERE can_send_letters AND id NOT IN (SELECT service_id FROM service_permissions
|
WHERE can_send_letters AND id NOT IN (SELECT service_id FROM service_permissions
|
||||||
WHERE service_id=id AND permission='letter')
|
WHERE service_id=id AND permission='letter')
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
conn.execute(
|
conn.execute(query)
|
||||||
|
|
||||||
|
query = text(
|
||||||
"""
|
"""
|
||||||
INSERT INTO service_permissions (service_id, permission, created_at)
|
INSERT INTO service_permissions (service_id, permission, created_at)
|
||||||
SELECT id, 'international_sms', '2017-05-26 17:30:00.000000' FROM services
|
SELECT id, 'international_sms', '2017-05-26 17:30:00.000000' FROM services
|
||||||
WHERE can_send_international_sms AND id NOT IN (SELECT service_id FROM service_permissions
|
WHERE can_send_international_sms AND id NOT IN (SELECT service_id FROM service_permissions
|
||||||
WHERE service_id=id AND permission='international_sms')
|
WHERE service_id=id AND permission='international_sms')
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
|
conn.execute(query)
|
||||||
|
|
||||||
|
|
||||||
def downgrade():
|
def downgrade():
|
||||||
|
|||||||
@@ -12,15 +12,18 @@ down_revision = "0095_migrate_existing_svc_perms"
|
|||||||
|
|
||||||
import sqlalchemy as sa
|
import sqlalchemy as sa
|
||||||
from alembic import op
|
from alembic import op
|
||||||
|
from sqlalchemy import text
|
||||||
from sqlalchemy.dialects import postgresql
|
from sqlalchemy.dialects import postgresql
|
||||||
|
|
||||||
|
|
||||||
def upgrade():
|
def upgrade():
|
||||||
query = (
|
query = text(
|
||||||
"UPDATE job_statistics "
|
"""
|
||||||
"set sent = sms_sent + emails_sent + letters_sent, "
|
UPDATE job_statistics
|
||||||
" delivered = sms_delivered + emails_delivered, "
|
SET sent = sms_sent + emails_sent + letters_sent,
|
||||||
" failed = sms_failed + emails_failed + letters_failed "
|
delivered = sms_delivered + emails_delivered,
|
||||||
|
failed = sms_failed + emails_failed + letters_failed
|
||||||
|
"""
|
||||||
)
|
)
|
||||||
|
|
||||||
conn = op.get_bind()
|
conn = op.get_bind()
|
||||||
@@ -28,7 +31,14 @@ def upgrade():
|
|||||||
|
|
||||||
|
|
||||||
def downgrade():
|
def downgrade():
|
||||||
query = "UPDATE job_statistics " "set sent = 0, " " delivered = 0, " " failed = 0 "
|
query = text(
|
||||||
|
"""
|
||||||
|
UPDATE job_statistics
|
||||||
|
SET sent = 0,
|
||||||
|
delivered = 0,
|
||||||
|
failed = 0
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
conn = op.get_bind()
|
conn = op.get_bind()
|
||||||
conn.execute(query)
|
conn.execute(query)
|
||||||
|
|||||||
@@ -24,7 +24,8 @@ def upgrade():
|
|||||||
op.add_column("monthly_billing", sa.Column("start_date", sa.DateTime))
|
op.add_column("monthly_billing", sa.Column("start_date", sa.DateTime))
|
||||||
op.add_column("monthly_billing", sa.Column("end_date", sa.DateTime))
|
op.add_column("monthly_billing", sa.Column("end_date", sa.DateTime))
|
||||||
conn = op.get_bind()
|
conn = op.get_bind()
|
||||||
results = conn.execute("Select id, month, year from monthly_billing")
|
query = text("SELECT id, month, year FROM monthly_billing")
|
||||||
|
results = conn.execute(query)
|
||||||
res = results.fetchall()
|
res = results.fetchall()
|
||||||
for x in res:
|
for x in res:
|
||||||
start_date, end_date = get_month_start_and_end_date_in_utc(
|
start_date, end_date = get_month_start_and_end_date_in_utc(
|
||||||
|
|||||||
@@ -48,15 +48,12 @@ def upgrade():
|
|||||||
)
|
)
|
||||||
|
|
||||||
for org_id, org_filename in LOGOS.items():
|
for org_id, org_filename in LOGOS.items():
|
||||||
conn.execute(
|
query = text(
|
||||||
text(
|
"""
|
||||||
"""
|
|
||||||
UPDATE dvla_organisation SET filename = :filename WHERE id = :id
|
UPDATE dvla_organisation SET filename = :filename WHERE id = :id
|
||||||
"""
|
"""
|
||||||
),
|
|
||||||
filename=org_filename,
|
|
||||||
id=org_id,
|
|
||||||
)
|
)
|
||||||
|
conn.execute(query, {"filename": org_filename, "id": org_id})
|
||||||
|
|
||||||
|
|
||||||
def downgrade():
|
def downgrade():
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ Create Date: 2018-12-12 12:00:09.770775
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
from alembic import op
|
from alembic import op
|
||||||
|
from sqlalchemy import text
|
||||||
|
|
||||||
revision = "0246_notifications_index"
|
revision = "0246_notifications_index"
|
||||||
down_revision = "0245_archived_flag_jobs"
|
down_revision = "0245_archived_flag_jobs"
|
||||||
@@ -13,11 +14,13 @@ down_revision = "0245_archived_flag_jobs"
|
|||||||
|
|
||||||
def upgrade():
|
def upgrade():
|
||||||
conn = op.get_bind()
|
conn = op.get_bind()
|
||||||
conn.execute(
|
query = text(
|
||||||
"CREATE INDEX IF NOT EXISTS ix_notifications_service_created_at ON notifications (service_id, created_at)"
|
"CREATE INDEX IF NOT EXISTS ix_notifications_service_created_at ON notifications (service_id, created_at)"
|
||||||
)
|
)
|
||||||
|
conn.execute(query)
|
||||||
|
|
||||||
|
|
||||||
def downgrade():
|
def downgrade():
|
||||||
conn = op.get_bind()
|
conn = op.get_bind()
|
||||||
conn.execute("DROP INDEX IF EXISTS ix_notifications_service_created_at")
|
query = text("DROP INDEX IF EXISTS ix_notifications_service_created_at")
|
||||||
|
conn.execute(query)
|
||||||
|
|||||||
@@ -15,9 +15,8 @@ down_revision = "0302_add_org_id_to_services"
|
|||||||
|
|
||||||
def upgrade():
|
def upgrade():
|
||||||
conn = op.get_bind()
|
conn = op.get_bind()
|
||||||
results = conn.execute(
|
query = text("SELECT service_id, organisation_id FROM organisation_to_service")
|
||||||
"select service_id, organisation_id from organisation_to_service"
|
results = conn.execute(query)
|
||||||
)
|
|
||||||
org_to_service = results.fetchall()
|
org_to_service = results.fetchall()
|
||||||
for x in org_to_service:
|
for x in org_to_service:
|
||||||
sql = """
|
sql = """
|
||||||
@@ -27,8 +26,10 @@ def upgrade():
|
|||||||
"""
|
"""
|
||||||
conn.execute(
|
conn.execute(
|
||||||
text(sql),
|
text(sql),
|
||||||
service_id=str(x.service_id),
|
{
|
||||||
organisation_id=str(x.organisation_id),
|
"service_id": str(x.service_id),
|
||||||
|
"organisation_id": str(x.organisation_id),
|
||||||
|
},
|
||||||
)
|
)
|
||||||
history_sql = """
|
history_sql = """
|
||||||
UPDATE services_history
|
UPDATE services_history
|
||||||
@@ -38,8 +39,10 @@ def upgrade():
|
|||||||
"""
|
"""
|
||||||
conn.execute(
|
conn.execute(
|
||||||
text(history_sql),
|
text(history_sql),
|
||||||
service_id=str(x.service_id),
|
{
|
||||||
organisation_id=str(x.organisation_id),
|
"service_id": str(x.service_id),
|
||||||
|
"organisation_id": str(x.organisation_id),
|
||||||
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -65,8 +68,10 @@ def downgrade():
|
|||||||
"""
|
"""
|
||||||
conn.execute(
|
conn.execute(
|
||||||
text(update_sql),
|
text(update_sql),
|
||||||
service_id=str(x.id),
|
{
|
||||||
organisation_id=str(x.organisation_id),
|
"service_id": str(x.service_id),
|
||||||
|
"organisation_id": str(x.organisation_id),
|
||||||
|
},
|
||||||
)
|
)
|
||||||
elif len(os) == 0:
|
elif len(os) == 0:
|
||||||
insert_sql = """
|
insert_sql = """
|
||||||
@@ -74,8 +79,10 @@ def downgrade():
|
|||||||
"""
|
"""
|
||||||
conn.execute(
|
conn.execute(
|
||||||
text(insert_sql),
|
text(insert_sql),
|
||||||
service_id=str(x.id),
|
{
|
||||||
organisation_id=str(x.organisation_id),
|
"service_id": str(x.service_id),
|
||||||
|
"organisation_id": str(x.organisation_id),
|
||||||
|
},
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
raise Exception(
|
raise Exception(
|
||||||
|
|||||||
@@ -17,9 +17,9 @@ down_revision = "0307_delete_dm_datetime"
|
|||||||
|
|
||||||
def upgrade():
|
def upgrade():
|
||||||
conn = op.get_bind()
|
conn = op.get_bind()
|
||||||
conn.execute("DELETE FROM provider_details WHERE identifier = 'loadtesting'")
|
conn.execute(text("DELETE FROM provider_details WHERE identifier = 'loadtesting'"))
|
||||||
conn.execute(
|
conn.execute(
|
||||||
"DELETE FROM provider_details_history WHERE identifier = 'loadtesting'"
|
text("DELETE FROM provider_details_history WHERE identifier = 'loadtesting'")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -40,5 +40,5 @@ def downgrade():
|
|||||||
(:uuid, 'Loadtesting', 'loadtesting', 100, 'sms', true, 1, false)
|
(:uuid, 'Loadtesting', 'loadtesting', 100, 'sms', true, 1, false)
|
||||||
"""
|
"""
|
||||||
),
|
),
|
||||||
uuid=uuid.uuid4(),
|
{"uuid": str(uuid.uuid4())},
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ def upgrade():
|
|||||||
values(uuid_in(md5(random()::text)::cstring), :updated_at, :service_id, :id, now(), null)
|
values(uuid_in(md5(random()::text)::cstring), :updated_at, :service_id, :id, now(), null)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
results = conn.execute(sql)
|
results = conn.execute(text(sql))
|
||||||
returned_letters = results.fetchall()
|
returned_letters = results.fetchall()
|
||||||
for x in returned_letters:
|
for x in returned_letters:
|
||||||
input_params = {
|
input_params = {
|
||||||
|
|||||||
@@ -23,10 +23,10 @@ def upgrade():
|
|||||||
VALUES (:service_id, 'international_letters', now())
|
VALUES (:service_id, 'international_letters', now())
|
||||||
"""
|
"""
|
||||||
conn = op.get_bind()
|
conn = op.get_bind()
|
||||||
results = conn.execute(sql)
|
results = conn.execute(text(sql))
|
||||||
services_to_add_permission = results.fetchall()
|
services_to_add_permission = results.fetchall()
|
||||||
for x in services_to_add_permission:
|
for x in services_to_add_permission:
|
||||||
conn.execute(text(insert_sql), service_id=x.service_id)
|
conn.execute(text(insert_sql), {"service_id": x.service_id})
|
||||||
|
|
||||||
|
|
||||||
def downgrade():
|
def downgrade():
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import os
|
|||||||
|
|
||||||
import sqlalchemy as sa
|
import sqlalchemy as sa
|
||||||
from alembic import op
|
from alembic import op
|
||||||
|
from sqlalchemy import text
|
||||||
|
|
||||||
revision = "0331_add_broadcast_org"
|
revision = "0331_add_broadcast_org"
|
||||||
down_revision = "0330_broadcast_invite_email"
|
down_revision = "0330_broadcast_invite_email"
|
||||||
@@ -53,17 +54,19 @@ def upgrade():
|
|||||||
"""
|
"""
|
||||||
conn = op.get_bind()
|
conn = op.get_bind()
|
||||||
conn.execute(
|
conn.execute(
|
||||||
sa.text(insert_sql),
|
text(insert_sql),
|
||||||
id=organisation_id,
|
{
|
||||||
name=f"Broadcast Services ({environment})",
|
"id": organisation_id,
|
||||||
active=True,
|
"name": f"Broadcast Services ({environment})",
|
||||||
agreement_signed=None,
|
"active": True,
|
||||||
crown=None,
|
"agreement_signed": None,
|
||||||
organisation_type="central",
|
"crown": None,
|
||||||
|
"organisation_type": "central",
|
||||||
|
},
|
||||||
)
|
)
|
||||||
conn.execute(
|
conn.execute(
|
||||||
sa.text(update_service_set_broadcast_org_sql),
|
text(update_service_set_broadcast_org_sql),
|
||||||
organisation_id=organisation_id,
|
{"organisation_id": organisation_id},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -79,6 +82,6 @@ def downgrade():
|
|||||||
"""
|
"""
|
||||||
conn = op.get_bind()
|
conn = op.get_bind()
|
||||||
conn.execute(
|
conn.execute(
|
||||||
sa.text(update_service_remove_org_sql), organisation_id=organisation_id
|
text(update_service_remove_org_sql), {"organisation_id": organisation_id}
|
||||||
)
|
)
|
||||||
conn.execute(sa.text(delete_sql), organisation_id=organisation_id)
|
conn.execute(text(delete_sql), {"organisation_id": organisation_id})
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ def upgrade():
|
|||||||
values(:service_id, 'test', :provider, now(), null)
|
values(:service_id, 'test', :provider, now(), null)
|
||||||
"""
|
"""
|
||||||
conn = op.get_bind()
|
conn = op.get_bind()
|
||||||
results = conn.execute(sql)
|
results = conn.execute(text(sql))
|
||||||
restrictions = results.fetchall()
|
restrictions = results.fetchall()
|
||||||
for x in restrictions:
|
for x in restrictions:
|
||||||
input_params = {"service_id": x.service_id, "provider": x.provider}
|
input_params = {"service_id": x.service_id, "provider": x.provider}
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ def upgrade():
|
|||||||
WHERE service_permissions.permission = 'broadcast'
|
WHERE service_permissions.permission = 'broadcast'
|
||||||
"""
|
"""
|
||||||
|
|
||||||
services = conn.execute(find_services_sql)
|
services = conn.execute(text(find_services_sql))
|
||||||
for service in services:
|
for service in services:
|
||||||
input_params = {"service_id": service.id}
|
input_params = {"service_id": service.id}
|
||||||
setting = conn.execute(
|
setting = conn.execute(
|
||||||
|
|||||||
@@ -26,14 +26,16 @@ def upgrade():
|
|||||||
|
|
||||||
# modify subject of verification email in templates
|
# modify subject of verification email in templates
|
||||||
conn.execute(
|
conn.execute(
|
||||||
"update templates set subject='Confirm US Notify registration' "
|
text(
|
||||||
"where name = 'Notify email verification code'"
|
"UPDATE templates SET subject='Confirm US Notify registration' WHERE name = 'Notify email verification code'"
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
# modify subject of verification email in templates_history
|
# modify subject of verification email in templates_history
|
||||||
conn.execute(
|
conn.execute(
|
||||||
"update templates_history set subject='Confirm US Notify registration' "
|
text(
|
||||||
"where name = 'Notify email verification code'"
|
"UPDATE templates_history SET subject='Confirm US Notify registration' WHERE name = 'Notify email verification code'"
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
val = """Hi ((name)),\n\nTo complete your registration for US Notify please click the link below\n\n((url))"""
|
val = """Hi ((name)),\n\nTo complete your registration for US Notify please click the link below\n\n((url))"""
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ Create Date: 2023-05-23 10:03:10.485368
|
|||||||
import sqlalchemy as sa
|
import sqlalchemy as sa
|
||||||
from alembic import op
|
from alembic import op
|
||||||
from sqlalchemy.dialects import postgresql
|
from sqlalchemy.dialects import postgresql
|
||||||
|
from sqlalchemy import text
|
||||||
|
|
||||||
revision = "0395_remove_intl_letters_perm"
|
revision = "0395_remove_intl_letters_perm"
|
||||||
down_revision = "0394_remove_contact_list"
|
down_revision = "0394_remove_contact_list"
|
||||||
@@ -21,7 +22,7 @@ def upgrade():
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
conn = op.get_bind()
|
conn = op.get_bind()
|
||||||
conn.execute(sql)
|
conn.execute(text(sql))
|
||||||
|
|
||||||
|
|
||||||
def downgrade():
|
def downgrade():
|
||||||
|
|||||||
2878
poetry.lock
generated
2878
poetry.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -47,7 +47,7 @@ poetry-dotenv-plugin = "==0.2.0"
|
|||||||
psycopg2-binary = "==2.9.9"
|
psycopg2-binary = "==2.9.9"
|
||||||
pyjwt = "==2.8.0"
|
pyjwt = "==2.8.0"
|
||||||
python-dotenv = "==1.0.0"
|
python-dotenv = "==1.0.0"
|
||||||
sqlalchemy = "==1.4.40"
|
sqlalchemy = "==2.0.27"
|
||||||
werkzeug = "^3.0.1"
|
werkzeug = "^3.0.1"
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -94,23 +94,24 @@ def notify_db_session(_notify_db, sms_providers):
|
|||||||
yield _notify_db.session
|
yield _notify_db.session
|
||||||
|
|
||||||
_notify_db.session.remove()
|
_notify_db.session.remove()
|
||||||
for tbl in reversed(_notify_db.metadata.sorted_tables):
|
with _notify_db.engine.begin() as connection:
|
||||||
if tbl.name not in [
|
for tbl in reversed(_notify_db.metadata.sorted_tables):
|
||||||
"provider_details",
|
if tbl.name not in [
|
||||||
"key_types",
|
"provider_details",
|
||||||
"branding_type",
|
"key_types",
|
||||||
"job_status",
|
"branding_type",
|
||||||
"provider_details_history",
|
"job_status",
|
||||||
"template_process_type",
|
"provider_details_history",
|
||||||
"notifications_all_time_view",
|
"template_process_type",
|
||||||
"notification_status_types",
|
"notifications_all_time_view",
|
||||||
"organization_types",
|
"notification_status_types",
|
||||||
"service_permission_types",
|
"organization_types",
|
||||||
"auth_type",
|
"service_permission_types",
|
||||||
"invite_status_type",
|
"auth_type",
|
||||||
"service_callback_type",
|
"invite_status_type",
|
||||||
]:
|
"service_callback_type",
|
||||||
_notify_db.engine.execute(tbl.delete())
|
]:
|
||||||
|
connection.execute(tbl.delete())
|
||||||
_notify_db.session.commit()
|
_notify_db.session.commit()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user