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.
This commit is contained in:
robertem 2017-02-04 21:35:29 +00:00
parent 8954ab4304
commit dd3967e67d
3 changed files with 58 additions and 10 deletions

11
db.py
View File

@ -42,7 +42,8 @@ class User(Base):
class Product(Base): class Product(Base):
__tablename__ = 'products' __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)) name = Column(String(45))
price = Column(Integer) price = Column(Integer)
stock = Column(Integer) stock = Column(Integer)
@ -67,7 +68,7 @@ class PurchaseEntry(Base):
__tablename__ = 'purchase_entries' __tablename__ = 'purchase_entries'
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True)
purchase_id = Column(Integer,ForeignKey("purchases.id")) 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) amount = Column(Integer)
product = relationship(Product,backref="purchases") product = relationship(Product,backref="purchases")
@ -90,16 +91,16 @@ class Transaction(Base):
amount = Column(Integer) amount = Column(Integer)
description = Column(String(50)) description = Column(String(50))
purchase_id = Column(Integer, ForeignKey('purchases.id')) purchase_id = Column(Integer, ForeignKey('purchases.id'))
#penalty = Column(Integer) penalty = Column(Integer)
user = relationship(User, backref=backref('transactions', order_by=time)) 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.user = user
self.amount = amount self.amount = amount
self.description = description self.description = description
self.purchase = purchase self.purchase = purchase
self.penalty = penalty_ratio self.penalty = penalty
def perform_transaction(self): def perform_transaction(self):
self.time = datetime.datetime.now() self.time = datetime.datetime.now()

47
migration-2017-02-04.sql Normal file
View File

@ -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;

View File

@ -787,8 +787,8 @@ class ShowUserMenu(Menu):
string += ', '.join(map(lambda e: e.product.name, string += ', '.join(map(lambda e: e.product.name,
t.purchase.entries)) t.purchase.entries))
string += ')' string += ')'
#if t.penalty > 1: if t.penalty > 1:
# string += ' * %dx penalty applied' % t.penalty string += ' * %dx penalty applied' % t.penalty
else: else:
string += t.description string += t.description
string += '\n' string += '\n'
@ -810,8 +810,8 @@ class ShowUserMenu(Menu):
string += ', '.join(map(lambda e: e.product.name, string += ', '.join(map(lambda e: e.product.name,
t.purchase.entries)) t.purchase.entries))
string += ')' string += ')'
#if t.penalty > 1: if t.penalty > 1:
# string += ' * %dx penalty applied' % t.penalty string += ' * %dx penalty applied' % t.penalty
else: else:
string += t.description string += t.description
string += '\n' string += '\n'
@ -927,7 +927,7 @@ When finished, write an empty line to confirm the purchase.
if not self.credit_check(thing): if not self.credit_check(thing):
if self.low_credit_warning(user=thing, timeout=self.superfast_mode): 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: else:
return False return False
else: else: