Skip to content

Commit 910d2b7

Browse files
refactor: cleanup parameters of open_library and open_sqlite_library (#1294)
1 parent 47d4de5 commit 910d2b7

3 files changed

Lines changed: 32 additions & 30 deletions

File tree

src/tagstudio/core/library/alchemy/library.py

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,6 @@ class Library:
218218
"""Class for the Library object, and all CRUD operations made upon it."""
219219

220220
library_dir: Path | None = None
221-
storage_path: Path | str | None = None
222221
engine: Engine | None = None
223222
folder: Folder | None = None
224223
included_files: set[Path] = set()
@@ -233,7 +232,6 @@ def close(self):
233232
if self.engine:
234233
self.engine.dispose()
235234
self.library_dir = None
236-
self.storage_path = None
237235
self.folder = None
238236
self.included_files = set()
239237

@@ -349,33 +347,36 @@ def tag_display_name(self, tag: Tag | None) -> str:
349347
else:
350348
return tag.name
351349

352-
def open_library(
353-
self, library_dir: Path, storage_path: Path | str | None = None
354-
) -> LibraryStatus:
355-
is_new: bool = True
356-
if storage_path == ":memory:":
357-
self.storage_path = storage_path
358-
is_new = True
359-
return self.open_sqlite_library(library_dir, is_new)
360-
else:
361-
self.storage_path = library_dir / TS_FOLDER_NAME / SQL_FILENAME
362-
assert isinstance(self.storage_path, Path)
363-
if self.verify_ts_folder(library_dir) and (is_new := not self.storage_path.exists()):
364-
json_path = library_dir / TS_FOLDER_NAME / JSON_FILENAME
365-
if json_path.exists():
366-
return LibraryStatus(
367-
success=False,
368-
library_path=library_dir,
369-
message="[JSON] Legacy v9.4 library requires conversion to v9.5+",
370-
json_migration_req=True,
371-
)
350+
def open_library(self, library_dir: Path, in_memory: bool = False) -> LibraryStatus:
351+
"""Wrapper for open_sqlite_library.
372352
373-
return self.open_sqlite_library(library_dir, is_new)
353+
Handles in-memory storage and checks whether a JSON-migration is necessary.
354+
"""
355+
assert isinstance(library_dir, Path)
356+
357+
if in_memory:
358+
return self.open_sqlite_library(library_dir, is_new=True, storage_path=":memory:")
359+
360+
is_new = True
361+
sql_path = library_dir / TS_FOLDER_NAME / SQL_FILENAME
362+
if self.verify_ts_folder(library_dir) and (is_new := not sql_path.exists()):
363+
json_path = library_dir / TS_FOLDER_NAME / JSON_FILENAME
364+
if json_path.exists():
365+
return LibraryStatus(
366+
success=False,
367+
library_path=library_dir,
368+
message="[JSON] Legacy v9.4 library requires conversion to v9.5+",
369+
json_migration_req=True,
370+
)
374371

375-
def open_sqlite_library(self, library_dir: Path, is_new: bool) -> LibraryStatus:
372+
return self.open_sqlite_library(library_dir, is_new, str(sql_path))
373+
374+
def open_sqlite_library(
375+
self, library_dir: Path, is_new: bool, storage_path: str
376+
) -> LibraryStatus:
376377
connection_string = URL.create(
377378
drivername="sqlite",
378-
database=str(self.storage_path),
379+
database=storage_path,
379380
)
380381
# NOTE: File-based databases should use NullPool to create new DB connection in order to
381382
# keep connections on separate threads, which prevents the DB files from being locked
@@ -384,7 +385,7 @@ def open_sqlite_library(self, library_dir: Path, is_new: bool) -> LibraryStatus:
384385
# More info can be found on the SQLAlchemy docs:
385386
# https://docs.sqlalchemy.org/en/20/changelog/migration_07.html
386387
# Under -> sqlite-the-sqlite-dialect-now-uses-nullpool-for-file-based-databases
387-
poolclass = None if self.storage_path == ":memory:" else NullPool
388+
poolclass = None if storage_path == ":memory:" else NullPool
388389
loaded_db_version: int = 0
389390

390391
logger.info(

src/tagstudio/qt/mixed/migration_modal.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,11 +410,12 @@ def migration_iterator(self):
410410
self.temp_path: Path = (
411411
self.json_lib.library_dir / TS_FOLDER_NAME / "migration_ts_library.sqlite"
412412
)
413-
self.sql_lib.storage_path = self.temp_path
414413
if self.temp_path.exists():
415414
logger.info('Temporary migration file "temp_path" already exists. Removing...')
416415
self.temp_path.unlink()
417-
self.sql_lib.open_sqlite_library(self.json_lib.library_dir, is_new=True)
416+
self.sql_lib.open_sqlite_library(
417+
self.json_lib.library_dir, is_new=True, storage_path=str(self.temp_path)
418+
)
418419
yield Translations.format(
419420
"json_migration.migrating_files_entries", entries=len(self.json_lib.entries)
420421
)

tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def cwd():
3333
def file_mediatypes_library():
3434
lib = Library()
3535

36-
status = lib.open_library(Path(""), ":memory:")
36+
status = lib.open_library(Path(""), in_memory=True)
3737
assert status.success
3838
folder = unwrap(lib.folder)
3939

@@ -84,7 +84,7 @@ def library(request, library_dir: Path): # pyright: ignore
8484
library_path = Path(request.param)
8585

8686
lib = Library()
87-
status = lib.open_library(library_path, ":memory:")
87+
status = lib.open_library(library_path, in_memory=True)
8888
assert status.success
8989
folder = unwrap(lib.folder)
9090

0 commit comments

Comments
 (0)