fix: make DBMigrations a context manager to correctly close connection

This commit is contained in:
Jann Stute
2026-09-04 15:45:08 +02:00
parent b11d4fe3ee
commit d9e419506c
2 changed files with 15 additions and 6 deletions
@@ -506,13 +506,12 @@ class Library:
# migrate if necessary
try:
migrations = DBMigrations(library_dir, sql_filename)
with DBMigrations(library_dir, sql_filename) as migrations:
# save backup if patches will be applied
if migrations.required:
Library.save_library_backup_to_disk(library_dir)
# save backup if patches will be applied
if migrations.required:
Library.save_library_backup_to_disk(library_dir)
migrations.run()
migrations.run()
except MigrationError as e:
return LibraryStatus(success=False, message=e.args[0])
@@ -78,11 +78,21 @@ class DBMigrations:
f"Opening Library with DB Version {self.loaded_db_version}/{DB_VERSION}"
)
self._exited = False
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, exc_traceback):
self._connection.close()
self._exited = True
@property
def required(self) -> bool:
return self.loaded_db_version < DB_VERSION
def run(self):
assert not self._exited
if not self.required:
return