Skip to content

Commit 05ae18c

Browse files
committed
Check if aggregated table exists
1 parent 28015d8 commit 05ae18c

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

mapswipe_workers/mapswipe_workers/auth.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ def copy_expert(self, sql, file):
8080
self._db_connection.commit()
8181
self._db_cur.close()
8282

83+
def table_exists(self, table_name):
84+
resp = self.retr_query(
85+
"SELECT to_regclass(%(table_name)s);", {"table_name": table_name}
86+
)
87+
return resp[0][0] is not None
88+
8389
def retr_query(self, query, data=None):
8490
self._db_cur = self._db_connection.cursor()
8591
self._db_cur.execute(query, data)

mapswipe_workers/mapswipe_workers/firebase_to_postgres/delete_project.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,13 @@ def delete_project(project_ids: list) -> bool:
114114
# -- Table from django/apps/aggregated/models.py. Used to cache stats data
115115
# NOTE: Django doesn't support database-level CASCADE delete
116116
# https://docs.djangoproject.com/en/4.1/ref/models/fields/#django.db.models.ForeignKey.on_delete
117-
sql_query = "DELETE FROM aggregated_aggregateduserstatdata WHERE project_id = %(project_id)s;"
118-
pg_db.query(sql_query, {"project_id": project_id})
119-
sql_query = "DELETE FROM aggregated_aggregatedusergroupstatdata WHERE project_id = %(project_id)s;"
120-
pg_db.query(sql_query, {"project_id": project_id})
117+
for aggregated_table_name in [
118+
"aggregated_aggregateduserstatdata",
119+
"aggregated_aggregatedusergroupstatdata",
120+
]:
121+
if pg_db.table_exists(aggregated_table_name):
122+
sql_query = f"DELETE FROM {aggregated_table_name} WHERE project_id = %(project_id)s;"
123+
pg_db.query(sql_query, {"project_id": project_id})
121124
# Finally delete the project
122125
sql_query = "DELETE FROM projects WHERE project_id = %(project_id)s;"
123126
pg_db.query(sql_query, {"project_id": project_id})

0 commit comments

Comments
 (0)