148 lines
5.1 KiB
Python
148 lines
5.1 KiB
Python
"""bundles
|
|
|
|
Revision ID: 367ecf7b898f
|
|
Revises: 1e0daf6bc1ae
|
|
Create Date: 2026-01-28 16:11:21.874409
|
|
|
|
"""
|
|
|
|
import sqlalchemy as sa
|
|
|
|
from sqlalchemy.dialects import postgresql
|
|
|
|
from alembic import op
|
|
|
|
|
|
# revision identifiers, used by Alembic.
|
|
revision = "367ecf7b898f"
|
|
down_revision = "1e0daf6bc1ae"
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
|
|
def upgrade():
|
|
op.create_table(
|
|
"bundle_items",
|
|
sa.Column("id", sa.Uuid(), server_default=sa.text("gen_random_uuid()"), nullable=False),
|
|
sa.Column("bundle_id", sa.Uuid(), nullable=False),
|
|
sa.Column("item_id", sa.Uuid(), nullable=False),
|
|
sa.Column("quantity", sa.Numeric(precision=15, scale=2), nullable=False),
|
|
sa.Column("sale_price", sa.Numeric(precision=15, scale=2), nullable=False),
|
|
sa.Column("print_in_bill", sa.Boolean(), server_default=sa.text("true"), nullable=False),
|
|
sa.Column("valid_from", sa.Date(), nullable=True),
|
|
sa.Column("valid_till", sa.Date(), nullable=True),
|
|
postgresql.ExcludeConstraint(
|
|
(sa.column("bundle_id"), "="),
|
|
(sa.column("item_id"), "="),
|
|
(sa.text("daterange(valid_from, valid_till, '[]')"), "&&"),
|
|
using="gist",
|
|
name=op.f("uq_bundle_items_bundle_id_item_id"),
|
|
),
|
|
sa.ForeignKeyConstraint(
|
|
["bundle_id"], ["stock_keeping_units.id"], name=op.f("fk_bundle_items_bundle_id_stock_keeping_units")
|
|
),
|
|
sa.ForeignKeyConstraint(
|
|
["item_id"], ["stock_keeping_units.id"], name=op.f("fk_bundle_items_item_id_stock_keeping_units")
|
|
),
|
|
sa.PrimaryKeyConstraint("id", name=op.f("pk_bundle_items")),
|
|
)
|
|
|
|
inventory_type = sa.Enum("regular", "bundle", "bundle_item", name="inventorytype")
|
|
inventory_type.create(op.get_bind())
|
|
op.add_column("inventories", sa.Column("type", inventory_type, server_default=sa.text("'regular'"), nullable=False))
|
|
op.add_column("inventories", sa.Column("parent_id", sa.Uuid(), nullable=True))
|
|
op.drop_constraint(op.f("uq_inventories_kot_id"), "inventories", type_="unique")
|
|
op.create_unique_constraint(
|
|
op.f("uq_inventories_kot_id"),
|
|
"inventories",
|
|
["kot_id", "sku_id", "is_happy_hour", "type", "parent_id"],
|
|
postgresql_nulls_not_distinct=True,
|
|
)
|
|
op.create_foreign_key(
|
|
op.f("fk_inventories_parent_id_inventories"), "inventories", "inventories", ["parent_id"], ["id"]
|
|
)
|
|
op.add_column(
|
|
"stock_keeping_units", sa.Column("is_bundle", sa.Boolean(), server_default=sa.text("false"), nullable=False)
|
|
)
|
|
mc = sa.table(
|
|
"menu_categories",
|
|
sa.column("id", sa.Uuid()),
|
|
sa.column("name", sa.Unicode(length=255)),
|
|
sa.column("is_active", sa.Boolean()),
|
|
sa.column("is_fixture", sa.Boolean()),
|
|
sa.column("sort_order", sa.Integer()),
|
|
)
|
|
|
|
op.execute(
|
|
mc.insert().values(
|
|
id="6752ed9d-6f1a-4941-940a-17759dcc6720",
|
|
name="Bundles",
|
|
is_active=True,
|
|
is_fixture=True,
|
|
sort_order=0,
|
|
)
|
|
)
|
|
|
|
customers = sa.table(
|
|
"customers",
|
|
sa.column("id", sa.Uuid),
|
|
sa.column("name", sa.Text),
|
|
sa.column("phone", sa.Text),
|
|
)
|
|
|
|
guest_book = sa.table(
|
|
"guest_book",
|
|
sa.column("customer_id", sa.Uuid),
|
|
)
|
|
|
|
voucher = sa.table(
|
|
"vouchers",
|
|
sa.column("customer_id", sa.Uuid),
|
|
)
|
|
|
|
bad_customer_ids = sa.select(customers.c.id).where(
|
|
sa.func.length(sa.func.trim(customers.c.phone)) == 0,
|
|
)
|
|
|
|
op.execute(guest_book.delete().where(guest_book.c.customer_id.in_(bad_customer_ids)))
|
|
|
|
op.execute(voucher.update().where(voucher.c.customer_id.in_(bad_customer_ids)).values(customer_id=None))
|
|
|
|
op.execute(customers.delete().where(customers.c.id.in_(bad_customer_ids)))
|
|
|
|
op.execute(
|
|
customers.update()
|
|
.where(
|
|
sa.func.length(sa.func.trim(customers.c.name)) == 0,
|
|
)
|
|
.values(name=customers.c.phone)
|
|
)
|
|
|
|
with op.batch_alter_table("customers") as batch_op:
|
|
batch_op.create_check_constraint("name_not_blank", sa.text("length(trim(both from name)) >= 1"))
|
|
batch_op.create_check_constraint("phone_not_blank", sa.text("length(trim(both from phone)) >= 1"))
|
|
|
|
|
|
def downgrade():
|
|
op.drop_table("bundle_items")
|
|
op.drop_column("stock_keeping_units", "is_bundle")
|
|
op.drop_constraint(op.f("fk_inventories_parent_id_inventories"), "inventories", type_="foreignkey")
|
|
op.drop_constraint(op.f("uq_inventories_kot_id"), "inventories", type_="unique")
|
|
op.create_unique_constraint(
|
|
op.f("uq_inventories_kot_id"),
|
|
"inventories",
|
|
["kot_id", "sku_id", "is_happy_hour", "price"],
|
|
postgresql_nulls_not_distinct=False,
|
|
)
|
|
op.drop_column("inventories", "parent_id")
|
|
op.drop_column("inventories", "type")
|
|
mc = sa.table(
|
|
"menu_categories",
|
|
sa.column("id", sa.Uuid()),
|
|
)
|
|
op.execute(mc.delete().where(mc.c.id == "6752ed9d-6f1a-4941-940a-17759dcc6720"))
|
|
|
|
with op.batch_alter_table("customers") as batch_op:
|
|
batch_op.drop_constraint("name_not_blank", type_="check")
|
|
batch_op.drop_constraint("phone_not_blank", type_="check")
|