validate hifi instance reorder against pre-existing instances

This commit is contained in:
elmerohueso 2026-04-28 18:29:12 -06:00
parent ef3790d146
commit 7f94597706
3 changed files with 19 additions and 5 deletions

View file

@ -11649,9 +11649,22 @@ class MusicDatabase:
return cursor.rowcount > 0
def reorder_hifi_instances(self, urls: List[str]) -> bool:
"""Update priorities based on the given URL order."""
"""Update priorities based on the given URL order.
Returns False if any URL does not exist in the database.
"""
if not urls:
return True
conn = self._get_connection()
cursor = conn.cursor()
placeholders = ",".join("?" for _ in urls)
cursor.execute(
f"SELECT url FROM hifi_instances WHERE url IN ({placeholders})",
urls
)
existing = {row["url"] for row in cursor.fetchall()}
missing = [u for u in urls if u not in existing]
if missing:
return False
for i, url in enumerate(urls):
cursor.execute("UPDATE hifi_instances SET priority = ? WHERE url = ?", (i, url))
conn.commit()

View file

@ -274,10 +274,10 @@ def test_reorder_hifi_instances_returns_true_on_empty_list(db):
assert db.reorder_hifi_instances([]) is True
def test_reorder_hifi_instances_returns_true_even_with_unknown_urls(db):
"""UPDATE that matches 0 rows is not an error."""
def test_reorder_hifi_instances_returns_false_with_unknown_urls(db):
"""Reorder should fail when any URL doesn't exist."""
_seed(db, instances=[("http://a.com", 0, 1)])
assert db.reorder_hifi_instances(["http://a.com", "http://phantom.com"]) is True
assert db.reorder_hifi_instances(["http://a.com", "http://phantom.com"]) is False
# ── seed_hifi_instances ───────────────────────────────────────────────────

View file

@ -24782,7 +24782,8 @@ def hifi_reorder_instances():
return jsonify({'success': False, 'error': 'URL list is required'}), 400
from database.music_database import get_database
db = get_database()
db.reorder_hifi_instances(urls)
if not db.reorder_hifi_instances(urls):
return jsonify({'success': False, 'error': 'One or more URLs not found'}), 400
# Reload the client
if soulseek_client and hasattr(soulseek_client, 'hifi') and soulseek_client.hifi:
soulseek_client.hifi.reload_instances()