diff --git a/src/lamp_py/migrations/versions/performance_manager_dev/008_32ba735d080c_add_revenue_columns.py b/src/lamp_py/migrations/versions/performance_manager_dev/008_32ba735d080c_add_revenue_columns.py index 7f029ed4..d6d7ebb1 100644 --- a/src/lamp_py/migrations/versions/performance_manager_dev/008_32ba735d080c_add_revenue_columns.py +++ b/src/lamp_py/migrations/versions/performance_manager_dev/008_32ba735d080c_add_revenue_columns.py @@ -4,6 +4,14 @@ Revises: 896dedd8a4db Create Date: 2024-09-20 08:47:52.784591 +This change adds a boolean revenue column to the vehcile_trips table. +Initially this will be filled with True and back-filled by a seperate operation + +Details +* upgrade -> drop triggers and indexes from table and add revenue column + +* downgrade -> drop revenue column + """ from alembic import op @@ -22,7 +30,6 @@ def upgrade() -> None: - # ### commands auto generated by Alembic - please adjust! ### op.execute( f"ALTER TABLE public.vehicle_trips DISABLE TRIGGER rt_trips_update_branch_trunk;" ) @@ -61,11 +68,7 @@ def upgrade() -> None: f"ALTER TABLE public.vehicle_trips ENABLE TRIGGER update_vehicle_trips_modified;" ) - # ### end Alembic commands ### - def downgrade() -> None: - # ### commands auto generated by Alembic - please adjust! ### op.drop_column("vehicle_trips", "revenue") op.drop_column("temp_event_compare", "revenue") - # ### end Alembic commands ### diff --git a/src/lamp_py/migrations/versions/performance_manager_prod/005_32ba735d080c_add_revenue_columns.py b/src/lamp_py/migrations/versions/performance_manager_prod/005_32ba735d080c_add_revenue_columns.py new file mode 100644 index 00000000..d6d7ebb1 --- /dev/null +++ b/src/lamp_py/migrations/versions/performance_manager_prod/005_32ba735d080c_add_revenue_columns.py @@ -0,0 +1,74 @@ +"""add revenue columns + +Revision ID: 32ba735d080c +Revises: 896dedd8a4db +Create Date: 2024-09-20 08:47:52.784591 + +This change adds a boolean revenue column to the vehcile_trips table. +Initially this will be filled with True and back-filled by a seperate operation + +Details +* upgrade -> drop triggers and indexes from table and add revenue column + +* downgrade -> drop revenue column + +""" + +from alembic import op +import sqlalchemy as sa + +from lamp_py.postgres.rail_performance_manager_schema import ( + TempEventCompare, + VehicleTrips, +) + +# revision identifiers, used by Alembic. +revision = "32ba735d080c" +down_revision = "896dedd8a4db" +branch_labels = None +depends_on = None + + +def upgrade() -> None: + op.execute( + f"ALTER TABLE public.vehicle_trips DISABLE TRIGGER rt_trips_update_branch_trunk;" + ) + op.execute( + f"ALTER TABLE public.vehicle_trips DISABLE TRIGGER update_vehicle_trips_modified;" + ) + op.drop_index("ix_vehicle_trips_composite_1", table_name="vehicle_trips") + op.drop_constraint("vehicle_trips_unique_trip", table_name="vehicle_trips") + + op.add_column( + "temp_event_compare", sa.Column("revenue", sa.Boolean(), nullable=True) + ) + op.add_column( + "vehicle_trips", sa.Column("revenue", sa.Boolean(), nullable=True) + ) + op.execute(sa.update(TempEventCompare).values(revenue=True)) + op.execute(sa.update(VehicleTrips).values(revenue=True)) + op.alter_column("temp_event_compare", "revenue", nullable=False) + op.alter_column("vehicle_trips", "revenue", nullable=False) + + op.create_unique_constraint( + "vehicle_trips_unique_trip", + "vehicle_trips", + ["service_date", "route_id", "trip_id"], + ) + op.create_index( + "ix_vehicle_trips_composite_1", + "vehicle_trips", + ["route_id", "direction_id", "vehicle_id"], + unique=False, + ) + op.execute( + f"ALTER TABLE public.vehicle_trips ENABLE TRIGGER rt_trips_update_branch_trunk;" + ) + op.execute( + f"ALTER TABLE public.vehicle_trips ENABLE TRIGGER update_vehicle_trips_modified;" + ) + + +def downgrade() -> None: + op.drop_column("vehicle_trips", "revenue") + op.drop_column("temp_event_compare", "revenue") diff --git a/src/lamp_py/migrations/versions/performance_manager_staging/009_32ba735d080c_add_revenue_columns.py b/src/lamp_py/migrations/versions/performance_manager_staging/009_32ba735d080c_add_revenue_columns.py new file mode 100644 index 00000000..d6d7ebb1 --- /dev/null +++ b/src/lamp_py/migrations/versions/performance_manager_staging/009_32ba735d080c_add_revenue_columns.py @@ -0,0 +1,74 @@ +"""add revenue columns + +Revision ID: 32ba735d080c +Revises: 896dedd8a4db +Create Date: 2024-09-20 08:47:52.784591 + +This change adds a boolean revenue column to the vehcile_trips table. +Initially this will be filled with True and back-filled by a seperate operation + +Details +* upgrade -> drop triggers and indexes from table and add revenue column + +* downgrade -> drop revenue column + +""" + +from alembic import op +import sqlalchemy as sa + +from lamp_py.postgres.rail_performance_manager_schema import ( + TempEventCompare, + VehicleTrips, +) + +# revision identifiers, used by Alembic. +revision = "32ba735d080c" +down_revision = "896dedd8a4db" +branch_labels = None +depends_on = None + + +def upgrade() -> None: + op.execute( + f"ALTER TABLE public.vehicle_trips DISABLE TRIGGER rt_trips_update_branch_trunk;" + ) + op.execute( + f"ALTER TABLE public.vehicle_trips DISABLE TRIGGER update_vehicle_trips_modified;" + ) + op.drop_index("ix_vehicle_trips_composite_1", table_name="vehicle_trips") + op.drop_constraint("vehicle_trips_unique_trip", table_name="vehicle_trips") + + op.add_column( + "temp_event_compare", sa.Column("revenue", sa.Boolean(), nullable=True) + ) + op.add_column( + "vehicle_trips", sa.Column("revenue", sa.Boolean(), nullable=True) + ) + op.execute(sa.update(TempEventCompare).values(revenue=True)) + op.execute(sa.update(VehicleTrips).values(revenue=True)) + op.alter_column("temp_event_compare", "revenue", nullable=False) + op.alter_column("vehicle_trips", "revenue", nullable=False) + + op.create_unique_constraint( + "vehicle_trips_unique_trip", + "vehicle_trips", + ["service_date", "route_id", "trip_id"], + ) + op.create_index( + "ix_vehicle_trips_composite_1", + "vehicle_trips", + ["route_id", "direction_id", "vehicle_id"], + unique=False, + ) + op.execute( + f"ALTER TABLE public.vehicle_trips ENABLE TRIGGER rt_trips_update_branch_trunk;" + ) + op.execute( + f"ALTER TABLE public.vehicle_trips ENABLE TRIGGER update_vehicle_trips_modified;" + ) + + +def downgrade() -> None: + op.drop_column("vehicle_trips", "revenue") + op.drop_column("temp_event_compare", "revenue")