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():
# 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))

View file

@ -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
)