Skip to content

Commit a6890c8

Browse files
authored
Merge pull request #351 from mapswipe/dev
fix write size limit in archive projects #350
2 parents 6a8c756 + 6cc1048 commit a6890c8

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

mapswipe_workers/python_scripts/archive_old_projects.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
from mapswipe_workers import auth
2-
from mapswipe_workers.definitions import logger
2+
from mapswipe_workers.definitions import logger, CustomError
3+
from typing import Iterable
4+
import re
5+
from firebase_admin import exceptions
6+
7+
8+
def chunks(data: list, size: int = 250) -> Iterable[list]:
9+
"""Yield successive n-sized chunks from list."""
10+
for i in range(0, len(data), size):
11+
yield data[i : i + size] # noqa E203
312

413

514
def get_old_projects():
@@ -52,8 +61,21 @@ def delete_old_groups(project_id):
5261
Delete old groups for a project
5362
"""
5463
fb_db = auth.firebaseDB()
55-
fb_db.reference("groups/{0}".format(project_id)).set({})
56-
logger.info(f"deleted groups for: {project_id}")
64+
ref = fb_db.reference(f"/groups/{project_id}")
65+
if not re.match(r"/\w+/[-a-zA-Z0-9]+", ref.path):
66+
raise CustomError(
67+
f"""Given argument resulted in invalid Firebase Realtime Database reference.
68+
{ref.path}"""
69+
)
70+
try:
71+
ref.delete()
72+
except exceptions.InvalidArgumentError:
73+
# Data to write exceeds the maximum size that can be modified
74+
# with a single request. Delete chunks of data instead.
75+
childs = ref.get(shallow=True)
76+
for chunk in chunks(list(childs.keys())):
77+
ref.update({key: None for key in chunk})
78+
ref.delete()
5779

5880

5981
def delete_other_old_data():

0 commit comments

Comments
 (0)