fix: integrity errors when restoring backups

This commit is contained in:
cwilvx
2025-06-17 09:47:41 +02:00
parent 39fe359864
commit 0934e4ed7a
2 changed files with 38 additions and 9 deletions
+27 -6
View File
@@ -7,6 +7,7 @@ import shutil
from time import time
from flask_openapi3 import Tag
from flask_openapi3 import APIBlueprint
import sqlalchemy.exc
from swingmusic.api.auth import admin_required
from swingmusic.db.userdata import FavoritesTable, PlaylistTable, ScrobbleTable
@@ -96,6 +97,9 @@ def backup():
class RestoreBackup:
# TODO: BACKUP AND RESTORE COLLECTIONS & MIXES!
# TODO: IMPROVE UX WHEN WAITING FOR RESTORE TO COMPLETE!
def __init__(self, backup_dir: Path):
self.backup_dir = backup_dir
self.backup_file = backup_dir / "data.json"
@@ -114,8 +118,12 @@ class RestoreBackup:
existing_hashes = set(fav.hash for fav in existing_favorites)
new_favorites = [fav for fav in favorites if fav["hash"] not in existing_hashes]
if new_favorites:
FavoritesTable.insert_many(new_favorites)
for fav in new_favorites:
try:
FavoritesTable.insert_item(fav)
except sqlalchemy.exc.IntegrityError:
print("Integrity error, skipping favorite")
print(fav)
def restore_playlists(self, playlists: list[dict]):
existing_playlists = PlaylistTable.get_all()
@@ -124,8 +132,16 @@ class RestoreBackup:
playlist for playlist in playlists if playlist["name"] not in existing_names
]
if new_playlists:
PlaylistTable.insert_many(new_playlists)
for playlist in new_playlists:
try:
if playlist.get("_score") is not None:
print("Removing _score from playlist")
del playlist["_score"]
PlaylistTable.add_one(playlist)
except sqlalchemy.exc.IntegrityError:
print("Integrity error, skipping playlist")
print(playlist)
def restore_scrobbles(self, scrobbles: list[dict]):
existing_scrobbles = ScrobbleTable.get_all(0)
@@ -139,8 +155,13 @@ class RestoreBackup:
if f"{scrobble['trackhash']}.{scrobble['timestamp']}" not in existing_hashes
]
if new_scrobbles:
ScrobbleTable.insert_many(new_scrobbles)
for scrobble in new_scrobbles:
try:
ScrobbleTable.add(scrobble)
except sqlalchemy.exc.IntegrityError:
print("Integrity error, skipping scrobble")
print(scrobble)
class RestoreBackupBody(BaseModel):
+8
View File
@@ -219,7 +219,12 @@ class FavoritesTable(Base):
# guard against hash collisions for different item types
item["hash"] = f"{item['type']}_{item['hash']}"
if item.get("timestamp") is None:
print("No timestamp found, using current timestamp")
item["timestamp"] = int(datetime.datetime.now().timestamp())
if item.get("userid") is None:
print("No userid found, using current userid")
item["userid"] = get_current_userid()
return next(cls.execute(insert(cls).values(item), commit=True))
@@ -337,7 +342,10 @@ class ScrobbleTable(Base):
@classmethod
def add(cls, item: dict[str, Any]):
if item.get("userid") is None:
print("No userid found, using current userid")
item["userid"] = get_current_userid()
return cls.insert_one(item)
@classmethod