Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release/2025.02.3 [main] with updated migration #1767

Merged
merged 1 commit into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Loading