2020-01-26 16:26:17 +01:00
|
|
|
"""Initial HLDS support
|
|
|
|
|
|
|
|
Revision ID: 9159a6fed021
|
|
|
|
Revises: 150252c1cdb1
|
|
|
|
Create Date: 2020-01-26 16:22:00.935963
|
|
|
|
|
|
|
|
"""
|
2020-01-26 18:53:14 +01:00
|
|
|
# pylint: disable=invalid-name
|
2020-01-26 16:26:17 +01:00
|
|
|
|
|
|
|
# revision identifiers, used by Alembic.
|
2020-01-26 18:53:14 +01:00
|
|
|
revision = "9159a6fed021"
|
|
|
|
down_revision = "150252c1cdb1"
|
2020-01-26 16:26:17 +01:00
|
|
|
|
2020-01-26 18:53:14 +01:00
|
|
|
from itertools import chain
|
|
|
|
|
2022-04-19 22:03:00 +02:00
|
|
|
import sqlalchemy as sa
|
|
|
|
from alembic import op
|
2020-01-26 18:53:14 +01:00
|
|
|
from hlds.definitions import location_definitions
|
2022-04-19 22:03:00 +02:00
|
|
|
from sqlalchemy.sql import column, table, text
|
2020-01-26 19:39:21 +01:00
|
|
|
|
|
|
|
LOCATION_LEGACY_TO_HLDS = {
|
|
|
|
2: "blauw_kotje",
|
|
|
|
3: "ocean_garden",
|
|
|
|
4: "delhaize",
|
|
|
|
5: "simpizza",
|
|
|
|
6: "shell_tankstation",
|
|
|
|
7: "pitta_pinte",
|
|
|
|
8: "lan_pizza",
|
|
|
|
9: "oriental",
|
|
|
|
10: "gouden_sate",
|
|
|
|
11: "quick",
|
|
|
|
12: "dominos",
|
|
|
|
13: "stella_di_mare",
|
|
|
|
14: "olifantje",
|
|
|
|
15: "pauls_boutique",
|
|
|
|
16: "fritoloog",
|
|
|
|
17: "andromeda",
|
|
|
|
19: "pita_cece",
|
|
|
|
20: "pita_uniq",
|
|
|
|
21: "basic_italian",
|
|
|
|
22: "pizza_hut",
|
|
|
|
23: "test",
|
|
|
|
25: "primadonna_takeaway",
|
|
|
|
26: "primadonna_delivery",
|
|
|
|
28: "s5",
|
|
|
|
29: "fitchen",
|
|
|
|
30: "kebab_anadolu",
|
|
|
|
}
|
|
|
|
|
2020-01-26 16:26:17 +01:00
|
|
|
|
|
|
|
def upgrade():
|
2020-01-26 18:53:14 +01:00
|
|
|
# First the simple actions
|
2022-04-19 22:03:00 +02:00
|
|
|
op.create_table(
|
|
|
|
"order_item_choice",
|
2020-01-26 21:01:04 +01:00
|
|
|
sa.Column("id", sa.Integer, nullable=False),
|
2020-01-26 18:53:14 +01:00
|
|
|
sa.Column("choice_id", sa.String(length=64), nullable=True),
|
2020-01-26 21:01:04 +01:00
|
|
|
sa.Column("order_item_id", sa.Integer, nullable=False),
|
2020-01-26 18:53:14 +01:00
|
|
|
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),
|
2022-04-19 22:03:00 +02:00
|
|
|
sa.ForeignKeyConstraint(
|
|
|
|
["order_item_id"],
|
|
|
|
["order_item.id"],
|
|
|
|
),
|
|
|
|
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", 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", type_=sa.String(120)
|
2020-01-26 16:26:17 +01:00
|
|
|
)
|
2020-01-26 18:53:14 +01:00
|
|
|
|
2022-04-19 22:03:00 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------
|
2020-01-26 18:53:14 +01:00
|
|
|
# Migrate historical product data to order items
|
|
|
|
|
|
|
|
# First create the new columns we will populate
|
2022-04-19 22:03:00 +02:00
|
|
|
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)
|
|
|
|
)
|
2020-01-26 21:01:04 +01:00
|
|
|
op.add_column("order_item", sa.Column("price", sa.Integer(), nullable=True))
|
2020-01-26 18:53:14 +01:00
|
|
|
# Brief, ad-hoc table constructs just for our UPDATE statement, see
|
|
|
|
# https://alembic.sqlalchemy.org/en/latest/ops.html#alembic.operations.Operations.execute
|
2022-04-19 22:03:00 +02:00
|
|
|
order_item = table(
|
|
|
|
"order_item",
|
2020-01-26 18:53:14 +01:00
|
|
|
column("product_id", sa.Integer),
|
|
|
|
column("dish_id", sa.String),
|
|
|
|
column("dish_name", sa.String),
|
2022-04-19 22:03:00 +02:00
|
|
|
column("price", sa.Integer),
|
2020-01-26 18:53:14 +01:00
|
|
|
)
|
|
|
|
# Construct and execute queries
|
2022-04-19 22:03:00 +02:00
|
|
|
op.execute(
|
|
|
|
text(
|
|
|
|
"""
|
2020-01-26 18:53:14 +01:00
|
|
|
UPDATE order_item
|
2020-01-26 21:01:04 +01:00
|
|
|
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)"""
|
2022-04-19 22:03:00 +02:00
|
|
|
)
|
|
|
|
)
|
2020-01-26 18:53:14 +01:00
|
|
|
# Historical product data migrated, drop obsolete column and table
|
2023-04-19 23:48:59 +02:00
|
|
|
op.drop_constraint("order_item_ibfk_3", "order_item", type_="foreignkey")
|
2020-01-26 18:53:14 +01:00
|
|
|
op.drop_column("order_item", "product_id")
|
|
|
|
op.drop_table("product")
|
|
|
|
|
2022-04-19 22:03:00 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------
|
2020-01-26 18:53:14 +01:00
|
|
|
# Migrate historical location data to orders
|
2022-04-19 22:03:00 +02:00
|
|
|
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)
|
|
|
|
)
|
2020-01-26 18:53:14 +01:00
|
|
|
# Brief, ad-hoc table constructs just for our UPDATE statement, see
|
|
|
|
# https://alembic.sqlalchemy.org/en/latest/ops.html#alembic.operations.Operations.execute
|
2022-04-19 22:03:00 +02:00
|
|
|
order = table(
|
|
|
|
"order",
|
2020-01-26 18:53:14 +01:00
|
|
|
column("legacy_location_id", sa.Integer),
|
|
|
|
column("location_id", sa.String),
|
2022-04-19 22:03:00 +02:00
|
|
|
column("location_name", sa.String),
|
2020-01-26 18:53:14 +01:00
|
|
|
)
|
|
|
|
# Construct and execute queries
|
|
|
|
new_location_id = [
|
|
|
|
order.update()
|
2022-04-19 22:03:00 +02:00
|
|
|
.where(order.c.legacy_location_id == old_id)
|
|
|
|
.values(location_id=new_id)
|
2020-01-26 18:53:14 +01:00
|
|
|
for old_id, new_id in LOCATION_LEGACY_TO_HLDS.items()
|
|
|
|
]
|
2022-04-19 22:03:00 +02:00
|
|
|
location_name_from_location = text(
|
|
|
|
"""
|
2020-01-26 23:06:21 +01:00
|
|
|
UPDATE `order`
|
2020-01-26 21:01:04 +01:00
|
|
|
SET location_name = (SELECT location.name FROM location
|
2020-01-26 23:06:21 +01:00
|
|
|
WHERE location.id = `order`.legacy_location_id)"""
|
2020-01-26 18:53:14 +01:00
|
|
|
)
|
|
|
|
for query in chain(new_location_id, [location_name_from_location]):
|
|
|
|
op.execute(query)
|
|
|
|
# Historical location data migrated, drop obsolete column and table
|
2023-04-19 23:48:59 +02:00
|
|
|
op.drop_constraint("order_ibfk_1", "order", type_="foreignkey")
|
2020-01-26 18:53:14 +01:00
|
|
|
op.drop_column("order", "legacy_location_id")
|
|
|
|
op.drop_table("location")
|
2020-01-26 16:26:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
def downgrade():
|
2020-01-26 21:01:04 +01:00
|
|
|
raise NotImplementedError("Downgrading to before HLDS is not supported")
|