From 40170046c5cc46a635010a51f899dad4676a1ffc Mon Sep 17 00:00:00 2001 From: Midgard Date: Sun, 26 Jan 2020 21:01:04 +0100 Subject: [PATCH] Make order_item.price nullable There are some order items in the DB where product = NULL so price cannot be defined for them. --- .../9159a6fed021_initial_haldis_support.py | 32 +++++++++---------- app/models/orderitem.py | 2 +- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/app/migrations/versions/9159a6fed021_initial_haldis_support.py b/app/migrations/versions/9159a6fed021_initial_haldis_support.py index 25a5213..49086b4 100644 --- a/app/migrations/versions/9159a6fed021_initial_haldis_support.py +++ b/app/migrations/versions/9159a6fed021_initial_haldis_support.py @@ -55,9 +55,9 @@ DISH_LEGACY_TO_HLDS = { def upgrade(): # First the simple actions op.create_table("order_item_choice", - sa.Column("id", sa.Integer(), nullable=False), + sa.Column("id", sa.Integer, nullable=False), sa.Column("choice_id", sa.String(length=64), nullable=True), - sa.Column("order_item_id", sa.Integer(), nullable=False), + sa.Column("order_item_id", sa.Integer, nullable=False), sa.Column("kind", sa.String(length=1), nullable=False), sa.Column("name", sa.String(length=120), nullable=True), sa.Column("value", sa.String(length=120), nullable=True), @@ -65,10 +65,10 @@ def upgrade(): sa.PrimaryKeyConstraint("id") ) op.add_column("order_item", sa.Column("hlds_data_version", sa.String(length=40), nullable=True)) - op.alter_column("order", "courrier_id", new_column_name="courier_id") + op.alter_column("order", "courrier_id", new_column_name="courier_id", type_=sa.Integer) op.alter_column("order_item", "extra", new_column_name="comment", - existing_type=sa.String(254), type_=sa.Text()) - op.alter_column("order_item", "name", new_column_name="user_name") + existing_type=sa.String(254), type_=sa.Text) + op.alter_column("order_item", "name", new_column_name="user_name", type_=sa.String(120)) #---------------------------------------------------------------------------------------------- # Migrate historical product data to order items @@ -76,7 +76,7 @@ def upgrade(): # First create the new columns we will populate op.add_column("order_item", sa.Column("dish_id", sa.String(length=64), nullable=True)) op.add_column("order_item", sa.Column("dish_name", sa.String(length=120), nullable=True)) - op.add_column("order_item", sa.Column("price", sa.Integer(), nullable=False)) + op.add_column("order_item", sa.Column("price", sa.Integer(), nullable=True)) # Brief, ad-hoc table constructs just for our UPDATE statement, see # https://alembic.sqlalchemy.org/en/latest/ops.html#alembic.operations.Operations.execute order_item = table("order_item", @@ -94,13 +94,12 @@ def upgrade(): ] dish_name_and_price_from_product = text(""" UPDATE order_item - SET dish_name = (SELECT name FROM product WHERE product.id == order_item.product_id), - price = (SELECT price FROM product WHERE product.id == order_item.product_id)""" + SET dish_name = (SELECT product.name FROM product WHERE product.id = order_item.product_id), + price = (SELECT product.price FROM product WHERE product.id = order_item.product_id)""" ) 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.drop_constraint(None, "order_item", type_="foreignkey") op.drop_column("order_item", "product_id") op.drop_table("product") @@ -108,7 +107,7 @@ def upgrade(): # Migrate historical location data to orders 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") + 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", @@ -125,24 +124,25 @@ def upgrade(): ] location_name_from_location = text(""" UPDATE order - SET location_name = (SELECT name FROM location - WHERE location.id == order.legacy_location_id)""" + SET location_name = (SELECT location.name FROM location + WHERE location.id = order.legacy_location_id)""" ) for query in chain(new_location_id, [location_name_from_location]): op.execute(query) # Historical location data migrated, drop obsolete column and table op.drop_column("order", "legacy_location_id") - op.drop_constraint(None, "order", type_="foreignkey") op.drop_table("location") def downgrade(): - "Don't use this. It will cripple the data." + raise NotImplementedError("Downgrading to before HLDS is not supported") - op.alter_column("order", "courier_id", new_column_name="courrier_id") + # Don't use this. It will cripple the data. + + op.alter_column("order", "courier_id", new_column_name="courrier_id", type_=sa.Integer) op.alter_column("order_item", "comment", new_column_name="extra", existing_type=sa.Text(), type_=sa.String(254)) - op.alter_column("order_item", "user_name", new_column_name="name") + op.alter_column("order_item", "user_name", new_column_name="name", type_=sa.String(120)) # ### commands auto generated by Alembic - please adjust! ### op.add_column("order_item", sa.Column("product_id", sa.INTEGER(), nullable=True)) diff --git a/app/models/orderitem.py b/app/models/orderitem.py index 5f99040..f597163 100644 --- a/app/models/orderitem.py +++ b/app/models/orderitem.py @@ -14,7 +14,7 @@ class OrderItem(db.Model): user_name = db.Column(db.String(120)) dish_id = db.Column(db.String(64), nullable=True) dish_name = db.Column(db.String(120), nullable=True) - price = db.Column(db.Integer, nullable=False) + price = db.Column(db.Integer, nullable=True) paid = db.Column( db.Boolean, default=False, nullable=True )