From 2039fe2593ec00d062f80f3bca7e17e9bd71caec Mon Sep 17 00:00:00 2001 From: Kenroy Gobourne <14842108+sultanofcardio@users.noreply.github.com> Date: Thu, 27 Feb 2025 19:08:38 -0500 Subject: [PATCH] chore: restore the periodicity table and the events.periodicity_id column (#1766) Co-authored-by: Marty --- ..._02_18_39-clean_up_schedule_tables_pt_1.py | 49 ------------- ...5_02_27_16_47-restore_periodicity_table.py | 68 +++++++++++++++++++ 2 files changed, 68 insertions(+), 49 deletions(-) create mode 100644 src/infrastructure/database/migrations/versions/2025_02_27_16_47-restore_periodicity_table.py diff --git a/src/infrastructure/database/migrations/versions/2025_02_02_18_39-clean_up_schedule_tables_pt_1.py b/src/infrastructure/database/migrations/versions/2025_02_02_18_39-clean_up_schedule_tables_pt_1.py index 06178d78be7..850a170f821 100644 --- a/src/infrastructure/database/migrations/versions/2025_02_02_18_39-clean_up_schedule_tables_pt_1.py +++ b/src/infrastructure/database/migrations/versions/2025_02_02_18_39-clean_up_schedule_tables_pt_1.py @@ -44,57 +44,8 @@ def upgrade() -> None: # Make sure that the `event_type` column is not null op.alter_column("events", "event_type", nullable=False) - # Drop the `periodicity_id` column from the `events` table - op.drop_column("events", "periodicity_id") - - # Drop the periodicity table - op.drop_table("periodicity") - def downgrade() -> None: - # Recreate the dropped tables - op.create_table( - "periodicity", - sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False), - sa.Column("is_deleted", sa.Boolean(), nullable=True), - sa.Column("created_at", sa.DateTime(), server_default=sa.text("timezone('utc', now())"), nullable=True), - sa.Column("updated_at", sa.DateTime(), server_default=sa.text("timezone('utc', now())"), nullable=True), - sa.Column("migrated_date", sa.DateTime(), nullable=True), - sa.Column("migrated_updated", sa.DateTime(), nullable=True), - sa.Column("type", sa.String(10), nullable=False), - sa.Column("start_date", sa.Date(), nullable=True), - sa.Column("end_date", sa.Date(), nullable=True), - sa.Column("selected_date", sa.Date(), nullable=True), - sa.PrimaryKeyConstraint("id", name=op.f("pk_periodicity")), - ) - - # Add the `periodicity_id` column back to the `events` table - op.add_column( - "events", - sa.Column( - "periodicity_id", - postgresql.UUID(as_uuid=True), - server_default=sa.text("gen_random_uuid()"), - nullable=False - ) - ) - - # Generate periodicity IDs for existing events - op.execute(""" - UPDATE events - SET periodicity_id = gen_random_uuid() - WHERE periodicity_id IS NULL - """) - - # Repopulate the `periodicity` table - # We do lose some data here (e.g. the original `id`, `created_at`, `updated_at`, `migrated_date`, `migrated_updated`), - # because we can't recover that data from the `events` table - op.execute(""" - INSERT INTO periodicity (id, is_deleted, type, start_date, end_date, selected_date) - SELECT e.periodicity_id, e.is_deleted, e.periodicity, e.start_date, e.end_date, e.selected_date - FROM events e - """) - # Drop the new columns from the `events` table op.drop_column("events", "activity_id") op.drop_column("events", "activity_flow_id") diff --git a/src/infrastructure/database/migrations/versions/2025_02_27_16_47-restore_periodicity_table.py b/src/infrastructure/database/migrations/versions/2025_02_27_16_47-restore_periodicity_table.py new file mode 100644 index 00000000000..1eeb5490058 --- /dev/null +++ b/src/infrastructure/database/migrations/versions/2025_02_27_16_47-restore_periodicity_table.py @@ -0,0 +1,68 @@ +"""Restore periodicity table + +Revision ID: 8fa31d7d9831 +Revises: 70987d489b17 +Create Date: 2025-02-27 16:47:39.684727 + +""" + +import sqlalchemy as sa +from alembic import op +from sqlalchemy.dialects import postgresql + +# revision identifiers, used by Alembic. +revision = "8fa31d7d9831" +down_revision = "70987d489b17" +branch_labels = None +depends_on = None + + +def upgrade() -> None: + # Recreate the periodicity table + op.create_table( + "periodicity", + sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False), + sa.Column("is_deleted", sa.Boolean(), nullable=True), + sa.Column("created_at", sa.DateTime(), server_default=sa.text("timezone('utc', now())"), nullable=True), + sa.Column("updated_at", sa.DateTime(), server_default=sa.text("timezone('utc', now())"), nullable=True), + sa.Column("migrated_date", sa.DateTime(), nullable=True), + sa.Column("migrated_updated", sa.DateTime(), nullable=True), + sa.Column("type", sa.String(10), nullable=False), + sa.Column("start_date", sa.Date(), nullable=True), + sa.Column("end_date", sa.Date(), nullable=True), + sa.Column("selected_date", sa.Date(), nullable=True), + sa.PrimaryKeyConstraint("id", name=op.f("pk_periodicity")), + if_not_exists=True, + ) + + # Conditionally add the `periodicity_id` column back to the `events` table + # There's no way to do this in alembic because there's no native IF NOT EXISTS syntax for altering tables + # So we use raw SQL + op.execute(""" + DO + $$ + BEGIN + IF NOT EXISTS (SELECT 1 + FROM information_schema.columns + WHERE table_name = 'events' + AND column_name = 'periodicity_id') + THEN + ALTER TABLE events ADD COLUMN periodicity_id uuid DEFAULT gen_random_uuid() NOT NULL; + END IF; + END + $$; + """) + + # Conditionally repopulate the `periodicity` table + # We do lose some data here (e.g. the original `id`, `created_at`, `updated_at`, `migrated_date`, `migrated_updated`), + # because we can't recover that data from the `events` table + op.execute(""" + INSERT INTO periodicity (id, is_deleted, type, start_date, end_date, selected_date) + SELECT e.periodicity_id, e.is_deleted, e.periodicity, e.start_date, e.end_date, e.selected_date + FROM events e + WHERE NOT EXISTS (SELECT 1 FROM periodicity) + """) + + +def downgrade() -> None: + pass