From 750d93673de0e3374c25ec1138ebdfc25e961aa6 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Tue, 15 Sep 2020 14:57:20 -0700 Subject: [PATCH] Add method purge_deleted_associated_directories. --- etiquette/photodb.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/etiquette/photodb.py b/etiquette/photodb.py index cd3208a..0186e81 100644 --- a/etiquette/photodb.py +++ b/etiquette/photodb.py @@ -120,6 +120,22 @@ class PDBAlbumMixin: return album + @decorators.transaction + def purge_deleted_associated_directories(self, albums=None): + directories = self.sql_select('SELECT DISTINCT directory FROM album_associated_directories') + directories = (pathclass.Path(d[0]) for d in directories) + directories = [d.absolute_path for d in directories if not d.exists] + if not directories: + return + self.log.debug('Purging associated directories %s', directories) + directories = sqlhelpers.listify(directories) + + query = f'DELETE FROM album_associated_directories WHERE directory in {directories}' + if albums is not None: + album_ids = sqlhelpers.listify(a.id for a in albums) + query += f' AND albumid IN {album_ids}' + self.sql_execute(query) + @decorators.transaction def purge_empty_albums(self, albums=None): if albums is None: