-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdatabase.py
219 lines (179 loc) · 6.82 KB
/
database.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
from typing import List
from sqlalchemy.engine.base import Engine
from sqlalchemy.orm import Session
from model import Application, ApplicationVersion, Interaction
class Database:
"""
The class Database contains a series of methods for interacting with a database.
"""
def __init__(self, engine: Engine):
"""
Args:
engine (Engine): sqlalchemy engine.
"""
self.engine = engine
def list_applications(self) -> List[Application]:
"""
Retrieve a list of non-deleted applications from the database.
Returns:
List[Application]: A list of Application objects.
"""
with Session(self.engine) as session:
return (
session.query(Application)
.filter(Application.deleted_at == None)
.order_by(Application.created_at.desc())
.all()
)
def create_application(self, app: Application):
"""
Adds a new application to the database.
Args:
app (Application): The application object to be added.
Returns:
None
"""
with Session(self.engine) as session:
session.add(app)
session.commit()
def get_application(self, app_id: str) -> Application:
"""
Retrieve an application by its ID.
Args:
app_id (str): The ID of the application to retrieve.
Returns:
Application: The application object corresponding to the given ID, or None if not found.
"""
with Session(self.engine) as session:
return session.query(Application).filter(Application.id == app_id).first()
def get_interaction(self, interaction_id: str) -> Interaction:
"""
Retrieve an interaction from the database by its ID.
Args:
interaction_id (str): The ID of the interaction to retrieve.
Returns:
Interaction: The retrieved interaction object.
"""
with Session(self.engine) as session:
return (
session.query(Interaction)
.filter(Interaction.id == interaction_id)
.first()
)
def create_interaction(self, interaction: Interaction):
"""
Add a new interaction to the database.
Args:
interaction (Interaction): An instance of the Interaction class
representing the interaction data to be added.
Returns:
None
"""
with Session(self.engine) as session:
session.add(interaction)
session.commit()
def update_interaction(self, interaction_id: str, attrs: dict):
"""
Update an Interaction record in the database with the specified ID.
Args:
interaction_id (str): The ID of the Interaction record to update.
attrs (dict): A dictionary containing the attributes to update.
The keys should correspond to the column names of the interactions
table in the database, and the values should be the new values for
those attributes.
Returns:
None
Example usage:
```
attrs = {
'data': {...},
'output': 'output value'
}
database.update_interaction('interaction_id_here', attrs)
```
"""
with Session(self.engine) as session:
session.query(Interaction).filter(Interaction.id == interaction_id).update(
attrs
)
session.commit()
def update_application(self, app_id: str, attrs: dict):
"""
Update an application in the database with the specified attributes.
Args:
app_id (str): The ID of the application to update.
attrs (dict): A dictionary containing the attributes to update.
Returns:
None
"""
with Session(self.engine) as session:
session.query(Application).filter(Application.id == app_id).update(attrs)
session.commit()
def update_version(self, version_id: str, attrs: dict):
"""
Update an version in the database with the specified attributes.
Args:
version_id (str): The ID of the version to update.
attrs (dict): A dictionary containing the attributes to update.
Returns:
None
"""
with Session(self.engine) as session:
session.query(ApplicationVersion).filter(
ApplicationVersion.id == version_id
).update(attrs)
session.commit()
def list_versions(self, app_id: str) -> List[ApplicationVersion]:
"""
Retrieve all non-deleted versions of an application by app_id.
Args:
app_id (str): The ID of the application to retrieve versions for.
Returns:
List[ApplicationVersion]: A list of ApplicationVersion objects
representing the non-deleted versions.
"""
with Session(self.engine) as session:
return (
session.query(ApplicationVersion)
.filter(ApplicationVersion.app_id == app_id)
.filter(ApplicationVersion.deleted_at == None)
.order_by(ApplicationVersion.created_at.desc())
.all()
)
def get_version(self, version_id: str) -> ApplicationVersion:
"""
Retrieve an application version by its ID.
Args:
version_id (str): The ID of the application version to retrieve.
Returns:
ApplicationVersion: The application version object corresponding to the given ID.
"""
with Session(self.engine) as session:
return (
session.query(ApplicationVersion)
.filter(ApplicationVersion.id == version_id)
.first()
)
def create_version(self, version: ApplicationVersion):
"""
Create a new application version in the database.
Args:
version (ApplicationVersion): The application version object to be added.
Returns:
None
"""
with Session(self.engine) as session:
session.add(version)
session.commit()
def update_version(self, version_id: str, attrs: dict):
"""
Update the attributes of an application version in the database.
Args:
version_id (str): The ID of the version to update.
attrs (dict): A dictionary containing the attributes to update and their new values.
"""
with Session(self.engine) as session:
session.query(ApplicationVersion).filter(
ApplicationVersion.id == version_id
).update(attrs)
session.commit()