@@ -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 (
0 commit comments