Fix tags, may have increased load on database while only using one query to fetch workout, topsets, and all tags

This commit is contained in:
Peter Stockings
2024-11-04 22:33:08 +11:00
parent 10326ccd7a
commit b5493e627c
6 changed files with 48 additions and 33 deletions

29
db.py
View File

@@ -342,14 +342,24 @@ class DataBase():
note, person_id, workout_id], commit=True)
def add_tag_for_workout(self, workout_id, tags_id):
# First, delete tags that are not in the new selection
self.execute(
"""
DELETE FROM workout_tag
WHERE workout_id = %s AND tag_id NOT IN %s
""",
[workout_id, tuple(tags_id)], commit=True
)
# If tags_id is not empty, delete tags that are not in the new selection
if tags_id:
self.execute(
"""
DELETE FROM workout_tag
WHERE workout_id = %s AND tag_id NOT IN %s
""",
[workout_id, tuple(tags_id)], commit=True
)
else:
# If tags_id is empty, delete all tags for this workout
self.execute(
"""
DELETE FROM workout_tag
WHERE workout_id = %s
""",
[workout_id], commit=True
)
# Then, attempt to insert the new tags
for tag_id in tags_id:
@@ -368,7 +378,8 @@ class DataBase():
T.tag_id AS "tag_id",
T.person_id AS "person_id",
T.name AS "tag_name",
T.filter AS "tag_filter"
T.filter AS "tag_filter",
TRUE AS "is_selected"
FROM Workout_Tag WT
LEFT JOIN Tag T ON WT.tag_id=T.tag_id
WHERE WT.workout_id=%s""", [workout_id])