Make order_item.price nullable

There are some order items in the DB where product = NULL so price
cannot be defined for them.
This commit is contained in:
Midgard 2020-01-26 21:01:04 +01:00
parent 7e476c39dc
commit 40170046c5
Signed by: midgard
GPG key ID: 511C112F1331BBB4
2 changed files with 17 additions and 17 deletions

View file

@ -55,9 +55,9 @@ DISH_LEGACY_TO_HLDS = {
def upgrade(): def upgrade():
# First the simple actions # First the simple actions
op.create_table("order_item_choice", 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("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("kind", sa.String(length=1), nullable=False),
sa.Column("name", sa.String(length=120), nullable=True), sa.Column("name", sa.String(length=120), nullable=True),
sa.Column("value", sa.String(length=120), nullable=True), sa.Column("value", sa.String(length=120), nullable=True),
@ -65,10 +65,10 @@ def upgrade():
sa.PrimaryKeyConstraint("id") sa.PrimaryKeyConstraint("id")
) )
op.add_column("order_item", sa.Column("hlds_data_version", sa.String(length=40), nullable=True)) 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", op.alter_column("order_item", "extra", new_column_name="comment",
existing_type=sa.String(254), type_=sa.Text()) existing_type=sa.String(254), type_=sa.Text)
op.alter_column("order_item", "name", new_column_name="user_name") op.alter_column("order_item", "name", new_column_name="user_name", type_=sa.String(120))
#---------------------------------------------------------------------------------------------- #----------------------------------------------------------------------------------------------
# Migrate historical product data to order items # Migrate historical product data to order items
@ -76,7 +76,7 @@ def upgrade():
# First create the new columns we will populate # 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_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("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 # Brief, ad-hoc table constructs just for our UPDATE statement, see
# https://alembic.sqlalchemy.org/en/latest/ops.html#alembic.operations.Operations.execute # https://alembic.sqlalchemy.org/en/latest/ops.html#alembic.operations.Operations.execute
order_item = table("order_item", order_item = table("order_item",
@ -94,13 +94,12 @@ def upgrade():
] ]
dish_name_and_price_from_product = text(""" dish_name_and_price_from_product = text("""
UPDATE order_item UPDATE order_item
SET dish_name = (SELECT name 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 price 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]): for query in chain(new_dish_id, [dish_name_and_price_from_product]):
op.execute(query) op.execute(query)
# Historical product data migrated, drop obsolete column and table # 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_column("order_item", "product_id")
op.drop_table("product") op.drop_table("product")
@ -108,7 +107,7 @@ def upgrade():
# Migrate historical location data to orders # Migrate historical location data to orders
op.add_column("order", sa.Column("location_name", sa.String(length=128), 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") 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 # Brief, ad-hoc table constructs just for our UPDATE statement, see
# https://alembic.sqlalchemy.org/en/latest/ops.html#alembic.operations.Operations.execute # https://alembic.sqlalchemy.org/en/latest/ops.html#alembic.operations.Operations.execute
order = table("order", order = table("order",
@ -125,24 +124,25 @@ def upgrade():
] ]
location_name_from_location = text(""" location_name_from_location = text("""
UPDATE order UPDATE order
SET location_name = (SELECT name FROM location 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]): for query in chain(new_location_id, [location_name_from_location]):
op.execute(query) op.execute(query)
# Historical location data migrated, drop obsolete column and table # Historical location data migrated, drop obsolete column and table
op.drop_column("order", "legacy_location_id") op.drop_column("order", "legacy_location_id")
op.drop_constraint(None, "order", type_="foreignkey")
op.drop_table("location") op.drop_table("location")
def downgrade(): 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", op.alter_column("order_item", "comment", new_column_name="extra",
existing_type=sa.Text(), type_=sa.String(254)) 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! ### # ### commands auto generated by Alembic - please adjust! ###
op.add_column("order_item", sa.Column("product_id", sa.INTEGER(), nullable=True)) op.add_column("order_item", sa.Column("product_id", sa.INTEGER(), nullable=True))

View file

@ -14,7 +14,7 @@ class OrderItem(db.Model):
user_name = db.Column(db.String(120)) user_name = db.Column(db.String(120))
dish_id = db.Column(db.String(64), nullable=True) dish_id = db.Column(db.String(64), nullable=True)
dish_name = db.Column(db.String(120), 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( paid = db.Column(
db.Boolean, default=False, nullable=True db.Boolean, default=False, nullable=True
) )