Parcourir la source

[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.
Colin Powell il y a 1 mois
Parent
commit
3208a32ffe
1 fichiers modifiés avec 9 ajouts et 6 suppressions
  1. 9 6
      vrobbler/apps/scrobbles/utils.py

+ 9 - 6
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)