From dd0c70e01ca3cd81152a270685aacc2f829be096 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Tue, 13 Jul 2010 21:14:11 -0700 Subject: [PATCH] add triggers for dropping album entries consistently with items --- beets/library.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/beets/library.py b/beets/library.py index 819645493..6b2c85ca7 100644 --- a/beets/library.py +++ b/beets/library.py @@ -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).