Skip to content

Commit dc81d75

Browse files
committed
Delete Aggregated data before project delete
1 parent d983fbf commit dc81d75

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

django/apps/aggregated/models.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class Type(models.IntegerChoices):
1616
value = models.CharField(max_length=225, null=True)
1717

1818

19+
# NOTE: Django generated table_name is used in mapswipe_workers->delete_project.py
1920
class AggregatedUserStatData(Model):
2021
# Ref Fields
2122
project = models.ForeignKey(Project, on_delete=models.CASCADE, related_name="+")
@@ -35,6 +36,7 @@ class Meta:
3536
)
3637

3738

39+
# NOTE: Django generated table_name is used in mapswipe_workers->delete_project.py
3840
class AggregatedUserGroupStatData(Model):
3941
# Ref Fields
4042
project = models.ForeignKey(Project, on_delete=models.CASCADE, related_name="+")

mapswipe_workers/mapswipe_workers/firebase_to_postgres/delete_project.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,14 @@ def delete_project(project_ids: list) -> bool:
111111
pg_db.query(sql_query, {"project_id": project_id})
112112
sql_query = "DELETE FROM groups WHERE project_id = %(project_id)s;"
113113
pg_db.query(sql_query, {"project_id": project_id})
114+
# -- Table from django/apps/aggregated/models.py. Used to cache stats data
115+
# NOTE: Django doesn't support database-level CASCADE delete
116+
# 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})
121+
# Finally delete the project
114122
sql_query = "DELETE FROM projects WHERE project_id = %(project_id)s;"
115123
pg_db.query(sql_query, {"project_id": project_id})
116124

0 commit comments

Comments
 (0)