fix(db): fix bug with releasing without lock in db.clean()

This commit is contained in:
psychedelicious 2023-12-05 06:48:53 +11:00
parent 7d4a78e470
commit 10cf10c16c

View File

@ -31,19 +31,18 @@ class SqliteDatabase:
self.conn.execute("PRAGMA foreign_keys = ON;") self.conn.execute("PRAGMA foreign_keys = ON;")
def clean(self) -> None: def clean(self) -> None:
try: with self.lock:
if self.db_path == sqlite_memory: try:
return if self.db_path == sqlite_memory:
initial_db_size = Path(self.db_path).stat().st_size return
self.lock.acquire() initial_db_size = Path(self.db_path).stat().st_size
self.conn.execute("VACUUM;") self.lock.acquire()
self.conn.commit() self.conn.execute("VACUUM;")
final_db_size = Path(self.db_path).stat().st_size self.conn.commit()
freed_space_in_mb = round((initial_db_size - final_db_size) / 1024 / 1024, 2) final_db_size = Path(self.db_path).stat().st_size
if freed_space_in_mb > 0: freed_space_in_mb = round((initial_db_size - final_db_size) / 1024 / 1024, 2)
self._logger.info(f"Cleaned database (freed {freed_space_in_mb}MB)") if freed_space_in_mb > 0:
except Exception as e: self._logger.info(f"Cleaned database (freed {freed_space_in_mb}MB)")
self._logger.error(f"Error cleaning database: {e}") except Exception as e:
raise self._logger.error(f"Error cleaning database: {e}")
finally: raise
self.lock.release()