-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathorm.py
80 lines (66 loc) · 2.03 KB
/
orm.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
import logging
from sqlalchemy import Column, Date, ForeignKey, Integer, String, Table, event
from sqlalchemy.orm import registry, relationship
from allocation.domain import model
logger = logging.getLogger(__name__)
mapper_registry = registry()
metadata = mapper_registry.metadata
order_lines = Table(
"order_lines",
metadata,
Column("id", Integer, primary_key=True, autoincrement=True),
Column("sku", String(255)),
Column("qty", Integer, nullable=False),
Column("orderid", String(255)),
)
products = Table(
"products",
metadata,
Column("sku", String(255), primary_key=True),
Column("version_number", Integer, nullable=False, server_default="0"),
)
batches = Table(
"batches",
metadata,
Column("id", Integer, primary_key=True, autoincrement=True),
Column("reference", String(255)),
Column("sku", ForeignKey("products.sku")),
Column("_purchased_quantity", Integer, nullable=False),
Column("eta", Date, nullable=True),
)
allocations = Table(
"allocations",
metadata,
Column("id", Integer, primary_key=True, autoincrement=True),
Column("orderline_id", ForeignKey("order_lines.id")),
Column("batch_id", ForeignKey("batches.id")),
)
allocations_view = Table(
"allocations_view",
metadata,
Column("orderid", String(255)),
Column("sku", String(255)),
Column("batchref", String(255)),
)
def start_mappers():
logger.info("Starting mappers")
lines_mapper = mapper_registry.map_imperatively(model.OrderLine, order_lines)
batches_mapper = mapper_registry.map_imperatively(
model.Batch,
batches,
properties={
"_allocations": relationship(
lines_mapper,
secondary=allocations,
collection_class=set,
)
},
)
mapper_registry.map_imperatively(
model.Product,
products,
properties={"batches": relationship(batches_mapper)},
)
@event.listens_for(model.Product, "load")
def receive_load(product, _):
product.events = []