-
-
Notifications
You must be signed in to change notification settings - Fork 170
/
data.py
118 lines (108 loc) · 3.15 KB
/
data.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
import click
from schema import Answer, Category, PossibleAnswer, Question, RealAnswer
from sqlalchemy.orm import sessionmaker
from pgsync.base import pg_engine, subtransactions
from pgsync.helper import teardown
from pgsync.utils import config_loader, get_config
@click.command()
@click.option(
"--config",
"-c",
help="Schema config",
type=click.Path(exists=True),
)
def main(config):
config: str = get_config(config)
teardown(drop_db=False, config=config)
doc = next(config_loader(config))
database: str = doc.get("database", doc["index"])
with pg_engine(database) as engine:
Session = sessionmaker(bind=engine, autoflush=True)
session = Session()
# Bootstrap
categories = [
Category(
id=1,
uid="c001",
text="Colours",
),
Category(
id=2,
uid="c002",
text="Weather",
),
]
with subtransactions(session):
session.add_all(categories)
questions = [
Question(
id=1,
uid="q001",
category_id=1,
category_uid="c001",
text="What is your favorite color?",
),
Question(
id=2,
uid="q002",
category_id=2,
category_uid="c002",
text="Is it raining outside?",
),
]
with subtransactions(session):
session.add_all(questions)
answers = [
Answer(id=1, uid="a001", text="Red"),
Answer(id=2, uid="a002", text="Yes"),
Answer(id=3, uid="a003", text="Green"),
Answer(id=4, uid="a004", text="No"),
]
with subtransactions(session):
session.add_all(answers)
possible_answers = [
PossibleAnswer(
question_id=1,
question_uid="q001",
answer_id=1,
answer_uid="a001",
),
PossibleAnswer(
question_id=1,
question_uid="q001",
answer_id=3,
answer_uid="a003",
),
PossibleAnswer(
question_id=2,
question_uid="q002",
answer_id=2,
answer_uid="a002",
),
PossibleAnswer(
question_id=2,
question_uid="q002",
answer_id=4,
answer_uid="a004",
),
]
with subtransactions(session):
session.add_all(possible_answers)
real_answers = [
RealAnswer(
question_id=1,
question_uid="q001",
answer_id=1,
answer_uid="a001",
),
RealAnswer(
question_id=2,
question_uid="q002",
answer_id=4,
answer_uid="a004",
),
]
with subtransactions(session):
session.add_all(real_answers)
if __name__ == "__main__":
main()