From dd3967e67dc67f68360702091dd284279c635587 Mon Sep 17 00:00:00 2001 From: robertem <3456661+RandomGamer342@users.noreply.github.com> Date: Sat, 4 Feb 2017 21:35:29 +0000 Subject: [PATCH] Database format update. Product's primary key is no longer the barcode, but a new sequential id. (products.product_id added as new column. products.bar_code no longer primary key.) Necessary foreign keys have been updated. Transactions have a new penalty column, and penalties applied are logged here. These are shown in the user transaction list as well. --- db.py | 11 +++++----- migration-2017-02-04.sql | 47 ++++++++++++++++++++++++++++++++++++++++ text_based.py | 10 ++++----- 3 files changed, 58 insertions(+), 10 deletions(-) create mode 100644 migration-2017-02-04.sql diff --git a/db.py b/db.py index fb38d43..6d6b8db 100644 --- a/db.py +++ b/db.py @@ -42,7 +42,8 @@ class User(Base): class Product(Base): __tablename__ = 'products' - bar_code = Column(String(13), primary_key=True) + product_id = Column(Integer, primary_key=True) + bar_code = Column(String(13)) name = Column(String(45)) price = Column(Integer) stock = Column(Integer) @@ -67,7 +68,7 @@ class PurchaseEntry(Base): __tablename__ = 'purchase_entries' id = Column(Integer, primary_key=True) purchase_id = Column(Integer,ForeignKey("purchases.id")) - product_bar_code = Column(String(13),ForeignKey("products.bar_code")) + product_id = Column(Integer,ForeignKey("products.product_id")) amount = Column(Integer) product = relationship(Product,backref="purchases") @@ -90,16 +91,16 @@ class Transaction(Base): amount = Column(Integer) description = Column(String(50)) purchase_id = Column(Integer, ForeignKey('purchases.id')) - #penalty = Column(Integer) + penalty = Column(Integer) user = relationship(User, backref=backref('transactions', order_by=time)) - def __init__(self, user, amount=0, description=None, purchase=None, penalty_ratio=1): + def __init__(self, user, amount=0, description=None, purchase=None, penalty=1): self.user = user self.amount = amount self.description = description self.purchase = purchase - self.penalty = penalty_ratio + self.penalty = penalty def perform_transaction(self): self.time = datetime.datetime.now() diff --git a/migration-2017-02-04.sql b/migration-2017-02-04.sql new file mode 100644 index 0000000..0f0cf1e --- /dev/null +++ b/migration-2017-02-04.sql @@ -0,0 +1,47 @@ +ALTER TABLE pvv_vv.products RENAME TO products_old; +CREATE TABLE pvv_vv.products +( + product_id serial, + bar_code character varying(13) NOT NULL, + name character varying(45), + price integer, + stock integer NOT NULL, + CONSTRAINT product_pkey PRIMARY KEY (product_id), + CONSTRAINT barcode_unique UNIQUE (bar_code) +) + +INSERT INTO pvv_vv.products (bar_code, name, price, stock) + SELECT bar_code, name, price, stock FROM products_old; + +ALTER TABLE pvv_vv.purchase_entries RENAME TO purchase_entries_old; +ALTER TABLE pvv_vv.purchase_entries_old + RENAME CONSTRAINT purchase_entries_pkey TO purchase_entries_old_pkey; +ALTER TABLE pvv_vv.purchase_entries_old + RENAME CONSTRAINT purchase_entries_purchase_id_fkey TO purchase_entries_old_purchase_id_fkey; +ALTER TABLE pvv_vv.purchase_entries_old + RENAME CONSTRAINT purchase_entries_product_bar_code_fkey TO purchase_entries_old_product_bar_code_fkey; + +CREATE TABLE pvv_vv.purchase_entries +( + id serial, + purchase_id integer, + product_id integer, + amount integer, + CONSTRAINT purchase_entries_pkey PRIMARY KEY (id), + CONSTRAINT purchase_entries_product_id_fkey FOREIGN KEY (product_id) + REFERENCES pvv_vv.products (product_id) MATCH SIMPLE + ON UPDATE NO ACTION + ON DELETE NO ACTION, + CONSTRAINT purchase_entries_purchase_id_fkey FOREIGN KEY (purchase_id) + REFERENCES pvv_vv.purchases (id) MATCH SIMPLE + ON UPDATE NO ACTION + ON DELETE NO ACTION +); +INSERT INTO purchase_entries (id, purchase_id, product_id, amount) + SELECT peo.id, peo.purchase_id, p.product_id, peo.amount + FROM purchase_entries_old AS peo + JOIN products AS p ON p.bar_code = peo.product_bar_code; +ALTER TABLE pvv_vv.transactions + ADD COLUMN penalty integer DEFAULT 1; +DROP TABLE products_old; +DROP TABLE purchase_entries_old; diff --git a/text_based.py b/text_based.py index 32c23e9..b0fd542 100755 --- a/text_based.py +++ b/text_based.py @@ -787,8 +787,8 @@ class ShowUserMenu(Menu): string += ', '.join(map(lambda e: e.product.name, t.purchase.entries)) string += ')' - #if t.penalty > 1: - # string += ' * %dx penalty applied' % t.penalty + if t.penalty > 1: + string += ' * %dx penalty applied' % t.penalty else: string += t.description string += '\n' @@ -810,8 +810,8 @@ class ShowUserMenu(Menu): string += ', '.join(map(lambda e: e.product.name, t.purchase.entries)) string += ')' - #if t.penalty > 1: - # string += ' * %dx penalty applied' % t.penalty + if t.penalty > 1: + string += ' * %dx penalty applied' % t.penalty else: string += t.description string += '\n' @@ -927,7 +927,7 @@ When finished, write an empty line to confirm the purchase. if not self.credit_check(thing): if self.low_credit_warning(user=thing, timeout=self.superfast_mode): - Transaction(thing, purchase=self.purchase, penalty_ratio=2) + Transaction(thing, purchase=self.purchase, penalty=2) else: return False else: