From 802eded5688b934337d60ecc696b2adfca389896 Mon Sep 17 00:00:00 2001 From: Amritanshu Date: Sun, 26 Feb 2023 19:51:29 +0530 Subject: [PATCH] Due to update to sqlalchemy, fixed schema nullability --- barker/alembic/env.py | 4 +- .../versions/f135019a4ebf_fix_nullability.py | 39 ++++ barker/barker/db/init_db.py | 217 ------------------ barker/barker/models/inventory.py | 8 +- barker/barker/models/overview.py | 2 +- barker/barker/models/permission.py | 2 +- barker/barker/models/role.py | 2 +- barker/barker/models/section_printer.py | 2 +- barker/barker/models/voucher.py | 6 +- 9 files changed, 53 insertions(+), 229 deletions(-) create mode 100644 barker/alembic/versions/f135019a4ebf_fix_nullability.py delete mode 100644 barker/barker/db/init_db.py diff --git a/barker/alembic/env.py b/barker/alembic/env.py index 37812f5..869e48d 100644 --- a/barker/alembic/env.py +++ b/barker/alembic/env.py @@ -2,7 +2,7 @@ import logging from alembic import context from barker.core.config import settings -from barker.db.base import User +from barker.db.base import reg from sqlalchemy import create_engine, pool @@ -14,7 +14,7 @@ logging.getLogger("alembic").setLevel(settings.ALEMBIC_LOG_LEVEL) # This line sets up loggers basically. -target_metadata = User.metadata +target_metadata = reg.metadata # other values from the config, defined by the needs of env.py, # can be acquired: diff --git a/barker/alembic/versions/f135019a4ebf_fix_nullability.py b/barker/alembic/versions/f135019a4ebf_fix_nullability.py new file mode 100644 index 0000000..2f4c365 --- /dev/null +++ b/barker/alembic/versions/f135019a4ebf_fix_nullability.py @@ -0,0 +1,39 @@ +"""fix nullability + +Revision ID: f135019a4ebf +Revises: 0e326930b8a4 +Create Date: 2023-02-22 03:41:23.537388 + +""" +import sqlalchemy as sa + +from alembic import op + + +# revision identifiers, used by Alembic. +revision = "f135019a4ebf" +down_revision = "0e326930b8a4" +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.alter_column("inventories", "quantity", existing_type=sa.NUMERIC(precision=15, scale=2), nullable=False) + op.alter_column("inventories", "price", existing_type=sa.NUMERIC(precision=15, scale=2), nullable=False) + op.alter_column("inventories", "tax_rate", existing_type=sa.NUMERIC(precision=15, scale=5), nullable=False) + op.alter_column("inventories", "discount", existing_type=sa.NUMERIC(precision=15, scale=5), nullable=False) + op.alter_column("permissions", "name", existing_type=sa.VARCHAR(length=255), nullable=False) + op.alter_column("roles", "name", existing_type=sa.VARCHAR(length=255), nullable=False) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.alter_column("roles", "name", existing_type=sa.VARCHAR(length=255), nullable=True) + op.alter_column("permissions", "name", existing_type=sa.VARCHAR(length=255), nullable=True) + op.alter_column("inventories", "discount", existing_type=sa.NUMERIC(precision=15, scale=5), nullable=True) + op.alter_column("inventories", "tax_rate", existing_type=sa.NUMERIC(precision=15, scale=5), nullable=True) + op.alter_column("inventories", "price", existing_type=sa.NUMERIC(precision=15, scale=2), nullable=True) + op.alter_column("inventories", "quantity", existing_type=sa.NUMERIC(precision=15, scale=2), nullable=True) + # ### end Alembic commands ### diff --git a/barker/barker/db/init_db.py b/barker/barker/db/init_db.py deleted file mode 100644 index 18e7352..0000000 --- a/barker/barker/db/init_db.py +++ /dev/null @@ -1,217 +0,0 @@ -import uuid - -from barker.core.config import settings # noqa: F401 -from barker.db.base import reg # noqa: F401 -from barker.models.customer import Customer -from barker.models.db_setting import DbSetting -from barker.models.modifier_category import ModifierCategory -from barker.models.permission import Permission -from barker.models.role import Role -from barker.models.section import Section -from barker.models.settle_option import SettleOption -from barker.models.user import User -from sqlalchemy.orm import Session - - -# make sure all SQL Alchemy models are imported (app.db.base) before initializing DB -# otherwise, SQL Alchemy might fail to initialize relationships properly -# for more details: https://github.com/tiangolo/full-stack-fastapi-postgresql/issues/28 - - -def init_db(db: Session) -> None: - # Tables should be created with Alembic migrations - # But if you don't want to use migrations, create - # the tables un-commenting the next line - # Base.metadata.create_all(bind=engine) - - prod = True - if prod: - db.add(Permission("Guest Book", uuid.UUID("7669dfc9-cc75-4e48-b267-145c8832a83c"))) - db.add(Permission("Printers", uuid.UUID("5b66c6f6-003a-4ef8-ba28-49b8ff1ac33c"))) - db.add(Permission("Sections", uuid.UUID("c973f32c-a37b-496a-8dc5-60d2e4c39e97"))) - db.add( - Permission( - "Section Printers", - uuid.UUID("7a04ba63-5d08-4078-9051-a6d91cce3e48"), - ) - ) - else: - add_permissions(db) - - main_section = Section("Main", uuid.UUID("3f13f6e7-dc76-4fca-8fdb-b2bbf29b35df")) - db.add(main_section) - mcs = [ - ModifierCategory( - "Old Modifiers", - 0, - 0, - True, - 0, - uuid.UUID("e046ad33-dc65-4c78-8833-c3d3538d44c0"), - ), - ModifierCategory( - "Bar Instructions", - 0, - 0, - True, - 0, - uuid.UUID("b572f401-3c2f-48b9-8973-ada5a6e4d3a6"), - ), - ModifierCategory( - "Delivery", - 0, - 0, - True, - 0, - uuid.UUID("caa72832-5034-405e-8442-68a8cc12ace9"), - ), - ModifierCategory( - "Kitchen Instructions", - 0, - 0, - True, - 0, - uuid.UUID("d6a0595f-e209-42e4-bb12-b7499f9a9c4d"), - ), - ModifierCategory( - "Mixers", - 0, - 0, - True, - 0, - uuid.UUID("60ca9122-adc5-463b-ad5f-33a68df8c3ae"), - ), - ModifierCategory( - "Pasta Sauce", - 0, - 0, - True, - 0, - uuid.UUID("ef5b1a0b-5eb1-45ff-bd82-3209c8b888df"), - ), - ] - for item in mcs: - db.add(item) - - options = [ - SettleOption("Unsettled", False, 1, True, 1), - SettleOption("Cash", True, 2, False, 2), - SettleOption("Credit Card", True, 2, True, 3), - SettleOption("No Charge", True, 3, True, 4), - SettleOption("Bill To Company", True, 2, True, 5), - SettleOption("Tip", True, 2, True, 6), - SettleOption("Round Off", False, 1, False, 7), - SettleOption("Amount", False, 1, False, 8), - SettleOption("Void", True, 1, True, 9), - SettleOption("Staff", True, 4, True, 10), - ] - for option in options: - db.add(option) - db.add(Customer("Cash", "", "", uuid.UUID("2c716f4b-0736-429a-ad51-610d7c47cb5e"))) - db.add( - DbSetting( - uuid.UUID("fb738ba2-a3c9-40ed-891c-b930e6454974"), - "Header", - { - "Text": """ Hops n Grains - The Microbrewery - SCO 358, Sector 9, Panchkula - A Unit of Peitho Foods Pvt. Ltd. - CIN: U15139CH2010PTC032202 - (Reg Add: Plot No. 907, Indl Area II, Chd) - TIN: 06592507323 - Service Tax: AAFCP5097GSD001 - """ - }, - ) - ) - db.add( - DbSetting( - uuid.UUID("f7799871-d16e-4c4d-9b57-2299a5839acb"), - "Footer", - {"Text": "Call: 0172-4026666, 8054923853, 8054923856"}, - ) - ) - - -def add_permissions(db: Session): - permissions = [ - Permission("Audit", uuid.UUID("f30fd1fb-df09-46f5-8c5d-181fd46f38de")), - Permission("Beer Consumption", uuid.UUID("efbb8f31-9631-4491-92f4-17cc98e6a0c0")), - Permission("Bill Details", uuid.UUID("612bb529-b50d-4653-a1c0-ebb725c7d728")), - Permission("Cashier Checkout", uuid.UUID("a86f8bcf-66f4-4c44-89e1-b714488b8331")), - Permission("Change Rate", uuid.UUID("10c63aae-0e48-4e54-b3b8-dd8a80b88fbf")), - Permission("Customers", uuid.UUID("e5fef133-cdbe-441f-bb54-1f0db0c5db79")), - Permission("Discount", uuid.UUID("1f66f131-0105-4466-8f8e-21e0ccc2ac27")), - Permission("Discount Report", uuid.UUID("0d8ba1d5-6731-417c-ab0e-be03cfdc96db")), - Permission("Edit Printed Bill", uuid.UUID("4ff6cb0f-93cb-4760-8219-4de280eaa957")), - Permission( - "Edit Printed Product", - uuid.UUID("959713be-d753-4e14-8ecd-e27f33587499"), - ), - Permission("Guest Book", uuid.UUID("7669dfc9-cc75-4e48-b267-145c8832a83c")), - Permission("Machines", uuid.UUID("f12b573f-edcb-490d-91c3-fa76f6502ffd")), - Permission("Merge Kots", uuid.UUID("bed774e9-4857-43b0-a4af-40230c9eb5db")), - Permission("Merge Tables", uuid.UUID("dfc493fb-ac14-4602-8596-f93f47b617de")), - Permission("Modifiers", uuid.UUID("9dc82529-1e86-41b4-a152-eaea9f775fea")), - Permission( - "Move Kot to New Table", - uuid.UUID("5ed5796a-ca99-437f-8f67-483b1ade81ed"), - ), - Permission("Move Table", uuid.UUID("cfdb69c9-d37a-40af-bef2-e29c04602543")), - Permission("NC Product", uuid.UUID("54263587-773e-4cbb-b1e4-4e814141158e")), - Permission("Open Bill", uuid.UUID("5811d233-a1ae-4d32-af52-cebbf46d274a")), - Permission("Owner", uuid.UUID("6d109b66-8452-42aa-bbe9-d4cef440b7a1")), - Permission("Print Bill", uuid.UUID("92242eae-bd38-4223-829b-2f981933b7f2")), - Permission("Print Kot", uuid.UUID("0eef56fb-7741-49fd-8b84-5069cccae767")), - Permission("Products", uuid.UUID("f1f892ed-34aa-4c50-be12-dbaf48be7d4c")), - Permission("Roles", uuid.UUID("d39f86f6-14e4-4fc3-b07e-01e2501a6f65")), - Permission("Sales", uuid.UUID("92ba45c4-2d34-4cbd-b5b8-d9164c3595d5")), - Permission("Sales Analysis", uuid.UUID("0c1acb7c-9116-46e2-a3be-9d632e811330")), - Permission("Sales Detail", uuid.UUID("e6476396-ef83-4c47-bc20-9f3b936813f9")), - Permission("Settle Bill", uuid.UUID("9f7b658a-25bc-4a53-8560-2dde1c7a188a")), - Permission("Split Bill", uuid.UUID("0a8cfaf4-624e-478f-be65-c3b6392078c7")), - Permission("Tables", uuid.UUID("ac4512e8-efdd-429e-a7ae-a34d18782663")), - Permission("Tax Analysis", uuid.UUID("bab36396-53dd-44ea-ab3c-426efa7830c8")), - Permission("Taxes", uuid.UUID("5c8fcdde-460d-4047-810f-e34fb899fadc")), - Permission("Users", uuid.UUID("243447b8-b403-47e6-8b3d-8e76f4df44a9")), - Permission("Void Bill", uuid.UUID("e3c76262-adc0-4936-8b4d-217c6292298b")), - Permission( - "Void or Reprinted Bill Report", - uuid.UUID("30c8e743-c710-42d7-843a-0b75543b3516"), - ), - Permission("Printers", uuid.UUID("5b66c6f6-003a-4ef8-ba28-49b8ff1ac33c")), - Permission("Sections", uuid.UUID("c973f32c-a37b-496a-8dc5-60d2e4c39e97")), - ] - - for permission in permissions: - db.add(permission) - - roles = [ - Role("Owner", uuid.UUID("06ec0ddb-949c-4357-aefb-65e5e55a9ae7")), - Role("Accountant", uuid.UUID("57d4538c-257a-4bf4-9a42-342992cb6af3")), - Role("Bar Captain", uuid.UUID("5a8ebb05-4817-45a5-866f-e523c30bfa25")), - Role("Captain", uuid.UUID("8db7001f-0964-4607-84f5-b6f4d9183fef")), - Role("Cashier", uuid.UUID("f1f0b112-1404-4b55-b121-f07ee6e08d12")), - Role("Controller", uuid.UUID("d9a5a478-1fe4-4847-84da-63cfba1a094a")), - Role("Manager", uuid.UUID("90ea7b14-9057-4bfd-a7cf-0ee7a3bb2463")), - Role("Senior Captain", uuid.UUID("d9b1b433-1ed5-4109-8ab2-cbd48ff010cd")), - Role("Server", uuid.UUID("6b378b71-f091-4485-a589-8db94ff1d6a4")), - ] - - roles[0].permissions = permissions - roles[1].permissions = list(permissions[i] for i in [2, 3, 11, 18, 24, 25, 26, 30, 34]) - roles[2].permissions = list(permissions[i] for i in [26]) - roles[3].permissions = list(permissions[i] for i in [16]) - roles[4].permissions = list(permissions[i] for i in [20, 24, 27]) - roles[5].permissions = list(permissions[i] for i in [0, 2, 7, 11, 18, 22, 24, 25, 26, 34]) - roles[6].permissions = list(permissions[i] for i in [2, 3, 4, 5, 6, 8, 11, 14, 18, 22, 25, 26, 28, 29, 33, 34]) - roles[7].permissions = list(permissions[i] for i in [9, 12, 13, 15, 16]) - roles[8].permissions = list(permissions[i] for i in [21, 24]) - - for role in roles: - db.add(role) - - admin = User("Admin", "123456", False) - admin.roles.append(roles[0]) - db.add(admin) diff --git a/barker/barker/models/inventory.py b/barker/barker/models/inventory.py index 2a60fc8..2bbfe60 100644 --- a/barker/barker/models/inventory.py +++ b/barker/barker/models/inventory.py @@ -42,12 +42,12 @@ class Inventory: product_id: Mapped[uuid.UUID] = mapped_column( "product_id", UUID(as_uuid=True), ForeignKey("products.id"), nullable=False ) - quantity: Mapped[Decimal] = mapped_column("quantity", Numeric(precision=15, scale=2)) - price: Mapped[Decimal] = mapped_column("price", Numeric(precision=15, scale=2)) + quantity: Mapped[Decimal] = mapped_column("quantity", Numeric(precision=15, scale=2), nullable=False) + price: Mapped[Decimal] = mapped_column("price", Numeric(precision=15, scale=2), nullable=False) is_happy_hour: Mapped[bool] = mapped_column("is_happy_hour", Boolean, nullable=False) - tax_rate: Mapped[Decimal] = mapped_column("tax_rate", Numeric(precision=15, scale=5)) + tax_rate: Mapped[Decimal] = mapped_column("tax_rate", Numeric(precision=15, scale=5), nullable=False) tax_id: Mapped[uuid.UUID] = mapped_column("tax_id", UUID(as_uuid=True), ForeignKey("taxes.id"), nullable=False) - discount: Mapped[Decimal] = mapped_column("discount", Numeric(precision=15, scale=5)) + discount: Mapped[Decimal] = mapped_column("discount", Numeric(precision=15, scale=5), nullable=False) sort_order: Mapped[int] = mapped_column("sort_order", Integer, nullable=False) kot: Mapped["Kot"] = relationship("Kot", back_populates="inventories") diff --git a/barker/barker/models/overview.py b/barker/barker/models/overview.py index 74bcfb8..16c9143 100644 --- a/barker/barker/models/overview.py +++ b/barker/barker/models/overview.py @@ -36,7 +36,7 @@ class Overview: unique=True, ) guest_book_id: Mapped[uuid.UUID] = mapped_column( - "guest_book_id", UUID(as_uuid=True), ForeignKey("guest_book.id"), unique=True + "guest_book_id", UUID(as_uuid=True), ForeignKey("guest_book.id"), unique=True, nullable=True ) status: Mapped[str] = mapped_column("status", Unicode(255), nullable=False) diff --git a/barker/barker/models/permission.py b/barker/barker/models/permission.py index c676cfd..6fac02f 100644 --- a/barker/barker/models/permission.py +++ b/barker/barker/models/permission.py @@ -21,7 +21,7 @@ class Permission: id: Mapped[uuid.UUID] = mapped_column( "id", UUID(as_uuid=True), primary_key=True, server_default=text("gen_random_uuid()"), insert_default=uuid.uuid4 ) - name: Mapped[str] = mapped_column("name", Unicode(255), unique=True) + name: Mapped[str] = mapped_column("name", Unicode(255), unique=True, nullable=False) roles: Mapped[List["Role"]] = relationship("Role", secondary=RolePermission.__table__, back_populates="permissions") diff --git a/barker/barker/models/role.py b/barker/barker/models/role.py index 923d85d..2cd7ccb 100644 --- a/barker/barker/models/role.py +++ b/barker/barker/models/role.py @@ -21,7 +21,7 @@ class Role: id: Mapped[uuid.UUID] = mapped_column( "id", UUID(as_uuid=True), primary_key=True, server_default=text("gen_random_uuid()"), insert_default=uuid.uuid4 ) - name: Mapped[str] = mapped_column("name", Unicode(255), unique=True) + name: Mapped[str] = mapped_column("name", Unicode(255), unique=True, nullable=False) permissions: Mapped[List["Permission"]] = relationship( "Permission", secondary=RolePermission.__table__, back_populates="roles" diff --git a/barker/barker/models/section_printer.py b/barker/barker/models/section_printer.py index 5f8d438..4250d9a 100644 --- a/barker/barker/models/section_printer.py +++ b/barker/barker/models/section_printer.py @@ -24,7 +24,7 @@ class SectionPrinter: "id", UUID(as_uuid=True), primary_key=True, server_default=text("gen_random_uuid()"), insert_default=uuid.uuid4 ) sale_category_id: Mapped[uuid.UUID] = mapped_column( - "sale_category_id", UUID(as_uuid=True), ForeignKey("sale_categories.id") + "sale_category_id", UUID(as_uuid=True), ForeignKey("sale_categories.id"), nullable=True ) section_id: Mapped[uuid.UUID] = mapped_column( "section_id", UUID(as_uuid=True), ForeignKey("sections.id"), nullable=False diff --git a/barker/barker/models/voucher.py b/barker/barker/models/voucher.py index a0561d6..849d694 100644 --- a/barker/barker/models/voucher.py +++ b/barker/barker/models/voucher.py @@ -49,9 +49,11 @@ class Voucher: ForeignKey("food_tables.id"), nullable=False, ) - customer_id: Mapped[uuid.UUID] = mapped_column("customer_id", UUID(as_uuid=True), ForeignKey("customers.id")) + customer_id: Mapped[uuid.UUID] = mapped_column( + "customer_id", UUID(as_uuid=True), ForeignKey("customers.id"), nullable=True + ) narration: Mapped[str] = mapped_column("narration", Unicode(1000), nullable=True) - reason: Mapped[str] = mapped_column("reason", Unicode(255)) + reason: Mapped[str] = mapped_column("reason", Unicode(255), nullable=True) voucher_type: Mapped[VoucherType] = mapped_column("voucher_type", Enum(VoucherType), nullable=False) user_id: Mapped[uuid.UUID] = mapped_column("user_id", UUID(as_uuid=True), ForeignKey("users.id"), nullable=False)