|
| 1 | +from mapswipe_workers import auth |
| 2 | +from mapswipe_workers.definitions import logger |
| 3 | + |
| 4 | + |
| 5 | +def get_old_projects(): |
| 6 | + """ |
| 7 | + Get all projects from Firebase which have been created |
| 8 | + before we switched to v2. |
| 9 | + """ |
| 10 | + fb_db = auth.firebaseDB() |
| 11 | + ref = fb_db.reference("projects") |
| 12 | + projects = ref.get() |
| 13 | + logger.info("got old projects from firebase") |
| 14 | + return projects |
| 15 | + |
| 16 | + |
| 17 | +def move_project_data_to_v2(project_id): |
| 18 | + """ |
| 19 | + Copy project information from old path to v2/projects in Firebase. |
| 20 | + Add status=archived attribute. |
| 21 | + Use Firebase transaction function for this. |
| 22 | + """ |
| 23 | + |
| 24 | + # Firebase transaction function |
| 25 | + def transfer(current_data): |
| 26 | + # we need to add these attributes |
| 27 | + # since they are expected for version 2 |
| 28 | + current_data["status"] = "archived" |
| 29 | + current_data["projectType"] = 1 |
| 30 | + current_data["projectId"] = str(project_id) |
| 31 | + current_data["progress"] = current_data.get("progress", 0) |
| 32 | + current_data["name"] = current_data.get("name", "unknown") |
| 33 | + fb_db.reference("v2/projects/{0}".format(project_id)).set(current_data) |
| 34 | + return dict() |
| 35 | + |
| 36 | + fb_db = auth.firebaseDB() |
| 37 | + projects_ref = fb_db.reference(f"projects/{project_id}") |
| 38 | + try: |
| 39 | + projects_ref.transaction(transfer) |
| 40 | + logger.info(f"{project_id}: Transfered project to v2 and delete in old path") |
| 41 | + return True |
| 42 | + except fb_db.TransactionAbortedError: |
| 43 | + logger.exception( |
| 44 | + f"{project_id}: Firebase transaction" |
| 45 | + f"for transferring project failed to commit" |
| 46 | + ) |
| 47 | + return False |
| 48 | + |
| 49 | + |
| 50 | +def delete_old_groups(project_id): |
| 51 | + """ |
| 52 | + Delete old groups for a project |
| 53 | + """ |
| 54 | + fb_db = auth.firebaseDB() |
| 55 | + fb_db.reference("groups/{0}".format(project_id)).set({}) |
| 56 | + logger.info(f"deleted groups for: {project_id}") |
| 57 | + |
| 58 | + |
| 59 | +def delete_other_old_data(): |
| 60 | + """ |
| 61 | + Delete old imports, results, announcements in Firebase |
| 62 | + """ |
| 63 | + fb_db = auth.firebaseDB() |
| 64 | + fb_db.reference("imports").set({}) |
| 65 | + fb_db.reference("results").set({}) |
| 66 | + fb_db.reference("announcements").set({}) |
| 67 | + logger.info(f"deleted old results, imports, announcements") |
| 68 | + |
| 69 | + |
| 70 | +def archive_old_projects(): |
| 71 | + """ |
| 72 | + Run workflow to archive old projects. |
| 73 | + First get all old projects. |
| 74 | + Move project data to v2/projects in Firebase and |
| 75 | + set status=archived. |
| 76 | + Then delete all groups for a project. |
| 77 | + Finally, delete old results, imports and announcements. |
| 78 | + We don't touch the old user data in this workflow. |
| 79 | + """ |
| 80 | + |
| 81 | + projects = get_old_projects() |
| 82 | + for project_id in projects.keys(): |
| 83 | + if move_project_data_to_v2(project_id): |
| 84 | + delete_old_groups(project_id) |
| 85 | + else: |
| 86 | + logger.info(f"didn't delete project and groups for project: {project_id}") |
| 87 | + |
| 88 | + delete_other_old_data() |
| 89 | + |
| 90 | + |
| 91 | +archive_old_projects() |
0 commit comments