-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdelete.py
365 lines (311 loc) · 11.6 KB
/
delete.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
"""Functions to delete data from the database."""
from psycopg import sql
from zeno_backend.classes.user import Author, Organization, User
from zeno_backend.database.database import db_pool
async def project(project: str):
"""Deletes a project with a specific id.
Args:
project (str): the id of the project to be deleted.
Raises:
Exception: something went wrong while deleting the project from the database.
"""
async with db_pool.connection() as conn:
async with conn.cursor() as cur:
# Drop the primary table with project data.
await cur.execute(
sql.SQL("DROP TABLE IF EXISTS {} CASCADE;").format(
sql.Identifier(project)
)
)
# Drop the table with column properties.
await cur.execute(
sql.SQL("DROP TABLE IF EXISTS {} CASCADE;").format(
sql.Identifier(f"{project}_column_map")
)
)
# Drop the table with tag data.
await cur.execute(
sql.SQL("DROP TABLE IF EXISTS {} CASCADE;").format(
sql.Identifier(f"{project}_tags_datapoints")
)
)
# Delete the project from the projects table.
await cur.execute(
"DELETE FROM projects WHERE uuid = %s;",
[
project,
],
)
await conn.commit()
async def report(report_id: int):
"""Deletes a report from Zeno.
Args:
report_id (int): the id of the report to be deleted.
"""
async with db_pool.connection() as conn:
async with conn.cursor() as cur:
await cur.execute(
"DELETE FROM reports WHERE id = %s;",
[report_id],
)
await conn.commit()
async def folder(folder_id: int, delete_slices: bool):
"""Deletes a folder from an existing project.
Args:
folder_id (int): the id of the folder to be deleted.
delete_slices (bool): whether to delete the slices in the folder as well.
"""
async with db_pool.connection() as conn:
async with conn.cursor() as cur:
if delete_slices:
await cur.execute(
"DELETE FROM slices WHERE folder_id = %s;",
[
folder_id,
],
)
await cur.execute(
"DELETE FROM folders WHERE id = %s;",
[
folder_id,
],
)
await conn.commit()
async def slice(slice_id: int):
"""Deletes a slice from an existing project.
Args:
slice_id (int): the id of the slice to be deleted.
"""
async with db_pool.connection() as conn:
async with conn.cursor() as cur:
await cur.execute(
"DELETE FROM slices WHERE id = %s;",
[slice_id],
)
await conn.commit()
async def chart(chart_id: int):
"""Deletes a chart from an existing project.
Args:
chart_id (int): the id of the chart to be deleted.
"""
async with db_pool.connection() as conn:
async with conn.cursor() as cur:
await cur.execute(
"DELETE FROM charts WHERE id = %s;",
[
chart_id,
],
)
await conn.commit()
async def tag(tag_id: int):
"""Deletes a tag from an existing project.
Args:
tag_id (int): the id of the tag to be deleted.
"""
async with db_pool.connection() as conn:
async with conn.cursor() as cur:
await cur.execute(
"DELETE FROM tags WHERE id = %s;",
[
tag_id,
],
)
await conn.commit()
async def organization(organization: Organization):
"""Deletes an organization from the database.
Args:
organization (Organization): the organization to delete.
"""
async with db_pool.connection() as conn:
async with conn.cursor() as cur:
await cur.execute(
"DELETE FROM organizations WHERE id = %s;",
[
organization.id,
],
)
await conn.commit()
async def project_user(project: str, user: User):
"""Remove a user from a project.
Args:
project (str): the project id from which to remove the user.
user (User): the user to remove.
"""
async with db_pool.connection() as conn:
async with conn.cursor() as cur:
await cur.execute(
"DELETE FROM user_project WHERE user_id = %s AND project_uuid = %s;",
[user.id, project],
)
await conn.commit()
async def project_org(project: str, organization: Organization):
"""Remove an organization from a project.
Args:
project (str): the project id from which to remove the organization.
organization (Organization): the organization to remove.
"""
async with db_pool.connection() as conn:
async with conn.cursor() as cur:
await cur.execute(
"DELETE FROM organization_project WHERE organization_id = %s "
"AND project_uuid = %s;",
[organization.id, project],
)
await conn.commit()
async def report_element(id: int):
"""Delete an element of a report.
Args:
id (int): ID of the element to be deleted.
"""
async with db_pool.connection() as conn:
async with conn.cursor() as cur:
await cur.execute(
"DELETE FROM report_elements WHERE id = %s;",
[id],
)
await conn.commit()
async def report_author(report_id: int, author: Author):
"""Remove an author from a report.
Args:
report_id (int): the report id from which to remove the author.
author (Author): the author to remove.
"""
async with db_pool.connection() as conn:
async with conn.cursor() as cur:
await cur.execute(
"DELETE FROM report_author WHERE user_id = %s AND report_id = %s;",
[author.user.id, report_id],
)
await conn.commit()
async def report_user(report_id: int, user: User):
"""Remove a user from a report.
Args:
report_id (int): the report id from which to remove the user.
user (User): the user to remove.
"""
async with db_pool.connection() as conn:
async with conn.cursor() as cur:
await cur.execute(
"DELETE FROM user_report WHERE user_id = %s AND report_id = %s;",
[user.id, report_id],
)
await conn.commit()
async def report_org(report_id: int, organization: Organization):
"""Remove an organization from a report.
Args:
report_id (int): the report id from which to remove the organization.
organization (Organization): the organization to remove.
"""
async with db_pool.connection() as conn:
async with conn.cursor() as cur:
await cur.execute(
"DELETE FROM organization_report WHERE organization_id = %s "
"AND report_id = %s;",
[organization.id, report_id],
)
await conn.commit()
async def dataset(project_uuid: str):
"""Delete dataset table and clear column_map for a project.
Args:
project_uuid (str): id of the project to delete a dataset from.
"""
async with db_pool.connection() as conn:
async with conn.cursor() as cur:
await cur.execute(
sql.SQL("DELETE FROM {};").format(
sql.Identifier(f"{project_uuid}_column_map")
)
)
await cur.execute(
sql.SQL("DROP TABLE IF EXISTS {} CASCADE;").format(
sql.Identifier(project_uuid)
)
)
await cur.execute(
sql.SQL("DROP TABLE IF EXISTS {} CASCADE;").format(
sql.Identifier(f"{project_uuid}_tags_datapoints")
)
)
await cur.execute(
"UPDATE charts SET data = NULL WHERE project_uuid = %s;", [project_uuid]
)
await conn.commit()
async def system(project_uuid: str, system_name: str):
"""Delete a system from a project.
Args:
project_uuid (str): id of the project to delete a system from.
system_name (str): name of the system to be deleted.
"""
async with db_pool.connection() as conn:
async with conn.cursor() as cur:
await cur.execute(
sql.SQL("SELECT column_id FROM {} WHERE model = %s;").format(
sql.Identifier(f"{project_uuid}_column_map")
),
[system_name],
)
columns = await cur.fetchall()
for column in columns:
await cur.execute(
sql.SQL("ALTER TABLE {} DROP COLUMN IF EXISTS {};").format(
sql.Identifier(project_uuid),
sql.Identifier(column[0]),
)
)
await cur.execute(
sql.SQL("DELETE FROM {} WHERE column_id = %s;").format(
sql.Identifier(f"{project_uuid}_column_map")
),
[column[0]],
)
await cur.execute(
"UPDATE charts SET data = NULL WHERE project_uuid = %s;", [project_uuid]
)
await conn.commit()
async def systems(project_uuid: str):
"""Delete all systems from a project.
Args:
project_uuid (str): id of the project to delete all systems from.
"""
async with db_pool.connection() as conn:
async with conn.cursor() as cur:
await cur.execute(
sql.SQL("SELECT column_id FROM {} WHERE model IS NOT NULL;").format(
sql.Identifier(f"{project_uuid}_column_map")
)
)
columns = await cur.fetchall()
for column in columns:
await cur.execute(
sql.SQL("ALTER TABLE {} DROP COLUMN IF EXISTS {};").format(
sql.Identifier(project_uuid),
sql.Identifier(column[0]),
)
)
await cur.execute(
sql.SQL("DELETE FROM {} WHERE column_id = %s;").format(
sql.Identifier(f"{project_uuid}_column_map")
),
[column[0]],
)
await conn.commit()
async def chart_config(project_uuid: str, chart_id: int | None = None):
"""Delete the chart config for a given project or chart.
Args:
project_uuid (str): uuid of the project to delete the chart config for.
chart_id (int | None): the id of the chart this is linked to. Defaults to None.
"""
async with db_pool.connection() as conn:
async with conn.cursor() as cur:
if chart_id is None:
await cur.execute(
"DELETE FROM chart_config WHERE project_uuid = %s "
"AND chart_id IS NULL;",
[project_uuid],
)
else:
await cur.execute(
"DELETE FROM chart_config WHERE chart_id = %s;",
[chart_id],
)
await conn.commit()