From b1d0c3e00466309c6d940410759619b3ffe214d8 Mon Sep 17 00:00:00 2001 From: Midgard Date: Sun, 26 Jan 2020 23:06:21 +0100 Subject: [PATCH] Finish working migration! --- .../versions/9159a6fed021_initial_haldis_support.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/app/migrations/versions/9159a6fed021_initial_haldis_support.py b/app/migrations/versions/9159a6fed021_initial_haldis_support.py index 49086b4..69d3915 100644 --- a/app/migrations/versions/9159a6fed021_initial_haldis_support.py +++ b/app/migrations/versions/9159a6fed021_initial_haldis_support.py @@ -100,14 +100,18 @@ def upgrade(): for query in chain(new_dish_id, [dish_name_and_price_from_product]): op.execute(query) # Historical product data migrated, drop obsolete column and table + op.execute(text("ALTER TABLE order_item DROP FOREIGN KEY order_item_ibfk_3")) op.drop_column("order_item", "product_id") op.drop_table("product") #---------------------------------------------------------------------------------------------- # Migrate historical location data to orders + op.execute(text("ALTER TABLE `order` DROP FOREIGN KEY order_ibfk_2")) + op.alter_column("order", "location_id", new_column_name="legacy_location_id", + type_=sa.Integer, nullable=True) + op.add_column("order", sa.Column("location_id", sa.String(length=64), nullable=True)) op.add_column("order", sa.Column("location_name", sa.String(length=128), nullable=True)) - op.alter_column("order", "location_id", new_column_name="legacy_location_id", type_=sa.Integer) # Brief, ad-hoc table constructs just for our UPDATE statement, see # https://alembic.sqlalchemy.org/en/latest/ops.html#alembic.operations.Operations.execute order = table("order", @@ -123,9 +127,9 @@ def upgrade(): for old_id, new_id in LOCATION_LEGACY_TO_HLDS.items() ] location_name_from_location = text(""" - UPDATE order + UPDATE `order` SET location_name = (SELECT location.name FROM location - WHERE location.id = order.legacy_location_id)""" + WHERE location.id = `order`.legacy_location_id)""" ) for query in chain(new_location_id, [location_name_from_location]): op.execute(query)