2020-10-27 20:46:55 +00:00
|
|
|
"""devices1
|
|
|
|
|
|
|
|
Revision ID: 34fe3d061c5f
|
|
|
|
Revises: 8c06ac60d125
|
|
|
|
Create Date: 2020-10-27 19:38:48.445908
|
|
|
|
|
|
|
|
"""
|
2020-11-15 07:28:12 +00:00
|
|
|
from datetime import date, timedelta
|
|
|
|
|
2020-10-27 20:46:55 +00:00
|
|
|
import sqlalchemy as sa
|
|
|
|
|
|
|
|
from alembic import op
|
|
|
|
|
|
|
|
# revision identifiers, used by Alembic.
|
2020-11-07 08:25:41 +00:00
|
|
|
from sqlalchemy import column, func, select, table, text
|
2020-10-27 20:46:55 +00:00
|
|
|
from sqlalchemy.dialects import postgresql
|
2020-11-08 14:31:27 +00:00
|
|
|
from sqlalchemy.dialects.postgresql import insert as pg_insert
|
2020-10-27 20:46:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
revision = "34fe3d061c5f"
|
|
|
|
down_revision = "8c06ac60d125"
|
|
|
|
branch_labels = None
|
|
|
|
depends_on = None
|
|
|
|
|
|
|
|
|
2020-11-09 07:13:18 +00:00
|
|
|
def update_sale_categories():
|
2020-10-27 20:46:55 +00:00
|
|
|
product = table(
|
2020-11-07 08:25:41 +00:00
|
|
|
"products",
|
2020-11-09 07:13:18 +00:00
|
|
|
column("product_id", postgresql.UUID(as_uuid=True)),
|
2020-11-07 08:25:41 +00:00
|
|
|
column("sale_category_id", postgresql.UUID(as_uuid=True)),
|
|
|
|
column("sale_category_name", sa.Unicode(length=255)),
|
|
|
|
)
|
|
|
|
sale_category = table(
|
|
|
|
"sale_categories", column("id", postgresql.UUID(as_uuid=True)), column("name", sa.Unicode(length=255))
|
2020-10-27 20:46:55 +00:00
|
|
|
)
|
|
|
|
op.execute(
|
|
|
|
product.update(
|
|
|
|
values={
|
|
|
|
"sale_category_id": select([sale_category.c.id]).where(
|
|
|
|
product.c.sale_category_name == sale_category.c.name
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
2020-11-09 07:13:18 +00:00
|
|
|
with op.batch_alter_table("products") as batch_op:
|
|
|
|
batch_op.alter_column("sale_category_id", nullable=False)
|
|
|
|
batch_op.drop_column("sale_category_name")
|
|
|
|
|
2020-10-27 20:46:55 +00:00
|
|
|
|
2020-11-09 07:13:18 +00:00
|
|
|
def update_section_printers():
|
2020-10-27 20:46:55 +00:00
|
|
|
section_printer = table(
|
|
|
|
"section_printers",
|
2020-11-07 08:25:41 +00:00
|
|
|
column("section_id", postgresql.UUID(as_uuid=True)),
|
2020-10-27 20:46:55 +00:00
|
|
|
column("section_name", sa.Unicode(length=255)),
|
2020-11-07 08:25:41 +00:00
|
|
|
column("printer_id", postgresql.UUID(as_uuid=True)),
|
2020-10-27 20:46:55 +00:00
|
|
|
column("printer_name", sa.Unicode(length=255)),
|
|
|
|
)
|
2020-11-07 08:25:41 +00:00
|
|
|
section = table("sections", column("id", postgresql.UUID(as_uuid=True)), column("name", sa.Unicode(length=255)))
|
2021-03-18 12:18:31 +00:00
|
|
|
printer = table(
|
|
|
|
"printers",
|
|
|
|
column("id", postgresql.UUID(as_uuid=True)),
|
|
|
|
column("name", sa.Unicode(length=255)),
|
|
|
|
column("cut_code", sa.Unicode(length=255)),
|
|
|
|
)
|
2020-10-27 20:46:55 +00:00
|
|
|
op.execute(
|
|
|
|
section_printer.update(
|
|
|
|
values={"section_id": select([section.c.id]).where(section_printer.c.section_name == section.c.name)}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
op.execute(
|
|
|
|
section_printer.update(
|
|
|
|
values={"printer_id": select([printer.c.id]).where(section_printer.c.printer_name == printer.c.name)}
|
|
|
|
)
|
|
|
|
)
|
2021-03-19 02:36:37 +00:00
|
|
|
op.execute(printer.update(values={"cut_code": "\x1d\x56\x41\x03"}))
|
2020-10-27 20:46:55 +00:00
|
|
|
with op.batch_alter_table("section_printers") as batch_op:
|
2020-11-07 08:25:41 +00:00
|
|
|
batch_op.alter_column("section_id", nullable=False)
|
2020-10-27 20:46:55 +00:00
|
|
|
batch_op.drop_column("section_name")
|
2020-11-07 08:25:41 +00:00
|
|
|
batch_op.alter_column("printer_id", nullable=False)
|
2020-10-27 20:46:55 +00:00
|
|
|
batch_op.drop_column("printer_name")
|
2020-11-07 08:25:41 +00:00
|
|
|
|
2020-11-09 07:13:18 +00:00
|
|
|
|
|
|
|
def add_all_roles_to_owner():
|
|
|
|
r = table(
|
|
|
|
"roles",
|
|
|
|
column("id", postgresql.UUID(as_uuid=True)),
|
|
|
|
column("name", sa.Unicode(length=255)),
|
|
|
|
)
|
|
|
|
p = table(
|
|
|
|
"permissions",
|
|
|
|
column("id", postgresql.UUID(as_uuid=True)),
|
|
|
|
column("name", sa.Unicode(length=255)),
|
|
|
|
)
|
|
|
|
rp = table(
|
|
|
|
"role_permissions",
|
|
|
|
column("id", postgresql.UUID(as_uuid=True)),
|
|
|
|
column("permission_id", postgresql.UUID(as_uuid=True)),
|
|
|
|
column("role_id", postgresql.UUID(as_uuid=True)),
|
|
|
|
)
|
|
|
|
op.execute(
|
|
|
|
pg_insert(rp)
|
|
|
|
.from_select(
|
|
|
|
[rp.c.role_id, rp.c.permission_id],
|
|
|
|
select([select([func.distinct(r.c.id)]).where(r.c.name.ilike("Owner")).as_scalar(), p.c.id]),
|
|
|
|
)
|
|
|
|
.on_conflict_do_nothing(),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-11-15 06:56:24 +00:00
|
|
|
def rename_permissions():
|
|
|
|
p = table(
|
|
|
|
"permissions",
|
|
|
|
column("id", postgresql.UUID(as_uuid=True)),
|
|
|
|
column("name", sa.Unicode(length=255)),
|
|
|
|
)
|
|
|
|
rp = table(
|
|
|
|
"role_permissions",
|
|
|
|
column("id", postgresql.UUID(as_uuid=True)),
|
|
|
|
column("permission_id", postgresql.UUID(as_uuid=True)),
|
|
|
|
column("role_id", postgresql.UUID(as_uuid=True)),
|
|
|
|
)
|
|
|
|
op.execute(p.update().where(p.c.name == "Accounts Audit").values(name="Audit"))
|
|
|
|
op.execute(p.update().where(p.c.name == "Beer Consumption").values(name="Beer Sale Report"))
|
|
|
|
op.execute(p.update().where(p.c.name == "Bill Details").values(name="Bill Settlement Report"))
|
|
|
|
op.execute(p.update().where(p.c.name == "Cashier Checkout").values(name="Cashier Report"))
|
|
|
|
op.execute(p.update().where(p.c.name == "Sales Detail").values(name="Product Sale Report"))
|
|
|
|
op.execute(p.update().where(p.c.name == "Sales Analysis").values(name="Sale Report"))
|
|
|
|
op.execute(p.update().where(p.c.name == "Tax Analysis").values(name="Tax Report"))
|
|
|
|
op.execute(p.update().where(p.c.name == "Machines").values(name="Devices"))
|
|
|
|
op.execute(p.update().where(p.c.name == "Tables").values(name="Sections"))
|
|
|
|
|
|
|
|
op.execute(
|
|
|
|
rp.delete().where(
|
|
|
|
rp.c.permission_id == select([p.c.id]).where(p.c.name.ilike("Void or Reprinted Bill Report")).as_scalar()
|
|
|
|
)
|
|
|
|
)
|
|
|
|
op.execute(rp.delete().where(rp.c.permission_id == select([p.c.id]).where(p.c.name.ilike("Roles")).as_scalar()))
|
|
|
|
op.execute(
|
|
|
|
rp.delete().where(rp.c.permission_id == select([p.c.id]).where(p.c.name.ilike("NC Product")).as_scalar())
|
|
|
|
)
|
|
|
|
op.execute(
|
|
|
|
rp.delete().where(rp.c.permission_id == select([p.c.id]).where(p.c.name.ilike("Change Rate")).as_scalar())
|
|
|
|
)
|
|
|
|
op.execute(p.delete().where(p.c.name == "Void or Reprinted Bill Report"))
|
|
|
|
op.execute(p.delete().where(p.c.name == "Roles"))
|
|
|
|
op.execute(p.delete().where(p.c.name == "NC Product"))
|
|
|
|
op.execute(p.delete().where(p.c.name == "Change Rate"))
|
|
|
|
|
|
|
|
|
|
|
|
def update_settlements():
|
|
|
|
s = table(
|
|
|
|
"settlements",
|
|
|
|
column("voucher_id", postgresql.UUID(as_uuid=True)),
|
|
|
|
column("settled", sa.Integer()),
|
|
|
|
)
|
|
|
|
v = table(
|
|
|
|
"vouchers",
|
|
|
|
sa.Column("id", postgresql.UUID(as_uuid=True)),
|
|
|
|
sa.Column("voucher_type", sa.Integer()),
|
|
|
|
)
|
|
|
|
op.execute(
|
|
|
|
s.update()
|
|
|
|
.values(settled=9)
|
|
|
|
.where(s.c.voucher_id == v.c.id)
|
|
|
|
.where(v.c.voucher_type == 5)
|
|
|
|
.where(s.c.settled == 1)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-11-09 07:13:18 +00:00
|
|
|
def upgrade():
|
|
|
|
# ### commands auto generated by Alembic - please adjust! ###
|
|
|
|
update_sale_categories()
|
|
|
|
update_section_printers()
|
2020-11-07 08:25:41 +00:00
|
|
|
|
|
|
|
# Migration to temporal products
|
2020-11-09 07:13:18 +00:00
|
|
|
op.drop_constraint("fk_inventories_product_id_products", "inventories", type_="foreignkey")
|
|
|
|
op.drop_constraint(
|
|
|
|
"fk_modifier_categories_products_product_id_products", "modifier_categories_products", type_="foreignkey"
|
|
|
|
)
|
|
|
|
op.drop_constraint("uq_products_name", "products", type_="unique")
|
|
|
|
op.drop_constraint("pk_products", "products", type_="primary")
|
|
|
|
op.rename_table("products", "product_versions")
|
|
|
|
with op.batch_alter_table("product_versions") as batch_op:
|
|
|
|
batch_op.add_column(sa.Column("product_id", postgresql.UUID(as_uuid=True), nullable=True)),
|
2020-11-07 08:25:41 +00:00
|
|
|
batch_op.add_column(sa.Column("valid_from", sa.Date(), nullable=True))
|
|
|
|
batch_op.add_column(sa.Column("valid_till", sa.Date(), nullable=True))
|
2020-11-09 07:13:18 +00:00
|
|
|
batch_op.create_primary_key("pk_product_versions", ["id"])
|
2020-11-07 08:25:41 +00:00
|
|
|
|
2020-11-15 07:28:12 +00:00
|
|
|
p = table(
|
|
|
|
"product_versions",
|
|
|
|
column("is_active", sa.Boolean()),
|
|
|
|
column("valid_till", sa.Date()),
|
|
|
|
)
|
|
|
|
op.execute(
|
|
|
|
p.update().values(valid_till=date.today() - timedelta(days=1)).where(p.c.is_active == False) # noqa E712
|
|
|
|
)
|
|
|
|
|
|
|
|
op.drop_column("product_versions", "is_active")
|
|
|
|
|
2020-11-09 07:13:18 +00:00
|
|
|
prod_v = table(
|
2020-11-07 08:25:41 +00:00
|
|
|
"product_versions",
|
2020-11-09 07:13:18 +00:00
|
|
|
column("id", postgresql.UUID(as_uuid=True)),
|
|
|
|
column("product_id", postgresql.UUID(as_uuid=True)),
|
2020-11-07 08:25:41 +00:00
|
|
|
)
|
|
|
|
|
2020-11-09 07:13:18 +00:00
|
|
|
op.execute(prod_v.update(values={"product_id": prod_v.c.id}))
|
|
|
|
op.create_table(
|
|
|
|
"products",
|
|
|
|
sa.Column("id", postgresql.UUID(as_uuid=True), server_default=sa.text("gen_random_uuid()"), nullable=False),
|
|
|
|
sa.PrimaryKeyConstraint("id", name=op.f("pk_products")),
|
2020-11-07 08:25:41 +00:00
|
|
|
)
|
2020-11-09 07:13:18 +00:00
|
|
|
op.alter_column("product_versions", "product_id", nullable=False)
|
2020-11-07 08:25:41 +00:00
|
|
|
|
|
|
|
prod = table(
|
2020-11-09 07:13:18 +00:00
|
|
|
"product_versions",
|
2020-11-07 08:25:41 +00:00
|
|
|
column("id", postgresql.UUID(as_uuid=True)),
|
2020-11-08 14:31:27 +00:00
|
|
|
column("name", sa.Unicode(length=255)),
|
|
|
|
column("units", sa.Unicode(length=255)),
|
2020-11-07 08:25:41 +00:00
|
|
|
column("valid_from", sa.Date()),
|
|
|
|
column("valid_till", sa.Date()),
|
|
|
|
)
|
|
|
|
|
2020-11-09 07:13:18 +00:00
|
|
|
pv = table(
|
|
|
|
"products",
|
|
|
|
column("id", postgresql.UUID(as_uuid=True)),
|
2020-11-07 08:25:41 +00:00
|
|
|
)
|
|
|
|
|
2020-11-09 07:13:18 +00:00
|
|
|
op.execute(pv.insert().from_select([pv.c.id], select([prod.c.id])))
|
|
|
|
|
|
|
|
op.create_foreign_key(
|
|
|
|
op.f("fk_product_versions_product_id_products"), "product_versions", "products", ["product_id"], ["id"]
|
2020-11-07 08:25:41 +00:00
|
|
|
)
|
2020-11-09 07:13:18 +00:00
|
|
|
|
|
|
|
op.create_foreign_key(op.f("fk_inventories_product_id_products"), "inventories", "products", ["product_id"], ["id"])
|
2020-11-07 08:25:41 +00:00
|
|
|
op.create_foreign_key(
|
2020-11-09 07:13:18 +00:00
|
|
|
op.f("fk_modifier_categories_products_product_id_products"),
|
2020-11-07 08:25:41 +00:00
|
|
|
"modifier_categories_products",
|
2020-11-09 07:13:18 +00:00
|
|
|
"products",
|
2020-11-07 08:25:41 +00:00
|
|
|
["product_id"],
|
|
|
|
["id"],
|
|
|
|
)
|
2020-11-09 07:13:18 +00:00
|
|
|
|
|
|
|
op.alter_column("modifier_categories_products", "modifier_categories_id", new_column_name="modifier_category_id")
|
|
|
|
op.drop_constraint("uq_modifier_categories_products_product_id", "modifier_categories_products", type_="unique")
|
|
|
|
op.create_unique_constraint(
|
|
|
|
op.f("uq_modifier_categories_products_product_id"),
|
|
|
|
"modifier_categories_products",
|
|
|
|
["product_id", "modifier_category_id"],
|
|
|
|
)
|
|
|
|
|
2020-11-07 08:25:41 +00:00
|
|
|
op.create_exclude_constraint(
|
2020-11-09 07:13:18 +00:00
|
|
|
"uq_product_versions_name",
|
|
|
|
"product_versions",
|
2020-11-07 08:25:41 +00:00
|
|
|
(prod.c.name, "="),
|
|
|
|
(prod.c.units, "="),
|
|
|
|
(func.daterange(prod.c.valid_from, prod.c.valid_till, text("'[]'")), "&&"),
|
|
|
|
)
|
2020-11-15 06:56:24 +00:00
|
|
|
rename_permissions()
|
2020-11-09 07:13:18 +00:00
|
|
|
add_all_roles_to_owner()
|
2020-11-15 06:56:24 +00:00
|
|
|
update_settlements()
|
2020-10-27 20:46:55 +00:00
|
|
|
# ### end Alembic commands ###
|
|
|
|
|
|
|
|
|
|
|
|
def downgrade():
|
|
|
|
# ### commands auto generated by Alembic - please adjust! ###
|
|
|
|
op.add_column("products", sa.Column("sale_category_name", sa.Unicode(length=255), nullable=False))
|
|
|
|
op.drop_constraint("fk_products_sale_category_id_sale_categories", "products", type_="foreignkey")
|
|
|
|
op.drop_column("products", "sale_category_id")
|
|
|
|
# ### end Alembic commands ###
|