diff --git a/migrations/versions/rev_20261201_22_87412e235878_rename_constraints_to_follow_the_naming_.py b/migrations/versions/rev_20261201_22_87412e235878_rename_constraints_to_follow_the_naming_.py new file mode 100644 index 0000000..9d7809d --- /dev/null +++ b/migrations/versions/rev_20261201_22_87412e235878_rename_constraints_to_follow_the_naming_.py @@ -0,0 +1,161 @@ +"""Rename constraints to follow the naming convention. + +The live database diverged from the ORM models in `velofy.db`: + +- Earlier migrations created some constraints with hand-written names + without `op.f()`; so, the naming convention was not applied to them. +- Revision 8bfb928a31f8 renamed the column "training_horizon" into + "train_horizon" but not the unique constraint that contains it. +- The "ordered_timestamps_N" checks in "orders" are numbered by their + position in a list in the ORM. A check that was later inserted into + that list shifted the numbers, while the corresponding migration + added the check under a separate name. +""" + +import os + +from alembic import op + +from velofy import configuration + + +revision = "87412e235878" +down_revision = "6cb552f31132" +branch_labels = None +depends_on = None + + +config = configuration.make_config("testing" if os.getenv("TESTING") else "production") + + +# The order matters: the "ordered_timestamps_N" constraints are shifted +# by one from the top down such that every new name is free. +RENAMES = ( + ( + "forecasts", + "uq_forecasts_on_pixel_id_start_at_time_step_training_ho_ec47", + "uq_forecasts_on_pixel_id_start_at_time_step_train_horizon_model", + ), + ( + "forecasts", + "ci95_must_be_wider_than_ci80", + "ck_forecasts_on_ci95_must_be_wider_than_ci80", + ), + ( + "forecasts", + "ci_upper_bound_greater_than_lower_bound", + "ck_forecasts_on_ci_upper_bound_greater_than_lower_bound", + ), + ( + "forecasts", + "prediction_must_be_within_ci", + "ck_forecasts_on_prediction_must_be_within_ci", + ), + ( + "orders", + "courier_waited_at_delivery_only_if_delivery", + "ck_orders_on_courier_waited_at_delivery_only_if_delivery", + ), + ( + "orders", + "delivery_not_confirmed_only_if_delivery", + "ck_orders_on_delivery_not_confirmed_only_if_delivery", + ), + ( + "orders", + "pickup_not_confirmed_only_if_pickup", + "ck_orders_on_pickup_not_confirmed_only_if_pickup", + ), + ( + "orders", + "check_orders_on_scheduled_orders_must_be_at_quarters_of_an_hour", + "ck_orders_on_scheduled_orders_must_be_at_quarters_of_an_hour", + ), + *( + ( + "orders", + f"ck_orders_on_ordered_timestamps_{n}", + f"ck_orders_on_ordered_timestamps_{n + 1}", + ) + for n in range(20, 8, -1) + ), + ( + "orders", + "check_orders_on_ordered_timestamps_placed_at_before_pickup_at", + "ck_orders_on_ordered_timestamps_9", + ), + ("replay_simulations", "run_is_a_count", "ck_replay_simulations_on_run_is_a_count"), + ( + "replayed_orders", + "ad_hoc_orders_within_business_hours", + "ck_replayed_orders_on_ad_hoc_orders_within_business_hours", + ), + ( + "replayed_orders", + "cancellations_may_only_occur_before_pickup", + "ck_replayed_orders_on_cancellations_may_only_occur_befo_e482", + ), + ( + "replayed_orders", + "either_ad_hoc_or_scheduled_order", + "ck_replayed_orders_on_either_ad_hoc_or_scheduled_order", + ), + ( + "replayed_orders", + "estimated_prep_duration_between_0_and_3600", + "ck_replayed_orders_on_estimated_prep_duration_between_0_d15f", + ), + ( + "replayed_orders", + "estimated_prep_duration_must_be_whole_minutes", + "ck_replayed_orders_on_estimated_prep_duration_must_be_w_7021", + ), + ( + "replayed_orders", + "orders_dispatched_in_business_hours", + "ck_replayed_orders_on_orders_dispatched_in_business_hours", + ), + ( + "replayed_orders", + "orders_must_be_either_cancelled_or_fully_simulated", + "ck_replayed_orders_on_orders_must_be_either_cancelled_o_360b", + ), + ( + "replayed_orders", + "scheduled_orders_must_be_at_quarters_of_an_hour", + "ck_replayed_orders_on_scheduled_orders_must_be_at_quart_c770", + ), + ( + "replayed_orders", + "scheduled_orders_not_within_30_minutes", + "ck_replayed_orders_on_scheduled_orders_not_within_30_minutes", + ), + ( + "replayed_orders", + "scheduled_orders_within_business_hours", + "ck_replayed_orders_on_scheduled_orders_within_business_hours", + ), + ( + "replayed_orders", + "utilization_between_0_and_100", + "ck_replayed_orders_on_utilization_between_0_and_100", + ), +) + + +def upgrade(): + """Upgrade to the next revision.""" + for table, old_name, new_name in RENAMES: + op.execute( + f"ALTER TABLE {config.CLEAN_SCHEMA}.{table}" + f" RENAME CONSTRAINT {old_name} TO {new_name};", + ) + + +def downgrade(): + """Downgrade to the previous revision.""" + for table, old_name, new_name in reversed(RENAMES): + op.execute( + f"ALTER TABLE {config.CLEAN_SCHEMA}.{table}" + f" RENAME CONSTRAINT {new_name} TO {old_name};", + )