More cleanup of migration.

Signed-off-by: Cliff Hill <Clifford.hill@gsa.gov>
This commit is contained in:
Cliff Hill
2024-01-19 15:17:29 -05:00
parent 8c50f393dc
commit d7ad4edf80

View File

@@ -7,6 +7,7 @@ Create Date: 2024-01-18 12:34:32.857422
""" """
from enum import Enum from enum import Enum
from typing import TypedDict from typing import TypedDict
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
@@ -15,6 +16,7 @@ from app.enums import (
AuthType, AuthType,
BrandType, BrandType,
CallbackType, CallbackType,
CodeType,
InvitedUserStatus, InvitedUserStatus,
JobStatus, JobStatus,
KeyType, KeyType,
@@ -111,8 +113,9 @@ _enum_params: dict[Enum, EnumValues] = {
TemplateType: {"values": ["sms", "email", "letter"], "name": "template_types"}, TemplateType: {"values": ["sms", "email", "letter"], "name": "template_types"},
TemplateProcessType: { TemplateProcessType: {
"values": ["normal", "priority"], "values": ["normal", "priority"],
"name": "template_process_types" "name": "template_process_types",
}, },
CodeType: {"values": ["email", "sms"], "name": "code_types"},
} }
@@ -165,9 +168,6 @@ def upgrade():
op.drop_constraint( op.drop_constraint(
"templates_history_process_type_fkey", "templates_history", type_="foreignkey" "templates_history_process_type_fkey", "templates_history", type_="foreignkey"
) )
op.drop_constraint(
"uix_user_to_organisation", "user_to_organization", type_="unique"
)
op.drop_constraint("users_auth_type_fkey", "users", type_="foreignkey") op.drop_constraint("users_auth_type_fkey", "users", type_="foreignkey")
# Drop composite indexes # Drop composite indexes
@@ -175,8 +175,6 @@ def upgrade():
op.drop_index( op.drop_index(
"ix_notifications_notification_type_composite", table_name="notifications" "ix_notifications_notification_type_composite", table_name="notifications"
) )
op.drop_index("ix_services_organisation_id", table_name="services")
op.drop_index("ix_services_history_organisation_id", table_name="services_history")
# drop old "helper" tables # drop old "helper" tables
op.drop_table("template_process_type") op.drop_table("template_process_type")
@@ -248,8 +246,8 @@ def upgrade():
existing_nullable=False, existing_nullable=False,
) )
op.alter_column( op.alter_column(
'notification_history', "notification_history",
'notification_status', "notification_status",
existing_type=sa.TEXT(), existing_type=sa.TEXT(),
type_=enum_type(NotificationStatus), type_=enum_type(NotificationStatus),
existing_nullable=True, existing_nullable=True,
@@ -271,8 +269,8 @@ def upgrade():
existing_nullable=False, existing_nullable=False,
) )
op.alter_column( op.alter_column(
'notifications', "notifications",
'notification_status', "notification_status",
existing_type=sa.TEXT(), existing_type=sa.TEXT(),
type_=enum_type(NotificationStatus), type_=enum_type(NotificationStatus),
existing_nullable=True, existing_nullable=True,
@@ -321,13 +319,6 @@ def upgrade():
type_=enum_type(NotificationType), type_=enum_type(NotificationType),
existing_nullable=False, existing_nullable=False,
) )
op.alter_column(
"rates",
"rate",
existing_type=sa.NUMERIC(),
type_=sa.Float(),
existing_nullable=False,
)
op.alter_column( op.alter_column(
"rates", "rates",
"notification_type", "notification_type",
@@ -367,13 +358,6 @@ def upgrade():
type_=enum_type(ServicePermissionType), type_=enum_type(ServicePermissionType),
existing_nullable=False, existing_nullable=False,
) )
op.alter_column(
"service_sms_senders",
"sms_sender",
existing_type=sa.VARCHAR(length=255),
type_=sa.String(length=11),
existing_nullable=False,
)
op.alter_column( op.alter_column(
"service_whitelist", "service_whitelist",
"recipient_type", "recipient_type",
@@ -381,13 +365,6 @@ def upgrade():
type_=enum_type(RecipientType), type_=enum_type(RecipientType),
existing_nullable=False, existing_nullable=False,
) )
op.alter_column(
"services",
"total_message_limit",
existing_type=sa.INTEGER(),
type_=sa.BigInteger(),
nullable=False,
)
op.alter_column( op.alter_column(
"services", "services",
"organization_type", "organization_type",
@@ -395,16 +372,6 @@ def upgrade():
type_=enum_type(OrganizationType), type_=enum_type(OrganizationType),
existing_nullable=True, existing_nullable=True,
) )
op.alter_column(
"services_history",
"total_message_limit",
existing_type=sa.INTEGER(),
type_=sa.BigInteger(),
nullable=False,
)
op.alter_column(
"services_history", "prefix_sms", existing_type=sa.BOOLEAN(), nullable=False
)
op.alter_column( op.alter_column(
"services_history", "services_history",
"organization_type", "organization_type",
@@ -444,12 +411,6 @@ def upgrade():
type_=enum_type(TemplateProcessType), type_=enum_type(TemplateProcessType),
existing_nullable=False, existing_nullable=False,
) )
op.alter_column(
"user_to_service", "user_id", existing_type=postgresql.UUID(), nullable=False
)
op.alter_column(
"user_to_service", "service_id", existing_type=postgresql.UUID(), nullable=False
)
op.alter_column( op.alter_column(
"users", "users",
"auth_type", "auth_type",
@@ -462,7 +423,7 @@ def upgrade():
"verify_codes", "verify_codes",
"code_type", "code_type",
existing_type=postgresql.ENUM("email", "sms", name="verify_code_types"), existing_type=postgresql.ENUM("email", "sms", name="verify_code_types"),
type_=sa.Enum("email", "sms", name="code_types"), type_=enum_type(CodeType),
existing_nullable=False, existing_nullable=False,
) )
@@ -479,34 +440,121 @@ def upgrade():
["notification_type", "status", "created_at"], ["notification_type", "status", "created_at"],
unique=False, unique=False,
) )
op.create_index(
op.f("ix_services_organization_id"),
"services",
["organization_id"],
unique=False,
)
op.create_index(
op.f("ix_services_history_organization_id"),
"services_history",
["organization_id"],
unique=False,
)
# ### end Alembic commands ### # ### end Alembic commands ###
def downgrade(): def downgrade():
# ### commands auto generated by Alembic - please adjust! ### # Recreate foreign keys
op.create_foreign_key(
"users_auth_type_fkey", "users", "auth_type", ["auth_type"], ["name"]
)
op.create_foreign_key(
"templates_history_process_type_fkey",
"templates_history",
"template_process_type",
["process_type"],
["name"],
)
op.create_foreign_key(
"templates_process_type_fkey",
"templates",
"template_process_type",
["process_type"],
["name"],
)
op.create_foreign_key(
"services_organisation_type_fkey",
"services",
"organization_types",
["organization_type"],
["name"],
)
op.create_foreign_key(
"service_permissions_permission_fkey",
"service_permissions",
"service_permission_types",
["permission"],
["name"],
)
op.create_foreign_key(
"service_callback_api_type_fk",
"service_callback_api",
"service_callback_type",
["callback_type"],
["name"],
)
op.create_foreign_key(
"organisation_organisation_type_fkey",
"organization",
"organization_types",
["organization_type"],
["name"],
)
op.create_foreign_key(
"fk_notifications_notification_status",
"notifications",
"notification_status_types",
["notification_status"],
["name"],
)
op.create_foreign_key(
"notifications_key_type_fkey",
"notifications",
"key_types",
["key_type"],
["name"],
)
op.create_foreign_key(
"fk_notification_history_notification_status",
"notification_history",
"notification_status_types",
["notification_status"],
["name"],
)
op.create_foreign_key(
"notification_history_key_type_fkey",
"notification_history",
"key_types",
["key_type"],
["name"],
)
op.create_foreign_key(
"jobs_job_status_fkey", "jobs", "job_status", ["job_status"], ["name"]
)
op.create_foreign_key(
"invited_users_auth_type_fkey",
"invited_users",
"auth_type",
["auth_type"],
["name"],
)
op.create_foreign_key(
"invited_organisation_users_status_fkey",
"invited_organization_users",
"invite_status_type",
["status"],
["name"],
)
op.create_foreign_key(
"email_branding_brand_type_fkey",
"email_branding",
"branding_type",
["brand_type"],
["name"],
)
op.create_foreign_key(
"api_keys_key_type_fkey", "api_keys", "key_types", ["key_type"], ["name"]
)
# Alter columns back
op.alter_column( op.alter_column(
"verify_codes", "verify_codes",
"code_type", "code_type",
existing_type=sa.Enum("email", "sms", name="code_types"), existing_type=enum_type(CodeType),
type_=postgresql.ENUM("email", "sms", name="verify_code_types"), type_=postgresql.ENUM("email", "sms", name="verify_code_types"),
existing_nullable=False, existing_nullable=False,
) )
op.create_foreign_key(
"users_auth_type_fkey", "users", "auth_type", ["auth_type"], ["name"]
)
op.alter_column( op.alter_column(
"users", "users",
"auth_type", "auth_type",
@@ -515,27 +563,6 @@ def downgrade():
existing_nullable=False, existing_nullable=False,
existing_server_default=sa.text("'sms_auth'::character varying"), existing_server_default=sa.text("'sms_auth'::character varying"),
) )
op.alter_column(
"user_to_service", "service_id", existing_type=postgresql.UUID(), nullable=True
)
op.alter_column(
"user_to_service", "user_id", existing_type=postgresql.UUID(), nullable=True
)
op.drop_constraint(
"uix_user_to_organization", "user_to_organization", type_="unique"
)
op.create_unique_constraint(
"uix_user_to_organisation",
"user_to_organization",
["user_id", "organization_id"],
)
op.create_foreign_key(
"templates_history_process_type_fkey",
"templates_history",
"template_process_type",
["process_type"],
["name"],
)
op.alter_column( op.alter_column(
"templates_history", "templates_history",
"process_type", "process_type",
@@ -552,13 +579,6 @@ def downgrade():
), ),
existing_nullable=False, existing_nullable=False,
) )
op.create_foreign_key(
"templates_process_type_fkey",
"templates",
"template_process_type",
["process_type"],
["name"],
)
op.alter_column( op.alter_column(
"templates", "templates",
"process_type", "process_type",
@@ -575,15 +595,6 @@ def downgrade():
), ),
existing_nullable=False, existing_nullable=False,
) )
op.drop_index(
op.f("ix_services_history_organization_id"), table_name="services_history"
)
op.create_index(
"ix_services_history_organisation_id",
"services_history",
["organization_id"],
unique=False,
)
op.alter_column( op.alter_column(
"services_history", "services_history",
"organization_type", "organization_type",
@@ -591,27 +602,6 @@ def downgrade():
type_=sa.VARCHAR(length=255), type_=sa.VARCHAR(length=255),
existing_nullable=True, existing_nullable=True,
) )
op.alter_column(
"services_history", "prefix_sms", existing_type=sa.BOOLEAN(), nullable=True
)
op.alter_column(
"services_history",
"total_message_limit",
existing_type=sa.BigInteger(),
type_=sa.INTEGER(),
nullable=True,
)
op.create_foreign_key(
"services_organisation_type_fkey",
"services",
"organization_types",
["organization_type"],
["name"],
)
op.drop_index(op.f("ix_services_organization_id"), table_name="services")
op.create_index(
"ix_services_organisation_id", "services", ["organization_id"], unique=False
)
op.alter_column( op.alter_column(
"services", "services",
"organization_type", "organization_type",
@@ -619,13 +609,6 @@ def downgrade():
type_=sa.VARCHAR(length=255), type_=sa.VARCHAR(length=255),
existing_nullable=True, existing_nullable=True,
) )
op.alter_column(
"services",
"total_message_limit",
existing_type=sa.BigInteger(),
type_=sa.INTEGER(),
nullable=True,
)
op.alter_column( op.alter_column(
"service_whitelist", "service_whitelist",
"recipient_type", "recipient_type",
@@ -633,20 +616,6 @@ def downgrade():
type_=postgresql.ENUM("mobile", "email", name="recipient_type"), type_=postgresql.ENUM("mobile", "email", name="recipient_type"),
existing_nullable=False, existing_nullable=False,
) )
op.alter_column(
"service_sms_senders",
"sms_sender",
existing_type=sa.String(length=11),
type_=sa.VARCHAR(length=255),
existing_nullable=False,
)
op.create_foreign_key(
"service_permissions_permission_fkey",
"service_permissions",
"service_permission_types",
["permission"],
["name"],
)
op.alter_column( op.alter_column(
"service_permissions", "service_permissions",
"permission", "permission",
@@ -668,13 +637,6 @@ def downgrade():
type_=sa.VARCHAR(), type_=sa.VARCHAR(),
existing_nullable=True, existing_nullable=True,
) )
op.create_foreign_key(
"service_callback_api_type_fk",
"service_callback_api",
"service_callback_type",
["callback_type"],
["name"],
)
op.alter_column( op.alter_column(
"service_callback_api", "service_callback_api",
"callback_type", "callback_type",
@@ -689,13 +651,6 @@ def downgrade():
type_=postgresql.ENUM("email", "sms", "letter", name="notification_type"), type_=postgresql.ENUM("email", "sms", "letter", name="notification_type"),
existing_nullable=False, existing_nullable=False,
) )
op.alter_column(
"rates",
"rate",
existing_type=sa.Float(),
type_=sa.NUMERIC(),
existing_nullable=False,
)
op.alter_column( op.alter_column(
"provider_details_history", "provider_details_history",
"notification_type", "notification_type",
@@ -710,13 +665,6 @@ def downgrade():
type_=postgresql.ENUM("email", "sms", "letter", name="notification_type"), type_=postgresql.ENUM("email", "sms", "letter", name="notification_type"),
existing_nullable=False, existing_nullable=False,
) )
op.create_foreign_key(
"organisation_organisation_type_fkey",
"organization",
"organization_types",
["organization_type"],
["name"],
)
op.alter_column( op.alter_column(
"organization", "organization",
"organization_type", "organization_type",
@@ -725,48 +673,11 @@ def downgrade():
existing_nullable=True, existing_nullable=True,
) )
op.alter_column( op.alter_column(
'notifications', "notifications",
'notification_status', "notification_status",
existing_type=enum_type(NotificationStatus), existing_type=enum_type(NotificationStatus),
type_=sa.TEXT(), type_=sa.TEXT(),
existing_nullable=True existing_nullable=True,
)
op.add_column(
"notifications",
sa.Column("queue_name", sa.TEXT(), autoincrement=False, nullable=True),
)
op.create_foreign_key(
"fk_notifications_notification_status",
"notifications",
"notification_status_types",
["notification_status"],
["name"],
)
op.create_foreign_key(
"notifications_key_type_fkey",
"notifications",
"key_types",
["key_type"],
["name"],
)
op.drop_index("ix_notifications_service_id_composite", table_name="notifications")
op.create_index(
"ix_notifications_service_id_composite",
"notifications",
["service_id", "notification_type", "notification_status", "created_at"],
unique=False,
)
op.drop_index(
"ix_notifications_notification_type_composite", table_name="notifications"
)
op.create_index(
"ix_notifications_notification_type_composite",
"notifications",
["notification_type", "notification_status", "created_at"],
unique=False,
)
op.alter_column(
"notifications", "international", existing_type=sa.BOOLEAN(), nullable=True
) )
op.alter_column( op.alter_column(
"notifications", "notifications",
@@ -783,30 +694,11 @@ def downgrade():
existing_nullable=False, existing_nullable=False,
) )
op.alter_column( op.alter_column(
'notification_history', "notification_history",
'notification_status', "notification_status",
existing_type=enum_type(NotificationStatus), existing_type=enum_type(NotificationStatus),
type_=sa.TEXT(), type_=sa.TEXT(),
existing_nullable=True existing_nullable=True,
)
op.add_column(
"notification_history",
sa.Column("carrier", sa.TEXT(), autoincrement=False, nullable=True),
)
op.create_foreign_key(
"fk_notification_history_notification_status",
"notification_history",
"notification_status_types",
["notification_status"],
["name"],
)
op.create_foreign_key(
"notification_history_key_type_fkey",
"notification_history",
"key_types",
["key_type"],
["name"],
) )
op.alter_column( op.alter_column(
"notification_history", "notification_history",
@@ -822,9 +714,6 @@ def downgrade():
type_=sa.VARCHAR(), type_=sa.VARCHAR(),
existing_nullable=False, existing_nullable=False,
) )
op.create_foreign_key(
"jobs_job_status_fkey", "jobs", "job_status", ["job_status"], ["name"]
)
op.alter_column( op.alter_column(
"jobs", "jobs",
"job_status", "job_status",
@@ -832,13 +721,6 @@ def downgrade():
type_=sa.VARCHAR(length=255), type_=sa.VARCHAR(length=255),
existing_nullable=False, existing_nullable=False,
) )
op.create_foreign_key(
"invited_users_auth_type_fkey",
"invited_users",
"auth_type",
["auth_type"],
["name"],
)
op.alter_column( op.alter_column(
"invited_users", "invited_users",
"auth_type", "auth_type",
@@ -860,13 +742,6 @@ def downgrade():
), ),
existing_nullable=False, existing_nullable=False,
) )
op.create_foreign_key(
"invited_organisation_users_status_fkey",
"invited_organization_users",
"invite_status_type",
["status"],
["name"],
)
op.alter_column( op.alter_column(
"invited_organization_users", "invited_organization_users",
"status", "status",
@@ -874,16 +749,6 @@ def downgrade():
type_=sa.VARCHAR(), type_=sa.VARCHAR(),
existing_nullable=False, existing_nullable=False,
) )
op.drop_index(
op.f("ix_inbound_sms_history_created_at"), table_name="inbound_sms_history"
)
op.create_foreign_key(
"email_branding_brand_type_fkey",
"email_branding",
"branding_type",
["brand_type"],
["name"],
)
op.alter_column( op.alter_column(
"email_branding", "email_branding",
"brand_type", "brand_type",
@@ -898,9 +763,6 @@ def downgrade():
type_=sa.VARCHAR(length=255), type_=sa.VARCHAR(length=255),
existing_nullable=False, existing_nullable=False,
) )
op.create_foreign_key(
"api_keys_key_type_fkey", "api_keys", "key_types", ["key_type"], ["name"]
)
op.alter_column( op.alter_column(
"api_keys", "api_keys",
"key_type", "key_type",
@@ -908,24 +770,26 @@ def downgrade():
type_=sa.VARCHAR(length=255), type_=sa.VARCHAR(length=255),
existing_nullable=False, existing_nullable=False,
) )
op.drop_constraint(None, "agreements", type_="foreignkey")
op.drop_index(op.f("ix_agreements_url"), table_name="agreements") # Composite Index stuff
op.drop_index(op.f("ix_agreements_partner_name"), table_name="agreements") op.drop_index("ix_notifications_service_id_composite", table_name="notifications")
op.alter_column( op.create_index(
"agreements", "organization_id", existing_type=postgresql.UUID(), nullable=False "ix_notifications_service_id_composite",
"notifications",
["service_id", "notification_type", "notification_status", "created_at"],
unique=False,
) )
op.alter_column( op.drop_index(
"agreements", "ix_notifications_notification_type_composite", table_name="notifications"
"budget_amount",
existing_type=postgresql.DOUBLE_PRECISION(precision=53),
nullable=False,
) )
op.alter_column( op.create_index(
"agreements", "end_time", existing_type=postgresql.TIMESTAMP(), nullable=False "ix_notifications_notification_type_composite",
) "notifications",
op.alter_column( ["notification_type", "notification_status", "created_at"],
"agreements", "start_time", existing_type=postgresql.TIMESTAMP(), nullable=False unique=False,
) )
# Recreate helper tables
op.create_table( op.create_table(
"service_permission_types", "service_permission_types",
sa.Column("name", sa.VARCHAR(length=255), autoincrement=False, nullable=False), sa.Column("name", sa.VARCHAR(length=255), autoincrement=False, nullable=False),
@@ -982,5 +846,3 @@ def downgrade():
sa.Column("name", sa.VARCHAR(length=255), autoincrement=False, nullable=False), sa.Column("name", sa.VARCHAR(length=255), autoincrement=False, nullable=False),
sa.PrimaryKeyConstraint("name", name="template_process_type_pkey"), sa.PrimaryKeyConstraint("name", name="template_process_type_pkey"),
) )
op.drop_table("notifications_all_time_view")
# ### end Alembic commands ###