-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdb_setup.py
180 lines (157 loc) · 5.47 KB
/
db_setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env python3
import os
import csv
import sqlite3
import petl as etl
from project.app import app, db
from project.app import Product, Supplier, Tag, Staff
#db_path = r"C:\GitHub\fc-inventory\project\db.sqlite"
app_dir = os.path.realpath(os.path.dirname(__file__))
db_path = os.path.join(app_dir, 'project', app.config['DATABASE_FILE'])
#----------------------------------------------------------------------------#
# ETL
#----------------------------------------------------------------------------#
src_suppliers = etl.fromcsv("sources/Suppliers.csv")
src_products = etl.fromcsv("sources/Products.csv")
src_tags = etl.fromcsv("sources/Categories.csv")
src_staff = etl.fromcsv("sources/Staff.csv")
def create_suppliers():
for row in etl.dicts(src_suppliers):
new_supplier = Supplier(
id=row["ID"],
name=row["Company"]
)
db.session.add(new_supplier)
db.session.commit()
def create_products():
for row in etl.dicts(src_products):
new_product = Product(
code=row["ProductCode"],
name=row["ProductName"],
list_price=row["StandardCost"],
selling_price=row["ListPrice"],
quantity_per_unit=row["QuantityPerUnit"],
description=row["Description"],
supplier_id=row["SupplierID"],
discontinued=row["Discontinued"],
)
db.session.add(new_product)
db.session.commit()
def create_tags():
for row in etl.dicts(src_tags):
new_row = Tag(
id=row["ID"],
name=row["Category"]
)
db.session.add(new_row)
db.session.commit()
def create_staff():
for row in etl.dicts(src_staff):
new_row = Staff(
id=row["ID"],
name=row["FullName"]
)
db.session.add(new_row)
db.session.commit()
#----------------------------------------------------------------------------#
# DB CONFIG
#----------------------------------------------------------------------------#
def set_trigger_fullname(db_path):
q1 = """
CREATE TRIGGER product_update_fullname
AFTER UPDATE ON product
FOR EACH ROW
BEGIN
UPDATE product
SET fullname = (SELECT supplier.name FROM supplier WHERE supplier.id = product.supplier_id) || ' | ' || name || ' | $' || list_price || ' list / $' || selling_price || ' selling price'
WHERE id = new.id;
END;
"""
q2 = """
CREATE TRIGGER product_insert_fullname
AFTER INSERT ON product
FOR EACH ROW
BEGIN
UPDATE product
SET fullname = (SELECT supplier.name FROM supplier WHERE supplier.id = product.supplier_id) || ' | ' || name || ' | $' || list_price || ' list / $' || selling_price || ' selling price'
WHERE id = new.id;
END;
"""
q3 = """
CREATE TRIGGER update_supplier_update_product_fullname
AFTER UPDATE ON supplier
FOR EACH ROW
BEGIN
UPDATE product
SET fullname = (SELECT supplier.name FROM supplier WHERE supplier.id = product.supplier_id) || ' | ' || product.name || ' | $' || product.list_price || ' list / $' || product.selling_price || ' selling price';
END;
"""
q4 = """UPDATE product SET fullname = (SELECT supplier.name FROM supplier WHERE supplier.id = product.supplier_id) || ' | ' || name || ' | $' || list_price || ' list / $' || selling_price || ' selling price';"""
conn = sqlite3.connect(db_path)
c = conn.cursor()
for each in [q1, q2, q3, q4]:
c.execute(each)
conn.commit()
conn.close()
def set_trigger_selling_price(db_path):
q1 = """
CREATE TRIGGER record_selling_price_at_time_of_sale
AFTER INSERT ON sale
FOR EACH ROW
BEGIN
UPDATE sale
SET sold_price = (
CASE
WHEN (sale.special_price IS NOT NULL AND sale.special_price > 0)
THEN sale.special_price
WHEN (sale.use_list_price = 1)
THEN (SELECT product.list_price FROM product WHERE product.id = sale.product_id)
ELSE
(SELECT product.selling_price FROM product WHERE product.id = sale.product_id)
END
)
WHERE id = new.id;
END;
"""
q2 = """
CREATE TRIGGER update_selling_price_if_special_price_changes
AFTER UPDATE ON sale
FOR EACH ROW
BEGIN
UPDATE sale
SET sold_price = (
CASE
WHEN (sale.special_price IS NOT NULL AND sale.special_price > 0)
THEN sale.special_price
WHEN (sale.use_list_price = 1)
THEN (SELECT product.list_price FROM product WHERE product.id = sale.product_id)
ELSE
(SELECT product.selling_price FROM product WHERE product.id = sale.product_id)
END
)
WHERE id = new.id;
END;
"""
conn = sqlite3.connect(db_path)
c = conn.cursor()
for each in [q1]:
c.execute(each)
conn.commit()
conn.close()
#----------------------------------------------------------------------------#
# CREATE SOME DATA
#----------------------------------------------------------------------------#
def build_db():
# build tables from source data
print(app.config)
create_suppliers()
create_tags()
create_products()
create_staff()
# set triggers
set_trigger_fullname(db_path)
set_trigger_selling_price(db_path)
if __name__ == '__main__':
db.drop_all()
db.create_all()
build_db()