diff --git a/DB/import.sh b/DB/import.sh index a2b976a..f34b738 100755 --- a/DB/import.sh +++ b/DB/import.sh @@ -1,5 +1,10 @@ -docker run -it -v /home/tanshu/cl:/mnt --link postgres:db --rm --env PGPASSWORD="123456" postgres:alpine bash -c 'psql -h db -U postgres -c "drop database petty"' -docker run -it -v /home/tanshu/cl:/mnt --link postgres:db --rm --env PGPASSWORD="123456" postgres:alpine bash -c 'psql -h db -U postgres -c "create database petty"' +#scp knox:csv.tar.zip /home/tanshu/Programming/csv +cd /home/tanshu/Programming/csv +#tar xvf csv.tar.zip +docker run -it --link postgres:db --rm --env PGPASSWORD="123456" postgres:alpine bash -c 'psql -h db -U postgres -c "drop database petty"' +docker run -it --link postgres:db --rm --env PGPASSWORD="123456" postgres:alpine bash -c 'psql -h db -U postgres -c "create database petty"' +docker run -it -v /home/tanshu/Programming/csv:/mnt --link postgres:db --rm --env PGPASSWORD="123456" postgres:alpine bash -c 'psql -h db -U postgres petty -c "CREATE EXTENSION btree_gist;"' +cd /home/tanshu/Programming/barker/barker alembic upgrade 8c06ac60d125 docker run -it -v /home/tanshu/Programming/csv:/mnt --link postgres:db --rm --env PGPASSWORD="123456" postgres:alpine bash -c 'psql -h db -U postgres petty -c "\copy roles(id, name) from /mnt/a-Roles.csv"' docker run -it -v /home/tanshu/Programming/csv:/mnt --link postgres:db --rm --env PGPASSWORD="123456" postgres:alpine bash -c 'psql -h db -U postgres petty -c "\copy permissions(id, name) from /mnt/b-Permissions.csv"' @@ -18,7 +23,7 @@ docker run -it -v /home/tanshu/Programming/csv:/mnt --link postgres:db --rm --en sed -i ':a;N;$!ba;s/\n\r\n\r\n\r\n\r\n\r\n\r/,/g' /home/tanshu/Programming/csv/n-Printers.csv docker run -it -v /home/tanshu/Programming/csv:/mnt --link postgres:db --rm --env PGPASSWORD="123456" postgres:alpine bash -c 'psql -h db -U postgres petty -c "\copy printers(id, name, address, cut_code) from /mnt/n-Printers.csv"' docker run -it -v /home/tanshu/Programming/csv:/mnt --link postgres:db --rm --env PGPASSWORD="123456" postgres:alpine bash -c 'psql -h db -U postgres petty -c "\copy section_printers(id, menu_category_id, section_name, printer_name, copies) from /mnt/o-SectionPrinters.csv"' -alembic upgrade head +alembic upgrade 34fe3d061c5f sed -i 's/\x0/\\N/g' ~/Programming/csv/p-Vouchers.csv docker run -it -v /home/tanshu/Programming/csv:/mnt --link postgres:db --rm --env PGPASSWORD="123456" postgres:alpine bash -c 'psql -h db -U postgres petty -c "\copy vouchers(id, date, pax, user_id, creation_date, last_edit_date, bill_id, food_table_id, customer_id, narration, reason, voucher_type, kot_id) from /mnt/p-Vouchers.csv"' docker run -it -v /home/tanshu/Programming/csv:/mnt --link postgres:db --rm --env PGPASSWORD="123456" postgres:alpine bash -c 'psql -h db -U postgres petty -c "\copy kots(id, voucher_id, code, food_table_id, date, user_id) from /mnt/q-Kots.csv"' diff --git a/barker/alembic/versions/34fe3d061c5f_finish_import.py b/barker/alembic/versions/34fe3d061c5f_finish_import.py index 820ea3b..4e901ee 100644 --- a/barker/alembic/versions/34fe3d061c5f_finish_import.py +++ b/barker/alembic/versions/34fe3d061c5f_finish_import.py @@ -10,7 +10,7 @@ import sqlalchemy as sa from alembic import op # revision identifiers, used by Alembic. -from sqlalchemy import column, select, table +from sqlalchemy import column, func, select, table, text from sqlalchemy.dialects import postgresql @@ -23,9 +23,13 @@ depends_on = None def upgrade(): # ### commands auto generated by Alembic - please adjust! ### product = table( - "products", column("sale_category_id", postgresql.UUID()), column("sale_category_name", sa.Unicode(length=255)) + "products", + 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)) ) - sale_category = table("sale_categories", column("id", postgresql.UUID()), column("name", sa.Unicode(length=255))) op.execute( product.update( values={ @@ -38,13 +42,13 @@ def upgrade(): section_printer = table( "section_printers", - column("section_id", postgresql.UUID()), + column("section_id", postgresql.UUID(as_uuid=True)), column("section_name", sa.Unicode(length=255)), - column("printer_id", postgresql.UUID()), + column("printer_id", postgresql.UUID(as_uuid=True)), column("printer_name", sa.Unicode(length=255)), ) - section = table("sections", column("id", postgresql.UUID()), column("name", sa.Unicode(length=255))) - printer = table("printers", column("id", postgresql.UUID()), column("name", sa.Unicode(length=255))) + section = table("sections", column("id", postgresql.UUID(as_uuid=True)), column("name", sa.Unicode(length=255))) + printer = table("printers", column("id", postgresql.UUID(as_uuid=True)), column("name", sa.Unicode(length=255))) op.execute( section_printer.update( values={"section_id": select([section.c.id]).where(section_printer.c.section_name == section.c.name)} @@ -55,14 +59,80 @@ def upgrade(): values={"printer_id": select([printer.c.id]).where(section_printer.c.printer_name == printer.c.name)} ) ) - with op.batch_alter_table("products") as batch_op: - batch_op.alter_column("sale_category_id", nullable=True) - batch_op.drop_column("sale_category_name") with op.batch_alter_table("section_printers") as batch_op: - batch_op.alter_column("section_id", nullable=True) + batch_op.alter_column("section_id", nullable=False) batch_op.drop_column("section_name") - batch_op.alter_column("printer_id", nullable=True) + batch_op.alter_column("printer_id", nullable=False) batch_op.drop_column("printer_name") + + 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") + + # Migration to temporal products + op.rename_table("products", "product_histories") + with op.batch_alter_table("product_histories") as batch_op: + batch_op.add_column(sa.Column("valid_from", sa.Date(), nullable=True)) + batch_op.add_column(sa.Column("valid_till", sa.Date(), nullable=True)) + batch_op.drop_column("is_active") + + op.create_table( + "product_versions", + sa.Column("id", postgresql.UUID(as_uuid=True), server_default=sa.text("gen_random_uuid()"), nullable=False), + sa.Column("version_id", postgresql.UUID(as_uuid=True), nullable=False), + sa.ForeignKeyConstraint( + ["version_id"], ["product_histories.id"], name=op.f("fk_product_versions_version_id_product_histories") + ), + sa.PrimaryKeyConstraint("id", name=op.f("pk_product_versions")), + sa.UniqueConstraint("id", "version_id", name=op.f("uq_product_versions_id")), + ) + op.drop_constraint("fk_inventories_product_id_products", "inventories", type_="foreignkey") + op.create_foreign_key( + op.f("fk_inventories_product_id_product_versions"), "inventories", "product_versions", ["product_id"], ["id"] + ) + + pv = table( + "product_versions", + column("id", postgresql.UUID(as_uuid=True)), + column("version_id", postgresql.UUID(as_uuid=True)), + ) + + prod = table( + "product_histories", + column("id", postgresql.UUID(as_uuid=True)), + column("name", postgresql.UUID(as_uuid=True)), + column("units", postgresql.UUID(as_uuid=True)), + column("valid_from", sa.Date()), + column("valid_till", sa.Date()), + ) + op.execute(pv.insert().from_select([pv.c.id, pv.c.version_id], select([prod.c.id, prod.c.id.label("vid")]))) + + 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"], + ) + + op.drop_constraint( + "fk_modifier_categories_products_product_id_products", "modifier_categories_products", type_="foreignkey" + ) + op.create_foreign_key( + op.f("fk_modifier_categories_products_product_id_product_versions"), + "modifier_categories_products", + "product_versions", + ["product_id"], + ["id"], + ) + op.create_exclude_constraint( + "uq_product_histories_name", + "product_histories", + (prod.c.name, "="), + (prod.c.units, "="), + (func.daterange(prod.c.valid_from, prod.c.valid_till, text("'[]'")), "&&"), + ) + op.drop_constraint("uq_products_name", "product_histories", type_="unique") # ### end Alembic commands ### diff --git a/barker/alembic/versions/8c06ac60d125_initial_commit.py b/barker/alembic/versions/8c06ac60d125_initial_commit.py index 0333528..6209f9e 100644 --- a/barker/alembic/versions/8c06ac60d125_initial_commit.py +++ b/barker/alembic/versions/8c06ac60d125_initial_commit.py @@ -24,7 +24,7 @@ def upgrade(): # ### commands auto generated by Alembic - please adjust! ### op.create_table( "customers", - sa.Column("id", postgresql.UUID(), nullable=False), + sa.Column("id", postgresql.UUID(as_uuid=True), server_default=sa.text("gen_random_uuid()"), nullable=False), sa.Column("name", sa.Unicode(length=255), nullable=False), sa.Column("phone", sa.Unicode(length=255), nullable=False), sa.Column("address", sa.Unicode(length=255), nullable=True), @@ -33,7 +33,7 @@ def upgrade(): ) op.create_table( "menu_categories", - sa.Column("id", postgresql.UUID(), nullable=False), + sa.Column("id", postgresql.UUID(as_uuid=True), server_default=sa.text("gen_random_uuid()"), nullable=False), sa.Column("name", sa.Unicode(length=255), nullable=False), sa.Column("discount_limit", sa.Numeric(), nullable=False), sa.Column("is_active", sa.Boolean(), nullable=False), @@ -44,7 +44,7 @@ def upgrade(): ) op.create_table( "modifier_categories", - sa.Column("id", postgresql.UUID(), nullable=False), + sa.Column("id", postgresql.UUID(as_uuid=True), server_default=sa.text("gen_random_uuid()"), nullable=False), sa.Column("name", sa.Unicode(length=255), nullable=False), sa.Column("minimum", sa.Integer(), nullable=False), sa.Column("maximum", sa.Integer(), nullable=True), @@ -55,14 +55,14 @@ def upgrade(): ) op.create_table( "permissions", - sa.Column("id", postgresql.UUID(), nullable=False), + sa.Column("id", postgresql.UUID(as_uuid=True), server_default=sa.text("gen_random_uuid()"), nullable=False), sa.Column("name", sa.Unicode(length=255), nullable=True), sa.PrimaryKeyConstraint("id", name=op.f("pk_permissions")), sa.UniqueConstraint("name", name=op.f("uq_permissions_name")), ) op.create_table( "printers", - sa.Column("id", postgresql.UUID(), nullable=False), + sa.Column("id", postgresql.UUID(as_uuid=True), server_default=sa.text("gen_random_uuid()"), nullable=False), sa.Column("name", sa.Unicode(length=255), nullable=False), sa.Column("address", sa.Unicode(length=255), nullable=False), sa.Column("cut_code", sa.Unicode(length=255), nullable=False), @@ -72,14 +72,14 @@ def upgrade(): ) op.create_table( "roles", - sa.Column("id", postgresql.UUID(), nullable=False), + sa.Column("id", postgresql.UUID(as_uuid=True), server_default=sa.text("gen_random_uuid()"), nullable=False), sa.Column("name", sa.Unicode(length=255), nullable=True), sa.PrimaryKeyConstraint("id", name=op.f("pk_roles")), sa.UniqueConstraint("name", name=op.f("uq_roles_name")), ) op.create_table( "sections", - sa.Column("id", postgresql.UUID(), nullable=False), + sa.Column("id", postgresql.UUID(as_uuid=True), server_default=sa.text("gen_random_uuid()"), nullable=False), sa.Column("name", sa.Unicode(length=255), nullable=False), sa.Column("is_fixture", sa.Boolean(), nullable=False), sa.PrimaryKeyConstraint("id", name=op.f("pk_sections")), @@ -87,7 +87,7 @@ def upgrade(): ) op.create_table( "settings", - sa.Column("id", postgresql.UUID(), nullable=False), + sa.Column("id", postgresql.UUID(as_uuid=True), server_default=sa.text("gen_random_uuid()"), nullable=False), sa.Column("name", sa.Unicode(length=255), nullable=False), sa.Column("data", sa.JSON(), nullable=True), sa.PrimaryKeyConstraint("id", name=op.f("pk_settings")), @@ -105,7 +105,7 @@ def upgrade(): ) op.create_table( "taxes", - sa.Column("id", postgresql.UUID(), nullable=False), + sa.Column("id", postgresql.UUID(as_uuid=True), server_default=sa.text("gen_random_uuid()"), nullable=False), sa.Column("name", sa.Unicode(length=255), nullable=False), sa.Column("rate", sa.Numeric(), nullable=False), sa.Column("is_fixture", sa.Boolean(), nullable=False), @@ -114,7 +114,7 @@ def upgrade(): ) op.create_table( "users", - sa.Column("id", postgresql.UUID(), nullable=False), + sa.Column("id", postgresql.UUID(as_uuid=True), server_default=sa.text("gen_random_uuid()"), nullable=False), sa.Column("name", sa.Unicode(length=255), nullable=False), sa.Column("password", sa.Unicode(length=60), nullable=False), sa.Column("locked_out", sa.Boolean(), nullable=False), @@ -123,9 +123,9 @@ def upgrade(): ) op.create_table( "devices", - sa.Column("id", postgresql.UUID(), nullable=False), + sa.Column("id", postgresql.UUID(as_uuid=True), server_default=sa.text("gen_random_uuid()"), nullable=False), sa.Column("name", sa.Unicode(length=255), nullable=False), - sa.Column("section_id", postgresql.UUID(), nullable=False), + sa.Column("section_id", postgresql.UUID(as_uuid=True), nullable=False), sa.Column("enabled", sa.Boolean(), nullable=False), sa.Column("creation_date", sa.DateTime(timezone=True), nullable=False), sa.ForeignKeyConstraint(["section_id"], ["sections.id"], name=op.f("fk_devices_section_id_sections")), @@ -134,21 +134,21 @@ def upgrade(): ) op.create_table( "login_history", - sa.Column("login_history_id", postgresql.UUID(as_uuid=True), nullable=False), + sa.Column("id", postgresql.UUID(as_uuid=True), server_default=sa.text("gen_random_uuid()"), nullable=False), sa.Column("user_id", postgresql.UUID(as_uuid=True), nullable=False), sa.Column("device_id", postgresql.UUID(as_uuid=True), nullable=False), sa.Column("date", sa.DateTime(timezone=True), nullable=False), sa.ForeignKeyConstraint(["device_id"], ["devices.id"], name=op.f("fk_login_history_device_id_devices")), sa.ForeignKeyConstraint(["user_id"], ["users.id"], name=op.f("fk_login_history_user_id_users")), - sa.PrimaryKeyConstraint("login_history_id", name=op.f("pk_login_history")), + sa.PrimaryKeyConstraint("id", name=op.f("pk_login_history")), sa.UniqueConstraint("user_id", "device_id", "date", name=op.f("uq_login_history_user_id")), ) op.create_table( "food_tables", - sa.Column("id", postgresql.UUID(), nullable=False), + sa.Column("id", postgresql.UUID(as_uuid=True), server_default=sa.text("gen_random_uuid()"), nullable=False), sa.Column("name", sa.Unicode(length=255), nullable=False), sa.Column("seats", sa.Integer(), nullable=False), - sa.Column("section_id", postgresql.UUID(), nullable=False), + sa.Column("section_id", postgresql.UUID(as_uuid=True), nullable=False), sa.Column("is_active", sa.Boolean(), nullable=False), sa.Column("sort_order", sa.Integer(), nullable=False), sa.ForeignKeyConstraint( @@ -161,8 +161,8 @@ def upgrade(): ) op.create_table( "guest_book", - sa.Column("id", postgresql.UUID(), nullable=False), - sa.Column("customer_id", postgresql.UUID(), nullable=False), + sa.Column("id", postgresql.UUID(as_uuid=True), server_default=sa.text("gen_random_uuid()"), nullable=False), + sa.Column("customer_id", postgresql.UUID(as_uuid=True), nullable=False), sa.Column("pax", sa.Numeric(), nullable=False), sa.Column("creation_date", sa.DateTime(timezone=True), nullable=False), sa.ForeignKeyConstraint( @@ -174,12 +174,12 @@ def upgrade(): ) op.create_table( "modifiers", - sa.Column("id", postgresql.UUID(), nullable=False), + sa.Column("id", postgresql.UUID(as_uuid=True), server_default=sa.text("gen_random_uuid()"), nullable=False), sa.Column("name", sa.Unicode(length=255), nullable=False), sa.Column("show_in_bill", sa.Boolean(), nullable=False), sa.Column("price", sa.Numeric(), nullable=False), sa.Column("is_active", sa.Boolean(), nullable=False), - sa.Column("modifier_category_id", postgresql.UUID(), nullable=False), + sa.Column("modifier_category_id", postgresql.UUID(as_uuid=True), nullable=False), sa.ForeignKeyConstraint( ["modifier_category_id"], ["modifier_categories.id"], @@ -190,9 +190,9 @@ def upgrade(): ) op.create_table( "role_permissions", - sa.Column("id", postgresql.UUID(), nullable=False), - sa.Column("permission_id", postgresql.UUID(), nullable=False), - sa.Column("role_id", postgresql.UUID(), nullable=False), + sa.Column("id", postgresql.UUID(as_uuid=True), server_default=sa.text("gen_random_uuid()"), nullable=False), + sa.Column("permission_id", postgresql.UUID(as_uuid=True), nullable=False), + sa.Column("role_id", postgresql.UUID(as_uuid=True), nullable=False), sa.ForeignKeyConstraint( ["permission_id"], ["permissions.id"], @@ -204,20 +204,20 @@ def upgrade(): ) op.create_table( "sale_categories", - sa.Column("id", postgresql.UUID(), nullable=False), + sa.Column("id", postgresql.UUID(as_uuid=True), server_default=sa.text("gen_random_uuid()"), nullable=False), sa.Column("name", sa.Unicode(length=255), nullable=False), - sa.Column("tax_id", postgresql.UUID(), nullable=False), + sa.Column("tax_id", postgresql.UUID(as_uuid=True), nullable=False), sa.ForeignKeyConstraint(["tax_id"], ["taxes.id"], name=op.f("fk_sale_categories_tax_id_taxes")), sa.PrimaryKeyConstraint("id", name=op.f("pk_sale_categories")), sa.UniqueConstraint("name", name=op.f("uq_sale_categories_name")), ) op.create_table( "section_printers", - sa.Column("id", postgresql.UUID(), nullable=False), - sa.Column("menu_category_id", postgresql.UUID(), nullable=True), - sa.Column("section_id", postgresql.UUID(), nullable=True), + sa.Column("id", postgresql.UUID(as_uuid=True), server_default=sa.text("gen_random_uuid()"), nullable=False), + sa.Column("menu_category_id", postgresql.UUID(as_uuid=True), nullable=True), + sa.Column("section_id", postgresql.UUID(as_uuid=True), nullable=True), sa.Column("section_name", sa.Unicode(length=255), nullable=False), - sa.Column("printer_id", postgresql.UUID(), nullable=True), + sa.Column("printer_id", postgresql.UUID(as_uuid=True), nullable=True), sa.Column("printer_name", sa.Unicode(length=255), nullable=False), sa.Column("copies", sa.Integer(), nullable=False), sa.ForeignKeyConstraint( @@ -244,9 +244,9 @@ def upgrade(): ) op.create_table( "user_roles", - sa.Column("id", postgresql.UUID(), nullable=False), - sa.Column("user_id", postgresql.UUID(), nullable=False), - sa.Column("role_id", postgresql.UUID(), nullable=False), + sa.Column("id", postgresql.UUID(as_uuid=True), server_default=sa.text("gen_random_uuid()"), nullable=False), + sa.Column("user_id", postgresql.UUID(as_uuid=True), nullable=False), + sa.Column("role_id", postgresql.UUID(as_uuid=True), nullable=False), sa.ForeignKeyConstraint(["role_id"], ["roles.id"], name=op.f("fk_user_roles_role_id_roles")), sa.ForeignKeyConstraint(["user_id"], ["users.id"], name=op.f("fk_user_roles_user_id_users")), sa.PrimaryKeyConstraint("id", name=op.f("pk_user_roles")), @@ -254,11 +254,11 @@ def upgrade(): ) op.create_table( "products", - sa.Column("id", postgresql.UUID(), nullable=False), + sa.Column("id", postgresql.UUID(as_uuid=True), server_default=sa.text("gen_random_uuid()"), nullable=False), sa.Column("name", sa.Unicode(length=255), nullable=False), sa.Column("units", sa.Unicode(length=255), nullable=False), - sa.Column("menu_category_id", postgresql.UUID(), nullable=False), - sa.Column("sale_category_id", postgresql.UUID(), nullable=True), + sa.Column("menu_category_id", postgresql.UUID(as_uuid=True), nullable=False), + sa.Column("sale_category_id", postgresql.UUID(as_uuid=True), nullable=True), sa.Column("sale_category_name", sa.Unicode(length=255), nullable=True), sa.Column("price", sa.Numeric(), nullable=False), sa.Column("has_happy_hour", sa.Boolean(), nullable=False), @@ -281,19 +281,19 @@ def upgrade(): ) op.create_table( "vouchers", - sa.Column("id", postgresql.UUID(), nullable=False), + sa.Column("id", postgresql.UUID(as_uuid=True), server_default=sa.text("gen_random_uuid()"), nullable=False), sa.Column("date", sa.DateTime(), nullable=False), sa.Column("pax", sa.Numeric(), nullable=False), sa.Column("bill_id", sa.Numeric(), nullable=True), sa.Column("kot_id", sa.Numeric(), nullable=False), sa.Column("creation_date", sa.DateTime(timezone=True), nullable=False), sa.Column("last_edit_date", sa.DateTime(timezone=True), nullable=False), - sa.Column("food_table_id", postgresql.UUID(), nullable=False), - sa.Column("customer_id", postgresql.UUID(), nullable=True), + sa.Column("food_table_id", postgresql.UUID(as_uuid=True), nullable=False), + sa.Column("customer_id", postgresql.UUID(as_uuid=True), nullable=True), sa.Column("narration", sa.Unicode(length=1000), nullable=True), sa.Column("reason", sa.Unicode(length=255), nullable=True), sa.Column("voucher_type", sa.Integer(), nullable=False), - sa.Column("user_id", postgresql.UUID(), nullable=False), + sa.Column("user_id", postgresql.UUID(as_uuid=True), nullable=False), sa.ForeignKeyConstraint( ["customer_id"], ["customers.id"], @@ -312,12 +312,12 @@ def upgrade(): op.create_index(op.f("ix_vouchers_date"), "vouchers", ["date"], unique=False) op.create_table( "kots", - sa.Column("id", postgresql.UUID(), nullable=False), - sa.Column("voucher_id", postgresql.UUID(), nullable=False), + sa.Column("id", postgresql.UUID(as_uuid=True), server_default=sa.text("gen_random_uuid()"), nullable=False), + sa.Column("voucher_id", postgresql.UUID(as_uuid=True), nullable=False), sa.Column("code", sa.Numeric(), nullable=False), - sa.Column("food_table_id", postgresql.UUID(), nullable=False), + sa.Column("food_table_id", postgresql.UUID(as_uuid=True), nullable=False), sa.Column("date", sa.DateTime(), nullable=False), - sa.Column("user_id", postgresql.UUID(), nullable=False), + sa.Column("user_id", postgresql.UUID(as_uuid=True), nullable=False), sa.ForeignKeyConstraint( ["food_table_id"], ["food_tables.id"], @@ -332,9 +332,9 @@ def upgrade(): op.create_index(op.f("ix_kots_voucher_id"), "kots", ["voucher_id"], unique=False) op.create_table( "modifier_categories_products", - sa.Column("id", postgresql.UUID(), nullable=False), - sa.Column("product_id", postgresql.UUID(), nullable=False), - sa.Column("modifier_categories_id", postgresql.UUID(), nullable=False), + sa.Column("id", postgresql.UUID(as_uuid=True), server_default=sa.text("gen_random_uuid()"), nullable=False), + sa.Column("product_id", postgresql.UUID(as_uuid=True), nullable=False), + sa.Column("modifier_categories_id", postgresql.UUID(as_uuid=True), nullable=False), sa.ForeignKeyConstraint( ["modifier_categories_id"], ["modifier_categories.id"], @@ -354,10 +354,10 @@ def upgrade(): ) op.create_table( "overview", - sa.Column("id", postgresql.UUID(), nullable=False), - sa.Column("voucher_id", postgresql.UUID(), nullable=False), - sa.Column("food_table_id", postgresql.UUID(), nullable=False), - sa.Column("guest_book_id", postgresql.UUID(), nullable=True), + sa.Column("id", postgresql.UUID(as_uuid=True), server_default=sa.text("gen_random_uuid()"), nullable=False), + sa.Column("voucher_id", postgresql.UUID(as_uuid=True), nullable=False), + sa.Column("food_table_id", postgresql.UUID(as_uuid=True), nullable=False), + sa.Column("guest_book_id", postgresql.UUID(as_uuid=True), nullable=True), sa.Column("status", sa.Unicode(length=255), nullable=False), sa.ForeignKeyConstraint( ["food_table_id"], @@ -381,10 +381,10 @@ def upgrade(): ) op.create_table( "reprints", - sa.Column("id", postgresql.UUID(), nullable=False), + sa.Column("id", postgresql.UUID(as_uuid=True), server_default=sa.text("gen_random_uuid()"), nullable=False), sa.Column("date", sa.DateTime(), nullable=False), - sa.Column("voucher_id", postgresql.UUID(), nullable=False), - sa.Column("user_id", postgresql.UUID(), nullable=False), + sa.Column("voucher_id", postgresql.UUID(as_uuid=True), nullable=False), + sa.Column("user_id", postgresql.UUID(as_uuid=True), nullable=False), sa.ForeignKeyConstraint(["user_id"], ["users.id"], name=op.f("fk_reprints_user_id_users")), sa.ForeignKeyConstraint( ["voucher_id"], @@ -397,8 +397,8 @@ def upgrade(): op.create_index(op.f("ix_reprints_voucher_id"), "reprints", ["voucher_id"], unique=False) op.create_table( "settlements", - sa.Column("id", postgresql.UUID(), nullable=False), - sa.Column("voucher_id", postgresql.UUID(), nullable=False), + sa.Column("id", postgresql.UUID(as_uuid=True), server_default=sa.text("gen_random_uuid()"), nullable=False), + sa.Column("voucher_id", postgresql.UUID(as_uuid=True), nullable=False), sa.Column("settled", sa.Integer(), nullable=False), sa.Column("amount", sa.Numeric(), nullable=False), sa.ForeignKeyConstraint( @@ -417,14 +417,14 @@ def upgrade(): op.create_index(op.f("ix_settlements_voucher_id"), "settlements", ["voucher_id"], unique=False) op.create_table( "inventories", - sa.Column("id", postgresql.UUID(), nullable=False), - sa.Column("kot_id", postgresql.UUID(), nullable=False), - sa.Column("product_id", postgresql.UUID(), nullable=False), + sa.Column("id", postgresql.UUID(as_uuid=True), server_default=sa.text("gen_random_uuid()"), nullable=False), + sa.Column("kot_id", postgresql.UUID(as_uuid=True), nullable=False), + sa.Column("product_id", postgresql.UUID(as_uuid=True), nullable=False), sa.Column("quantity", sa.Numeric(), nullable=True), sa.Column("price", sa.Numeric(), nullable=True), sa.Column("is_happy_hour", sa.Boolean(), nullable=False), sa.Column("tax_rate", sa.Numeric(), nullable=True), - sa.Column("tax_id", postgresql.UUID(), nullable=False), + sa.Column("tax_id", postgresql.UUID(as_uuid=True), nullable=False), sa.Column("discount", sa.Numeric(), nullable=True), sa.Column("sort_order", sa.Numeric(), nullable=False), sa.ForeignKeyConstraint(["kot_id"], ["kots.id"], name=op.f("fk_inventories_kot_id_kots")), @@ -446,9 +446,9 @@ def upgrade(): op.create_index(op.f("ix_inventories_kot_id"), "inventories", ["kot_id"], unique=False) op.create_table( "inventory_modifiers", - sa.Column("id", postgresql.UUID(), nullable=False), - sa.Column("inventory_id", postgresql.UUID(), nullable=False), - sa.Column("modifier_id", postgresql.UUID(), nullable=False), + sa.Column("id", postgresql.UUID(as_uuid=True), server_default=sa.text("gen_random_uuid()"), nullable=False), + sa.Column("inventory_id", postgresql.UUID(as_uuid=True), nullable=False), + sa.Column("modifier_id", postgresql.UUID(as_uuid=True), nullable=False), sa.Column("price", sa.Numeric(), nullable=False), sa.ForeignKeyConstraint( ["inventory_id"], diff --git a/barker/barker/models/__init__.py b/barker/barker/models/__init__.py index 154093a..1c5221b 100644 --- a/barker/barker/models/__init__.py +++ b/barker/barker/models/__init__.py @@ -10,7 +10,8 @@ from .master import ( Modifier, ModifierCategory, Printer, - Product, + ProductHistory, + ProductVersions, SaleCategory, Section, SectionPrinter, diff --git a/barker/barker/models/auth.py b/barker/barker/models/auth.py index a02926a..c45fbd9 100644 --- a/barker/barker/models/auth.py +++ b/barker/barker/models/auth.py @@ -5,7 +5,7 @@ import uuid from datetime import datetime from hashlib import md5 -from sqlalchemy import Boolean, Column, DateTime, Unicode, UniqueConstraint, desc +from sqlalchemy import Boolean, Column, DateTime, Unicode, UniqueConstraint, desc, text from sqlalchemy.dialects.postgresql import UUID from sqlalchemy.orm import Session, relationship, synonym from sqlalchemy.schema import ForeignKey, Table @@ -20,7 +20,9 @@ def encrypt(val): class LoginHistory(Base): __tablename__ = "login_history" __table_args__ = (UniqueConstraint("user_id", "device_id", "date"),) - id = Column("login_history_id", UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) + id = Column( + "id", UUID(as_uuid=True), primary_key=True, server_default=text("gen_random_uuid()"), default=uuid.uuid4 + ) user_id = Column("user_id", UUID(as_uuid=True), ForeignKey("users.id"), nullable=False) device_id = Column( "device_id", @@ -40,7 +42,9 @@ class LoginHistory(Base): class Device(Base): __tablename__ = "devices" - id = Column("id", UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) + id = Column( + "id", UUID(as_uuid=True), primary_key=True, server_default=text("gen_random_uuid()"), default=uuid.uuid4 + ) name = Column("name", Unicode(255), unique=True, nullable=False) enabled = Column("enabled", Boolean, nullable=False) section_id = Column("section_id", UUID(as_uuid=True), ForeignKey("sections.id"), nullable=False) @@ -68,7 +72,7 @@ class Device(Base): user_roles = Table( "user_roles", Base.metadata, - Column("id", UUID(as_uuid=True), primary_key=True, default=uuid.uuid4), + Column("id", UUID(as_uuid=True), primary_key=True, server_default=text("gen_random_uuid()"), default=uuid.uuid4), Column("user_id", UUID(as_uuid=True), ForeignKey("users.id"), nullable=False), Column("role_id", UUID(as_uuid=True), ForeignKey("roles.id"), nullable=False), UniqueConstraint("user_id", "role_id"), @@ -77,7 +81,7 @@ user_roles = Table( role_permissions = Table( "role_permissions", Base.metadata, - Column("id", UUID(as_uuid=True), primary_key=True, default=uuid.uuid4), + Column("id", UUID(as_uuid=True), primary_key=True, server_default=text("gen_random_uuid()"), default=uuid.uuid4), Column( "permission_id", UUID(as_uuid=True), @@ -92,7 +96,9 @@ role_permissions = Table( class User(Base): __tablename__ = "users" - id = Column("id", UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) + id = Column( + "id", UUID(as_uuid=True), primary_key=True, server_default=text("gen_random_uuid()"), default=uuid.uuid4 + ) name = Column("name", Unicode(255), unique=True, nullable=False) _password = Column("password", Unicode(60), nullable=False) locked_out = Column("locked_out", Boolean, nullable=False) @@ -135,7 +141,9 @@ class User(Base): class Role(Base): __tablename__ = "roles" - id = Column("id", UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) + id = Column( + "id", UUID(as_uuid=True), primary_key=True, server_default=text("gen_random_uuid()"), default=uuid.uuid4 + ) name = Column("name", Unicode(255), unique=True) def __init__(self, name=None, id_=None): @@ -146,7 +154,9 @@ class Role(Base): class Permission(Base): __tablename__ = "permissions" - id = Column("id", UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) + id = Column( + "id", UUID(as_uuid=True), primary_key=True, server_default=text("gen_random_uuid()"), default=uuid.uuid4 + ) name = Column("name", Unicode(255), unique=True) roles = relationship("Role", secondary=role_permissions, backref="permissions") diff --git a/barker/barker/models/master.py b/barker/barker/models/master.py index 94ccdf3..9d654d2 100644 --- a/barker/barker/models/master.py +++ b/barker/barker/models/master.py @@ -4,6 +4,7 @@ from sqlalchemy import ( JSON, Boolean, Column, + Date, ForeignKey, Integer, Numeric, @@ -11,7 +12,10 @@ from sqlalchemy import ( Unicode, UniqueConstraint, case, + func, + text, ) +from sqlalchemy.dialects import postgresql from sqlalchemy.dialects.postgresql import UUID from sqlalchemy.ext.hybrid import hybrid_property from sqlalchemy.orm import relationship @@ -22,10 +26,12 @@ from .meta import Base class Customer(Base): __tablename__ = "customers" - id = Column("id", UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) + id = Column( + "id", UUID(as_uuid=True), primary_key=True, server_default=text("gen_random_uuid()"), default=uuid.uuid4 + ) name = Column("name", Unicode(255), nullable=False) - phone = Column("phone", Unicode(255), nullable=True, unique=True) - address = Column("address", Unicode(255), nullable=False) + phone = Column("phone", Unicode(255), nullable=False, unique=True) + address = Column("address", Unicode(255), nullable=True) @property def __name__(self): @@ -45,7 +51,9 @@ class Customer(Base): class FoodTable(Base): __tablename__ = "food_tables" - id = Column("id", UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) + id = Column( + "id", UUID(as_uuid=True), primary_key=True, server_default=text("gen_random_uuid()"), default=uuid.uuid4 + ) name = Column("name", Unicode(255), nullable=False, unique=True) seats = Column("seats", Integer, nullable=False) section_id = Column("section_id", UUID(as_uuid=True), ForeignKey("sections.id"), nullable=False) @@ -78,7 +86,9 @@ class FoodTable(Base): class Tax(Base): __tablename__ = "taxes" - id = Column("id", UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) + id = Column( + "id", UUID(as_uuid=True), primary_key=True, server_default=text("gen_random_uuid()"), default=uuid.uuid4 + ) name = Column("name", Unicode(255), nullable=False, unique=True) rate = Column("rate", Numeric, nullable=False) is_fixture = Column("is_fixture", Boolean, nullable=False) @@ -93,7 +103,9 @@ class Tax(Base): class MenuCategory(Base): __tablename__ = "menu_categories" - id = Column("id", UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) + id = Column( + "id", UUID(as_uuid=True), primary_key=True, server_default=text("gen_random_uuid()"), default=uuid.uuid4 + ) name = Column("name", Unicode(255), nullable=False, unique=True) discount_limit = Column("discount_limit", Numeric, nullable=False) @@ -113,7 +125,9 @@ class MenuCategory(Base): class SaleCategory(Base): __tablename__ = "sale_categories" - id = Column("id", UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) + id = Column( + "id", UUID(as_uuid=True), primary_key=True, server_default=text("gen_random_uuid()"), default=uuid.uuid4 + ) name = Column("name", Unicode(255), nullable=False, unique=True) tax_id = Column("tax_id", UUID(as_uuid=True), ForeignKey("taxes.id"), nullable=False) tax = relationship("Tax", foreign_keys=tax_id) @@ -124,11 +138,35 @@ class SaleCategory(Base): self.id = id_ -class Product(Base): - __tablename__ = "products" - __table_args__ = (UniqueConstraint("name", "units"),) +class ProductVersions(Base): + __tablename__ = "product_versions" + __table_args__ = (UniqueConstraint("id", "version_id"),) + id = Column( + "id", UUID(as_uuid=True), primary_key=True, server_default=text("gen_random_uuid()"), default=uuid.uuid4 + ) + version_id = Column("version_id", UUID(as_uuid=True), ForeignKey("product_histories.id"), nullable=False) - id = Column("id", UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) + +modifier_categories_products = Table( + "modifier_categories_products", + Base.metadata, + Column("id", UUID(as_uuid=True), primary_key=True, server_default=text("gen_random_uuid()"), default=uuid.uuid4), + Column("product_id", UUID(as_uuid=True), ForeignKey("product_versions.id"), nullable=False), + Column( + "modifier_category_id", + UUID(as_uuid=True), + ForeignKey("modifier_categories.id"), + nullable=False, + ), + UniqueConstraint("product_id", "modifier_category_id"), +) + + +class ProductHistory(Base): + __tablename__ = "product_histories" + id = Column( + "id", UUID(as_uuid=True), primary_key=True, server_default=text("gen_random_uuid()"), default=uuid.uuid4 + ) name = Column("name", Unicode(255), nullable=False) units = Column("units", Unicode(255), nullable=False) menu_category_id = Column( @@ -147,18 +185,31 @@ class Product(Base): has_happy_hour = Column("has_happy_hour", Boolean, nullable=False) is_not_available = Column("is_not_available", Boolean, nullable=False) quantity = Column("quantity", Numeric, nullable=False) - is_active = Column("is_active", Boolean, nullable=False) sort_order = Column("sort_order", Integer, nullable=False) + valid_from = Column("valid_from", Date(), nullable=True) + valid_till = Column("valid_till", Date(), nullable=True) + menu_category = relationship("MenuCategory", backref="products") sale_category = relationship("SaleCategory", backref="products") modifier_categories = relationship( "ModifierCategory", - secondary="modifier_categories_products", + secondary="join(product_versions, modifier_categories_products, " + "ProductVersions.id==modifier_categories_products.c.product_id)", + primaryjoin="ProductHistory.id==ProductVersions.version_id", + secondaryjoin="modifier_categories_products.c.modifier_category_id==ModifierCategory.id", order_by="ModifierCategory.sort_order", backref="products", ) + __table_args__ = ( + postgresql.ExcludeConstraint( + (name, "="), + (units, "="), + (func.daterange(valid_from, valid_till, text("'[]'")), "&&"), + ), + ) + def __init__( self, name=None, @@ -203,25 +254,12 @@ class Product(Base): return True, "" -modifier_categories_products = Table( - "modifier_categories_products", - Base.metadata, - Column("id", UUID(as_uuid=True), primary_key=True, default=uuid.uuid4), - Column("product_id", UUID(as_uuid=True), ForeignKey("products.id"), nullable=False), - Column( - "modifier_categories_id", - UUID(as_uuid=True), - ForeignKey("modifier_categories.id"), - nullable=False, - ), - UniqueConstraint("product_id", "modifier_categories_id"), -) - - class ModifierCategory(Base): __tablename__ = "modifier_categories" - id = Column("id", UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) + id = Column( + "id", UUID(as_uuid=True), primary_key=True, server_default=text("gen_random_uuid()"), default=uuid.uuid4 + ) name = Column("name", Unicode(255), nullable=False, unique=True) minimum = Column("minimum", Integer, nullable=False) maximum = Column("maximum", Integer, nullable=True) @@ -248,7 +286,9 @@ class ModifierCategory(Base): class Modifier(Base): __tablename__ = "modifiers" - id = Column("id", UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) + id = Column( + "id", UUID(as_uuid=True), primary_key=True, server_default=text("gen_random_uuid()"), default=uuid.uuid4 + ) name = Column("name", Unicode(255), nullable=False, unique=True) show_in_bill = Column("show_in_bill", Boolean, nullable=False) price = Column("price", Numeric, nullable=False) @@ -282,7 +322,9 @@ class Modifier(Base): class DbSetting(Base): __tablename__ = "settings" - id = Column("id", UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) + id = Column( + "id", UUID(as_uuid=True), primary_key=True, server_default=text("gen_random_uuid()"), default=uuid.uuid4 + ) name = Column("name", Unicode(255), unique=True, nullable=False) data = Column("data", JSON) @@ -295,7 +337,9 @@ class DbSetting(Base): class Section(Base): __tablename__ = "sections" - id = Column("id", UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) + id = Column( + "id", UUID(as_uuid=True), primary_key=True, server_default=text("gen_random_uuid()"), default=uuid.uuid4 + ) name = Column("name", Unicode(255), unique=True, nullable=False) is_fixture = Column("is_fixture", Boolean, nullable=False, default=False) @@ -312,7 +356,9 @@ class Section(Base): class Printer(Base): __tablename__ = "printers" - id = Column("id", UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) + id = Column( + "id", UUID(as_uuid=True), primary_key=True, server_default=text("gen_random_uuid()"), default=uuid.uuid4 + ) name = Column("name", Unicode(255), unique=True, nullable=False) address = Column("address", Unicode(255), unique=True, nullable=False) cut_code = Column("cut_code", Unicode(255), nullable=False) @@ -328,7 +374,9 @@ class SectionPrinter(Base): __tablename__ = "section_printers" __table_args__ = (UniqueConstraint("menu_category_id", "section_id"),) - id = Column("id", UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) + id = Column( + "id", UUID(as_uuid=True), primary_key=True, server_default=text("gen_random_uuid()"), default=uuid.uuid4 + ) menu_category_id = Column("menu_category_id", UUID(as_uuid=True), ForeignKey("menu_categories.id")) section_id = Column("section_id", UUID(as_uuid=True), ForeignKey("sections.id"), nullable=False) printer_id = Column("printer_id", UUID(as_uuid=True), ForeignKey("printers.id"), nullable=False) diff --git a/barker/barker/models/voucher.py b/barker/barker/models/voucher.py index 19a12d8..47dedeb 100644 --- a/barker/barker/models/voucher.py +++ b/barker/barker/models/voucher.py @@ -15,11 +15,13 @@ from sqlalchemy import ( UniqueConstraint, case, func, + text, ) from sqlalchemy.dialects.postgresql import UUID from sqlalchemy.ext.hybrid import hybrid_property from sqlalchemy.orm import backref, relationship, synonym +from .master import ProductVersions from .meta import Base @@ -33,7 +35,9 @@ class VoucherType(Enum): class GuestBook(Base): __tablename__ = "guest_book" - id = Column("id", UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) + id = Column( + "id", UUID(as_uuid=True), primary_key=True, server_default=text("gen_random_uuid()"), default=uuid.uuid4 + ) customer_id = Column("customer_id", UUID(as_uuid=True), ForeignKey("customers.id"), nullable=False) pax = Column("pax", Numeric, nullable=False) date = Column("creation_date", DateTime(timezone=True), nullable=False) @@ -53,7 +57,9 @@ class GuestBook(Base): class Overview(Base): __tablename__ = "overview" - id = Column("id", UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) + id = Column( + "id", UUID(as_uuid=True), primary_key=True, server_default=text("gen_random_uuid()"), default=uuid.uuid4 + ) voucher_id = Column( "voucher_id", UUID(as_uuid=True), @@ -87,7 +93,9 @@ class InventoryModifier(Base): __tablename__ = "inventory_modifiers" __table_args__ = (UniqueConstraint("inventory_id", "modifier_id"),) - id = Column("id", UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) + id = Column( + "id", UUID(as_uuid=True), primary_key=True, server_default=text("gen_random_uuid()"), default=uuid.uuid4 + ) inventory_id = Column("inventory_id", UUID(as_uuid=True), ForeignKey("inventories.id"), nullable=False) modifier_id = Column("modifier_id", UUID(as_uuid=True), ForeignKey("modifiers.id"), nullable=False) price = Column("price", Numeric, nullable=False) @@ -105,7 +113,9 @@ class Voucher(Base): __tablename__ = "vouchers" __table_args__ = (UniqueConstraint("bill_id", "voucher_type"),) - id = Column("id", UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) + id = Column( + "id", UUID(as_uuid=True), primary_key=True, server_default=text("gen_random_uuid()"), default=uuid.uuid4 + ) date = Column("date", DateTime, nullable=False, index=True) pax = Column("pax", Numeric, nullable=False) bill_id = Column("bill_id", Numeric) @@ -202,7 +212,9 @@ class Voucher(Base): class Kot(Base): __tablename__ = "kots" - id = Column("id", UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) + id = Column( + "id", UUID(as_uuid=True), primary_key=True, server_default=text("gen_random_uuid()"), default=uuid.uuid4 + ) voucher_id = Column( "voucher_id", UUID(as_uuid=True), @@ -244,7 +256,9 @@ class Settlement(Base): __tablename__ = "settlements" __table_args__ = (UniqueConstraint("voucher_id", "settled"),) - id = Column("id", UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) + id = Column( + "id", UUID(as_uuid=True), primary_key=True, server_default=text("gen_random_uuid()"), default=uuid.uuid4 + ) voucher_id = Column( "voucher_id", UUID(as_uuid=True), @@ -267,7 +281,9 @@ class Settlement(Base): class Reprint(Base): __tablename__ = "reprints" - id = Column("id", UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) + id = Column( + "id", UUID(as_uuid=True), primary_key=True, server_default=text("gen_random_uuid()"), default=uuid.uuid4 + ) date = Column("date", DateTime, nullable=False, index=True) voucher_id = Column( "voucher_id", @@ -291,9 +307,11 @@ class Inventory(Base): __tablename__ = "inventories" __table_args__ = (UniqueConstraint("kot_id", "product_id", "is_happy_hour", "price"),) - id = Column("id", UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) + id = Column( + "id", UUID(as_uuid=True), primary_key=True, server_default=text("gen_random_uuid()"), default=uuid.uuid4 + ) kot_id = Column("kot_id", UUID(as_uuid=True), ForeignKey("kots.id"), nullable=False, index=True) - product_id = Column("product_id", UUID(as_uuid=True), ForeignKey("products.id"), nullable=False) + product_id = Column("product_id", UUID(as_uuid=True), ForeignKey("product_versions.id"), nullable=False) quantity = Column("quantity", Numeric) price = Column("price", Numeric) is_happy_hour = Column("is_happy_hour", Boolean, nullable=False) @@ -304,7 +322,7 @@ class Inventory(Base): kot = relationship("Kot", backref="inventories") tax = relationship("Tax", foreign_keys=tax_id) - product = relationship("Product", backref="inventories") + product = relationship("ProductHistory", secondary=ProductVersions.__table__, backref="inventories") def __init__( self,