add triggers for dropping album entries consistently with items

This commit is contained in:
Adrian Sampson 2010-07-13 21:14:11 -07:00
parent 5096a12052
commit dd0c70e01c

View file

@ -716,6 +716,8 @@ class Library(BaseLibrary):
self._make_table('items', item_fields)
self._make_table('albums', album_fields)
self._make_triggers()
def _make_table(self, table, fields):
"""Set up the schema of the library file. fields is a list of
@ -753,6 +755,31 @@ class Library(BaseLibrary):
self.conn.executescript(setup_sql)
self.conn.commit()
def _make_triggers(self):
"""Setup triggers for the database to keep the tables
consistent.
"""
# Set up triggers for dropping album info rows when no longer
# needed.
trigger_sql = """
WHEN
((SELECT id FROM items WHERE album=OLD.album AND artist=OLD.artist)
IS NULL)
BEGIN
DELETE FROM albums WHERE
album=OLD.album AND artist=OLD.artist;
END;
"""
self.conn.execute("""
CREATE TRIGGER IF NOT EXISTS delete_album
AFTER DELETE ON items
""" + trigger_sql)
self.conn.execute("""
CREATE TRIGGER IF NOT EXISTS change_album
AFTER UPDATE OF album, artist ON items
""" + trigger_sql)
self.conn.commit()
def destination(self, item):
"""Returns the path in the library directory designated for item
item (i.e., where the file ought to be).