Migration worked without errors now.

Signed-off-by: Cliff Hill <Clifford.hill@gsa.gov>
This commit is contained in:
Cliff Hill
2024-01-25 14:45:37 -05:00
parent ced386b0c5
commit 5d02f22408
2 changed files with 929 additions and 882 deletions

View File

@@ -1,7 +1,6 @@
import datetime import datetime
import itertools import itertools
import uuid import uuid
from enum import Enum
from flask import current_app, url_for from flask import current_app, url_for
from notifications_utils.clients.encryption.encryption_client import EncryptionError from notifications_utils.clients.encryption.encryption_client import EncryptionError
@@ -14,7 +13,7 @@ from notifications_utils.recipients import (
) )
from notifications_utils.template import PlainTextEmailTemplate, SMSMessageTemplate from notifications_utils.template import PlainTextEmailTemplate, SMSMessageTemplate
from sqlalchemy import CheckConstraint, Index, UniqueConstraint from sqlalchemy import CheckConstraint, Index, UniqueConstraint
from sqlalchemy.dialects.postgresql import JSON, JSONB, UUID, ENUM from sqlalchemy.dialects.postgresql import JSON, JSONB, UUID
from sqlalchemy.ext.associationproxy import association_proxy from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.ext.declarative import declared_attr from sqlalchemy.ext.declarative import declared_attr
from sqlalchemy.orm import validates from sqlalchemy.orm import validates

View File

