Coverage for downtify/artist_release_store.py: 100%
33 statements
« prev ^ index » next coverage.py v7.17.4, created at 2026-11-21 22:18 +0000
« prev ^ index » next coverage.py v7.17.4, created at 2026-11-21 22:18 +0000
1"""Release-specific progress; a recording may occur on several releases."""
3from __future__ import annotations
5from datetime import datetime, timezone
6from typing import Callable, ContextManager
8from downtify.db.config import Dialect
9from downtify.db.core import Connection
12class ArtistReleaseStore:
13 def __init__(
14 self, connect: Callable[[], ContextManager[Connection]]
15 ) -> None:
16 self._connect = connect
17 with self._connect() as conn:
18 if getattr(conn, 'dialect', Dialect.SQLITE) is Dialect.SQLITE:
19 self._init_sqlite(conn)
21 @staticmethod
22 def _init_sqlite(conn: Connection) -> None:
23 """Create the SQLite-only schema; PostgreSQL migrations own it."""
24 conn.execute("""CREATE TABLE IF NOT EXISTS artist_release_tracks (
25 playlist_id INTEGER NOT NULL,
26 album_id TEXT NOT NULL,
27 position INTEGER NOT NULL,
28 track_id TEXT NOT NULL,
29 filename TEXT NOT NULL,
30 PRIMARY KEY (playlist_id, album_id, position),
31 FOREIGN KEY (playlist_id) REFERENCES monitored_playlists(id)
32 ON DELETE CASCADE
33 )""")
34 columns = {
35 r['name'] for r in conn.execute('PRAGMA table_info(seen_albums)')
36 }
37 if 'last_attempt' not in columns:
38 conn.execute(
39 'ALTER TABLE seen_albums ADD COLUMN last_attempt TEXT'
40 )
42 def tracks(
43 self, playlist_id: int, album_id: str
44 ) -> dict[tuple[int, str], str]:
45 with self._connect() as conn:
46 return {
47 (r['position'], r['track_id']): r['filename']
48 for r in conn.execute(
49 'SELECT position,track_id,filename FROM artist_release_tracks WHERE playlist_id=? AND album_id=?',
50 (playlist_id, album_id),
51 )
52 }
54 def counts(self, playlist_id: int) -> dict[str, int]:
55 """``{album_id: tracks recorded}`` for an artist watch."""
57 with self._connect() as conn:
58 return {
59 r['album_id']: r['n']
60 for r in conn.execute(
61 'SELECT album_id, COUNT(*) AS n FROM artist_release_tracks '
62 'WHERE playlist_id=? GROUP BY album_id',
63 (playlist_id,),
64 )
65 }
67 def record(
68 self,
69 playlist_id: int,
70 album_id: str,
71 position: int,
72 track_id: str,
73 filename: str,
74 ) -> None:
75 with self._connect() as conn:
76 conn.execute(
77 """INSERT INTO artist_release_tracks VALUES (?,?,?,?,?)
78 ON CONFLICT(playlist_id,album_id,position) DO UPDATE SET
79 track_id=excluded.track_id,filename=excluded.filename""",
80 (playlist_id, album_id, position, track_id, filename),
81 )
83 def next_release(self, playlist_id: int) -> str | None:
84 """One historical release per pass; skipped baselines stay skipped.
86 NULL attempts (legacy rows) come first. Attempts advance before
87 contacting a provider, so unavailable releases cannot starve others.
88 """
89 with self._connect() as conn:
90 row = conn.execute(
91 """SELECT album_id FROM seen_albums
92 WHERE playlist_id=? AND skipped=0
93 ORDER BY last_attempt NULLS FIRST, id LIMIT 1""",
94 (playlist_id,),
95 ).fetchone()
96 return row['album_id'] if row else None
98 def attempt(self, playlist_id: int, album_id: str) -> None:
99 with self._connect() as conn:
100 conn.execute(
101 'UPDATE seen_albums SET last_attempt=? WHERE playlist_id=? AND album_id=?',
102 (
103 datetime.now(timezone.utc).isoformat(),
104 playlist_id,
105 album_id,
106 ),
107 )