-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #638 from mapswipe/update-organisation-name-script
Update organisation name script
- Loading branch information
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
mapswipe_workers/python_scripts/change_organization_name_for_projects.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import sys | ||
|
||
from mapswipe_workers.auth import firebaseDB | ||
from mapswipe_workers.definitions import logger | ||
|
||
|
||
def get_all_projects(organization_name: str): | ||
"""Get the project ids for active and inactive projects in Firebase DB.""" | ||
|
||
project_id_list = [] | ||
fb_db = firebaseDB() | ||
|
||
# we neglect private projects here | ||
# since there are no projects set up in production yet | ||
status_list = ["active", "inactive"] | ||
|
||
for status in status_list: | ||
logger.info(f"query {status} projects") | ||
projects = ( | ||
fb_db.reference("v2/projects/") | ||
.order_by_child("status") | ||
.equal_to(status) | ||
.get() | ||
) | ||
for project_id, data in projects.items(): | ||
if data.get("requestingOrganisation", None) == organization_name: | ||
project_id_list.append(project_id) | ||
|
||
logger.info(f"got {len(project_id_list)} project from firebase.") | ||
logger.info(f"projects: {project_id_list}") | ||
return project_id_list | ||
|
||
|
||
def update_projects( | ||
project_id_list, old_organization_name: str, new_organization_name: str | ||
): | ||
fb_db = firebaseDB() | ||
if len(project_id_list) < 1: | ||
logger.info("there are no matching projects.") | ||
else: | ||
for i, project_id in enumerate(project_id_list): | ||
fb_db.reference(f"v2/projects/{project_id}/requestingOrganisation").set( | ||
new_organization_name | ||
) | ||
logger.info( | ||
f"#{i} set organization name '{new_organization_name}'" | ||
f"for project '{project_id}'" | ||
) | ||
|
||
project_name = fb_db.reference(f"v2/projects/{project_id}/name").get() | ||
new_project_name = project_name.replace( | ||
old_organization_name, new_organization_name | ||
) | ||
fb_db.reference(f"v2/projects/{project_id}/organizationName").set( | ||
new_project_name | ||
) | ||
logger.info( | ||
f"#{i} set project name '{new_project_name}' for project '{project_id}'" | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
"""change_organization_name_for_projects.py old_name new_name""" | ||
old_organization_name = sys.argv[1] | ||
new_organization_name = sys.argv[2] | ||
project_id_list = get_all_projects(old_organization_name) | ||
update_projects(project_id_list, old_organization_name, new_organization_name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
update projects set organization_name = 'Arizona State University' where organization_name = 'ASU'; | ||
update projects set name = replace(name, 'ASU', 'Arizona State University') where organization_name = 'Arizona State University'; |