mirror of
https://github.com/TagStudioDev/TagStudio.git
synced 2026-09-04 18:19:04 +02:00
fix: make DBMigrations a context manager to correctly close connection
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user