Add sanity check for thumbnails

Warn in case we have thumbnails in our db that do not refer to an
existing media entry in our db.
This commit is contained in:
Sebastian Spaeth 2023-04-20 09:45:20 +02:00
parent 1df1a8e74c
commit 1f3bc1e0c9

View File

@ -122,6 +122,16 @@ class MediaRepository:
f = File(self, row[0], row[1]//1000, row[2])
files.append(f)
return files
def sanity_check_thumbnails(self) -> None:
"""Warn if we have thumbnails in the db that do not refer to existing media"""
with self.conn.cursor() as cur:
cur.execute("SELECT COUNT(media_id) from mediaapi_thumbnail WHERE media_id NOT IN (SELECT media_id FROM mediaapi_media_repository);")
row = cur.fetchone()
if row[0]:
logging.error("You have {} thumbnails in your db that do not refer to media. This needs fixing (we don't do that)!".format(row[0]))
#--------------------------------------------------------------
def read_config(conf_file: Union[str,Path]) -> Tuple[Path, str]:
"""Read in the dendrite config file and return db creds and media path"""
@ -176,7 +186,8 @@ if __name__ == '__main__':
args = parse_options()
(MEDIA_PATH, CONN_STR) = read_config(args.config)
mr = MediaRepository(MEDIA_PATH, CONN_STR)
# today() is local computer timezone, which might differ from the db server
mr.sanity_check_thumbnails() # warn in case of superfluous thumbnails
#------------------
cleantime = datetime.today() - timedelta(days=args.days)
logging.info("Deleting remote media older than %s", cleantime)
files = mr.get_remote_media()