@@ -5,8 +5,10 @@ Revises: 0409_fix_service_name
Create Date: 2024-01-18 12:34:32.857422 Create Date: 2024-01-18 12:34:32.857422
""" """
from contextlib import contextmanager
from enum import Enum from enum import Enum
from typing import TypedDict from re import I
from typing import Iterator, TypedDict
import sqlalchemy as sa import sqlalchemy as sa
from alembic import op from alembic import op
@@ -141,7 +143,73 @@ def enum_type(enum: Enum) -> sa.Enum:
) )
@contextmanager
def view_handler() -> Iterator[None]:
op.execute("DROP VIEW notifications_all_time_view")
yield
op.execute(
"""
CREATE VIEW notifications_all_time_view AS
(
SELECT
id,
job_id,
job_row_number,
service_id,
template_id,
template_version,
api_key_id,
key_type,
billable_units,
notification_type,
created_at,
sent_at,
sent_by,
updated_at,
notification_status,
reference,
client_reference,
international,
phone_prefix,
rate_multiplier,
created_by_id,
document_download_count
FROM notifications
) UNION
(
SELECT
id,
job_id,
job_row_number,
service_id,
template_id,
template_version,
api_key_id,
key_type,
billable_units,
notification_type,
created_at,
sent_at,
sent_by,
updated_at,
notification_status,
reference,
client_reference,
international,
phone_prefix,
rate_multiplier,
created_by_id,
document_download_count
FROM notification_history
)
"""
)
def upgrade(): def upgrade():
with view_handler():
# Remove foreign key constraints for old "helper" tables. # Remove foreign key constraints for old "helper" tables.
op.drop_constraint("api_keys_key_type_fkey", "api_keys", type_="foreignkey") op.drop_constraint("api_keys_key_type_fkey", "api_keys", type_="foreignkey")
op.drop_constraint( op.drop_constraint(
@@ -157,7 +225,9 @@ def upgrade():
) )
op.drop_constraint("jobs_job_status_fkey", "jobs", type_="foreignkey") op.drop_constraint("jobs_job_status_fkey", "jobs", type_="foreignkey")
op.drop_constraint( op.drop_constraint(
"notification_history_key_type_fkey", "notification_history", type_="foreignkey" "notification_history_key_type_fkey",
"notification_history",
type_="foreignkey",
) )
op.drop_constraint( op.drop_constraint(
"fk_notification_history_notification_status", "fk_notification_history_notification_status",
@@ -177,23 +247,23 @@ def upgrade():
"service_callback_api_type_fk", "service_callback_api", type_="foreignkey" "service_callback_api_type_fk", "service_callback_api", type_="foreignkey"
) )
op.drop_constraint( op.drop_constraint(
"service_permissions_permission_fkey", "service_permissions", type_="foreignkey" "service_permissions_permission_fkey",
"service_permissions",
type_="foreignkey",
) )
op.drop_constraint( op.drop_constraint(
"services_organisation_type_fkey", "services", type_="foreignkey" "services_organisation_type_fkey", "services", type_="foreignkey"
) )
op.drop_constraint("templates_process_type_fkey", "templates", type_="foreignkey")
op.drop_constraint( op.drop_constraint(
"templates_history_process_type_fkey", "templates_history", type_="foreignkey" "templates_process_type_fkey", "templates", type_="foreignkey"
)
op.drop_constraint(
"templates_history_process_type_fkey",
"templates_history",
type_="foreignkey",
) )
op.drop_constraint("users_auth_type_fkey", "users", type_="foreignkey") op.drop_constraint("users_auth_type_fkey", "users", type_="foreignkey")
# Drop composite indexes
op.drop_index("ix_notifications_service_id_composite", table_name="notifications")
op.drop_index(
"ix_notifications_notification_type_composite", table_name="notifications"
)
# drop old "helper" tables # drop old "helper" tables
op.drop_table("template_process_type") op.drop_table("template_process_type")
op.drop_table("key_types") op.drop_table("key_types")
@@ -480,6 +550,7 @@ def upgrade():
existing_type=sa.VARCHAR(), existing_type=sa.VARCHAR(),
type_=enum_type(AuthType), type_=enum_type(AuthType),
existing_nullable=False, existing_nullable=False,
postgresql_using="auth_type::auth_types",
) )
op.alter_column( op.alter_column(
"verify_codes", "verify_codes",
@@ -490,30 +561,9 @@ def upgrade():
postgresql_using=enum_using("code_type", CodeType), postgresql_using=enum_using("code_type", CodeType),
) )
# Recreate composite indexes
op.create_index(
"ix_notifications_service_id_composite",
"notifications",
["service_id", "notification_type", "status", "created_at"],
unique=False,
)
op.create_index(
"ix_notifications_notification_type_composite",
"notifications",
["notification_type", "status", "created_at"],
unique=False,
)
# ### end Alembic commands ###
def downgrade(): def downgrade():
# Dropping composite indexes with view_handler():
op.drop_index("ix_notifications_service_id_composite", table_name="notifications")
op.drop_index(
"ix_notifications_notification_type_composite", table_name="notifications"
)
# Alter columns back # Alter columns back
op.alter_column( op.alter_column(
"verify_codes", "verify_codes",
@@ -755,7 +805,9 @@ def downgrade():
# Recreate helper tables # Recreate helper tables
service_permission_types = op.create_table( service_permission_types = 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
),
sa.PrimaryKeyConstraint("name", name="service_permission_types_pkey"), sa.PrimaryKeyConstraint("name", name="service_permission_types_pkey"),
) )
op.bulk_insert( op.bulk_insert(
@@ -778,7 +830,9 @@ def downgrade():
) )
job_status = op.create_table( job_status = op.create_table(
"job_status", "job_status",
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="job_status_pkey"), sa.PrimaryKeyConstraint("name", name="job_status_pkey"),
) )
op.bulk_insert( op.bulk_insert(
@@ -821,7 +875,9 @@ def downgrade():
) )
branding_type = op.create_table( branding_type = op.create_table(
"branding_type", "branding_type",
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="branding_type_pkey"), sa.PrimaryKeyConstraint("name", name="branding_type_pkey"),
) )
op.bulk_insert( op.bulk_insert(
@@ -847,7 +903,9 @@ def downgrade():
) )
organization_types = op.create_table( organization_types = op.create_table(
"organization_types", "organization_types",
sa.Column("name", sa.VARCHAR(length=255), autoincrement=False, nullable=False), sa.Column(
"name", sa.VARCHAR(length=255), autoincrement=False, nullable=False
),
sa.Column( sa.Column(
"annual_free_sms_fragment_limit", "annual_free_sms_fragment_limit",
sa.BIGINT(), sa.BIGINT(),
@@ -891,7 +949,9 @@ def downgrade():
) )
key_types = op.create_table( key_types = op.create_table(
"key_types", "key_types",
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="key_types_pkey"), sa.PrimaryKeyConstraint("name", name="key_types_pkey"),
) )
op.bulk_insert( op.bulk_insert(
@@ -904,7 +964,9 @@ def downgrade():
) )
template_process_type = op.create_table( template_process_type = op.create_table(
"template_process_type", "template_process_type",
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.bulk_insert( op.bulk_insert(
@@ -915,20 +977,6 @@ def downgrade():
], ],
) )
# Creating composite indexes
op.create_index(
"ix_notifications_service_id_composite",
"notifications",
["service_id", "notification_type", "notification_status", "created_at"],
unique=False,
)
op.create_index(
"ix_notifications_notification_type_composite",
"notifications",
["notification_type", "notification_status", "created_at"],
unique=False,
)
# Recreate foreign keys # Recreate foreign keys
op.create_foreign_key( op.create_foreign_key(
"users_auth_type_fkey", "users", "auth_type", ["auth_type"], ["name"] "users_auth_type_fkey", "users", "auth_type", ["auth_type"], ["name"]