Skip to content

Commit 9faa457

Browse files
committed
fix archive projects write size limit #310
1 parent 34acabc commit 9faa457

File tree

1 file changed

+36
-10
lines changed

1 file changed

+36
-10
lines changed

mapswipe_workers/mapswipe_workers/firebase_to_postgres/archive_project.py

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,50 @@ def archive_project(project_ids: list) -> None:
1515
"""
1616
for project_id in project_ids:
1717
logger.info("Archive project with the id {0}".format(project_id))
18-
logger.info(
19-
"Delete results, groups and tasks of project with the id {0}".format(
20-
project_id
21-
)
22-
)
18+
logger.info("Delete results of project with the id {0}".format(project_id))
2319

2420
fb_db = auth.firebaseDB()
2521
fb_db.reference("v2/results/{0}".format(project_id)).set({})
26-
fb_db.reference("v2/groups/{0}".format(project_id)).set({})
27-
fb_db.reference("v2/tasks/{0}".format(project_id)).set({})
2822

23+
# get group keys for this project to estimate size in firebase
24+
group_keys = list(
25+
fb_db.reference("v2/tasks/{0}/".format(project_id)).get(shallow=True).keys()
26+
)
27+
chunk_size = 250
28+
chunks = int(len(group_keys) / chunk_size) + 1
29+
30+
# delete groups and tasks in firebase for each chunk using the update function
31+
for i in range(1, chunks):
32+
logger.info(
33+
"Delete max {1} groups and tasks of project with the id {0}".format(
34+
project_id, chunk_size
35+
)
36+
)
37+
update_dict = {}
38+
for group_id in group_keys[:250]:
39+
update_dict[group_id] = None
40+
fb_db.reference("v2/groups/{0}".format(project_id)).update(update_dict)
41+
fb_db.reference("v2/tasks/{0}".format(project_id)).update(update_dict)
42+
43+
logger.info(
44+
"Set status=archived in Firebase for project with the id {0}".format(
45+
project_id
46+
)
47+
)
2948
fb_db = auth.firebaseDB()
3049
ref = fb_db.reference("v2/projects/{0}/status".format(project_id))
3150
ref.set("archived")
3251

52+
logger.info(
53+
"Set status=archived in Postgres for project with the id {0}".format(
54+
project_id
55+
)
56+
)
3357
pg_db = auth.postgresDB()
34-
sql_query = (
35-
"UPDATE projects SET status = 'archived' "
36-
+ "WHERE project_id = '{0}'".format(project_id)
58+
sql_query = """
59+
UPDATE projects SET status = 'archived'
60+
WHERE project_id = '{0}';
61+
""".format(
62+
project_id
3763
)
3864
pg_db.query(sql_query)

0 commit comments

Comments
 (0)