db: Give Libraries a way to list their Tracks

And make sure the tracks are deleted when a Library is deleted.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2021-08-23 11:57:01 -04:00
parent ea555a428c
commit 5a52bdc546
2 changed files with 20 additions and 0 deletions

View File

@ -9,6 +9,7 @@ from gi.repository import GObject
from . import commit
from . import execute
from . import objects
from . import track
class Library(objects.Row):
def __lt__(self, rhs): return self.path < rhs.path
@ -19,6 +20,11 @@ class Library(objects.Row):
return execute(f"SELECT {column} FROM libraries "
"WHERE libraryid=?", [ self.rowid ])
def tracks(self):
cursor = execute(f"SELECT trackid FROM tracks "
"WHERE libraryid=?", [ self.rowid ])
return [ track.Track(row["trackid"]) for row in cursor.fetchall() ]
@GObject.Property
def path(self):
return pathlib.Path(self.get_column("path"))
@ -50,6 +56,8 @@ class LibraryTable(objects.Table):
"VALUES (?)", (str(path),))
def do_delete(self, library):
for t in library.tracks():
track.Table.delete(t)
return execute("DELETE FROM libraries WHERE libraryid=?", [ int(library) ])
def do_get(self, rowid):

View File

@ -28,8 +28,12 @@ class TestLibraryTable(unittest.TestCase):
def test_library_table_delete(self):
library = db.library.Table.find(pathlib.Path("/a/b/c"))
track = db.make_fake_track(1, 1.234, "Test 1", "/a/b/c/1.ext",
lib=library.path)
db.library.Table.delete(library)
self.assertIsNone(db.library.Table.lookup(pathlib.Path("/a/b/c")))
self.assertIsNone(db.track.Table.get(int(track)))
def test_library_table_get(self):
library = db.library.Table.insert(pathlib.Path("/a/b/c"))
@ -57,3 +61,11 @@ class TestLibraryTable(unittest.TestCase):
abc.set_property("enabled", False)
self.assertFalse(abc.enabled)
def test_library_tracks(self):
library = db.library.Table.find(pathlib.Path("/a/b/c"))
track1 = db.make_fake_track(1, 1.234, "Test 1", "/a/b/c/1.ext",
lib=library.path)
track2 = db.make_fake_track(2, 2.345, "Test 1", "/a/b/c/2.ext",
lib=library.path)
self.assertEqual(library.tracks(), [ track1, track2 ])