Skip to content

Commit 81c85b1

Browse files
authored
Merge pull request #311 from mapswipe/fix-archive-projects
Fix archive projects
2 parents 34acabc + 312f7db commit 81c85b1

File tree

1 file changed

+38
-16
lines changed

1 file changed

+38
-16
lines changed

mapswipe_workers/mapswipe_workers/firebase_to_postgres/archive_project.py

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,47 @@ def archive_project(project_ids: list) -> None:
1414
Set status = archived for project in Firebase and Postgres.
1515
"""
1616
for project_id in project_ids:
17-
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-
)
17+
logger.info(f"Archive project with the id {project_id}")
18+
logger.info(f"Delete results of project with the id {project_id}")
2319

2420
fb_db = auth.firebaseDB()
25-
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({})
21+
fb_db.reference(f"v2/results/{project_id}").set({})
22+
23+
# get group keys for this project to estimate size in firebase
24+
groups = fb_db.reference(f"v2/groups/{project_id}").get(shallow=True)
25+
26+
if not groups:
27+
logger.info("no groups to delete in firebase")
28+
else:
29+
group_keys = list(groups.keys())
30+
chunk_size = 250
31+
chunks = int(len(group_keys) / chunk_size) + 1
32+
33+
# delete groups, tasks in firebase for each chunk using the update function
34+
for i in range(0, chunks):
35+
logger.info(
36+
f"Delete max {chunk_size} groups and tasks"
37+
f"of project with the id {project_id}"
38+
)
39+
update_dict = {}
40+
for group_id in group_keys[:chunk_size]:
41+
update_dict[group_id] = None
42+
fb_db.reference(f"v2/groups/{project_id}").update(update_dict)
43+
fb_db.reference(f"v2/tasks/{project_id}").update(update_dict)
44+
group_keys = group_keys[chunk_size:]
2845

46+
logger.info(
47+
f"Set status=archived in Firebase for project with the id {project_id}"
48+
)
2949
fb_db = auth.firebaseDB()
30-
ref = fb_db.reference("v2/projects/{0}/status".format(project_id))
31-
ref.set("archived")
50+
fb_db.reference(f"v2/projects/{project_id}/status").set("archived")
3251

33-
pg_db = auth.postgresDB()
34-
sql_query = (
35-
"UPDATE projects SET status = 'archived' "
36-
+ "WHERE project_id = '{0}'".format(project_id)
52+
logger.info(
53+
f"Set status=archived in Postgres for project with the id {project_id}"
3754
)
38-
pg_db.query(sql_query)
55+
pg_db = auth.postgresDB()
56+
sql_query = """
57+
UPDATE projects SET status = 'archived'
58+
WHERE project_id = %(project_id)s;
59+
"""
60+
pg_db.query(sql_query, {"project_id": project_id})

0 commit comments

Comments
 (0)