diff --git a/database/music_database.py b/database/music_database.py index 120846be..a2c9df7c 100644 --- a/database/music_database.py +++ b/database/music_database.py @@ -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() diff --git a/tests/test_hifi_instance_methods.py b/tests/test_hifi_instance_methods.py index c41db12b..d300591c 100644 --- a/tests/test_hifi_instance_methods.py +++ b/tests/test_hifi_instance_methods.py @@ -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 ─────────────────────────────────────────────────── diff --git a/web_server.py b/web_server.py index 2d4b8eb4..b732a46d 100644 --- a/web_server.py +++ b/web_server.py @@ -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()