|
1 | 1 | 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 |
3 | 12 |
|
4 | 13 |
|
5 | 14 | def get_old_projects(): |
@@ -52,8 +61,21 @@ def delete_old_groups(project_id): |
52 | 61 | Delete old groups for a project |
53 | 62 | """ |
54 | 63 | 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() |
57 | 79 |
|
58 | 80 |
|
59 | 81 | def delete_other_old_data(): |
|
0 commit comments