From 3208a32ffe05b3624ffa1fec9a199a21ee7727e0 Mon Sep 17 00:00:00 2001 From: Colin Powell Date: Sat, 29 Mar 2025 13:49:54 -0400 Subject: [PATCH] [scrobbles] Fixes dedup utility, adding a transaction Deleting tracks needs to be in a transaction so we don't try to delete a track that may still have scrobbles associated with it. --- vrobbler/apps/scrobbles/utils.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/vrobbler/apps/scrobbles/utils.py b/vrobbler/apps/scrobbles/utils.py index 2b83bcd..8897ec4 100644 --- a/vrobbler/apps/scrobbles/utils.py +++ b/vrobbler/apps/scrobbles/utils.py @@ -2,12 +2,12 @@ import hashlib import logging import re from datetime import datetime, timedelta, tzinfo +from sqlite3 import IntegrityError import pytz -import requests from django.apps import apps from django.contrib.auth import get_user_model -from django.db import models +from django.db import models, transcation from django.utils import timezone from profiles.models import UserProfile from profiles.utils import now_user_timezone @@ -271,7 +271,6 @@ def get_file_md5_hash(file_path: str) -> str: file_hash.update(chunk) return file_hash.hexdigest() - def deduplicate_tracks(commit=False) -> int: from music.models import Track @@ -288,9 +287,13 @@ def deduplicate_tracks(commit=False) -> int: for other in tracks.exclude(id=first.id): print("moving scrobbles for ", other.id, " to ", first.id) if commit: - other.scrobble_set.update(track=first) - print("deleting ", other.id, " - ", other) - other.delete() + with transaction.atomic(): + other.scrobble_set.update(track=first) + print("deleting ", other.id, " - ", other) + try: + other.delete() + except IntegrityError as e: + print("could not delete ", other.id, f": IntegrityError {e}") return len(dups)