more files

This commit is contained in:
Kenneth Kehl
2023-07-18 13:16:43 -07:00
parent 8546475bc3
commit 426016c570
2 changed files with 42 additions and 20 deletions

View File

@@ -35,27 +35,26 @@ alter_str = 'ALTER TABLE {table} ALTER COLUMN status TYPE {enum} USING status::t
def upgrade(): def upgrade():
op.execute('ALTER TYPE {enum} RENAME TO {tmp_name}'.format(enum=enum_name, tmp_name=tmp_name)) op.execute('ALTER TYPE notify_status_type RENAME TO tmp_notify_status_type')
new_type.create(op.get_bind()) new_type.create(op.get_bind())
op.execute(alter_str.format(table='notifications', enum=enum_name)) op.execute('ALTER TABLE notifications ALTER COLUMN status TYPE notify_status_type USING status::text::notify_status_type')
op.execute(alter_str.format(table='notification_history', enum=enum_name)) op.execute(
'ALTER TABLE notification_history ALTER COLUMN status TYPE notify_status_type USING status::text::notify_status_type')
op.execute('DROP TYPE ' + tmp_name) op.execute('DROP TYPE tmp_notify_status_type')
def downgrade(): def downgrade():
op.execute('ALTER TYPE {enum} RENAME TO {tmp_name}'.format(enum=enum_name, tmp_name=tmp_name)) op.execute('ALTER TYPE notify_status_type RENAME TO tmp_notify_status_type')
# Convert 'sent' template into 'sending' op.execute("UPDATE notifications SET status='sending' where status='sent'")
update_str = "UPDATE {table} SET status='sending' where status='sent'" op.execute("UPDATE notification_history SET status='sending' where status='sent'")
op.execute(update_str.format(table='notifications'))
op.execute(update_str.format(table='notification_history'))
old_type.create(op.get_bind()) old_type.create(op.get_bind())
op.execute(alter_str.format(table='notifications', enum=enum_name)) op.execute('ALTER TABLE notifications ALTER COLUMN status TYPE notify_status_type USING status::text::notify_status_type')
op.execute(alter_str.format(table='notification_history', enum=enum_name)) op.execute(
'ALTER TABLE notification_history ALTER COLUMN status TYPE notify_status_type USING status::text::notify_status_type')
op.execute('DROP TYPE ' + tmp_name) op.execute('DROP TYPE tmp_notify_status_type')

View File

@@ -7,6 +7,8 @@ Create Date: 2017-05-23 18:13:03.532095
""" """
# revision identifiers, used by Alembic. # revision identifiers, used by Alembic.
from sqlalchemy import text
revision = '0095_migrate_existing_svc_perms' revision = '0095_migrate_existing_svc_perms'
down_revision = '0094_job_stats_update' down_revision = '0094_job_stats_update'
@@ -27,13 +29,34 @@ def upgrade():
"{2} AND id NOT IN (SELECT service_id FROM service_permissions "\ "{2} AND id NOT IN (SELECT service_id FROM service_permissions "\
"WHERE service_id=id AND permission='{0}')".format(permission, migration_date, flag) "WHERE service_id=id AND permission='{0}')".format(permission, migration_date, flag)
op.execute("INSERT INTO service_permissions (service_id, permission, created_at) {}".format(get_values('sms'))) conn = op.get_bind()
op.execute("INSERT INTO service_permissions (service_id, permission, created_at) {}".format(get_values('email'))) conn.execute("""
op.execute("INSERT INTO service_permissions (service_id, permission, created_at) {}".format( INSERT INTO service_permissions (service_id, permission, created_at)
get_values_if_flag('letter', 'can_send_letters'))) SELECT id, 'sms', '2017-05-26 17:30:00.000000' FROM services
op.execute("INSERT INTO service_permissions (service_id, permission, created_at) {}".format( WHERE id NOT IN (SELECT service_id FROM service_permissions
get_values_if_flag('international_sms', 'can_send_international_sms'))) WHERE service_id=id AND permission='sms')
""")
conn.execute("""
INSERT INTO service_permissions (service_id, permission, created_at)
SELECT id, 'email', '2017-05-26 17:30:00.000000' FROM services
WHERE id NOT IN (SELECT service_id FROM service_permissions
WHERE service_id=id AND permission='email')
""")
conn.execute("""
INSERT INTO service_permissions (service_id, permission, created_at)
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 service_id=id AND permission='letter')
""")
conn.execute("""
INSERT INTO service_permissions (service_id, permission, created_at)
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 service_id=id AND permission='international_sms')
""")
def downgrade(): def downgrade():
op.execute("DELETE FROM service_permissions WHERE created_at = '{}'::timestamp".format(migration_date)) op.execute("DELETE FROM service_permissions WHERE created_at = '2017-05-26 17:30:00.000000'::timestamp")