1818from os import makedirs
1919from pathlib import Path
2020from typing import TYPE_CHECKING
21- from uuid import uuid4
2221
2322import sqlalchemy
2423import structlog
9594from tagstudio .core .library .alchemy .joins import TagEntry , TagParent
9695from tagstudio .core .library .alchemy .models import (
9796 Entry ,
98- Folder ,
9997 Namespace ,
10098 Tag ,
10199 TagAlias ,
@@ -234,7 +232,6 @@ class Library:
234232
235233 library_dir : Path | None = None
236234 engine : Engine | None = None
237- folder : Folder | None = None
238235 included_files : set [Path ] = set ()
239236
240237 def __init__ (self ) -> None :
@@ -259,7 +256,6 @@ def migrate_json_to_sqlite(self, json_lib: JsonLibrary):
259256 """Migrate JSON library data to the SQLite database."""
260257 logger .info ("Starting Library Conversion..." )
261258 start_time = time .time ()
262- folder : Folder = Folder (path = self .library_dir , uuid = str (uuid4 ()))
263259
264260 # Tags
265261 for tag in json_lib .tags :
@@ -312,7 +308,6 @@ def migrate_json_to_sqlite(self, json_lib: JsonLibrary):
312308 [
313309 Entry (
314310 path = entry .path / entry .filename ,
315- folder = folder ,
316311 fields = [],
317312 id = entry .id + 1 , # NOTE: JSON IDs start at 0 instead of 1
318313 date_added = datetime .now (),
@@ -479,16 +474,6 @@ def create_sqlite_library(
479474 session .add (Version (key = DB_VERSION_CURRENT_KEY , value = DB_VERSION ))
480475 session .flush ()
481476
482- # add folder for current path
483- folder = Folder (
484- path = library_dir ,
485- uuid = str (uuid4 ()),
486- )
487- session .add (folder )
488- session .expunge (folder )
489- session .flush ()
490- self .folder = folder
491-
492477 # Generate default .ts_ignore file
493478 try :
494479 ts_ignore_template = (
@@ -584,6 +569,7 @@ def open_sqlite_library(
584569 (self .__apply_db200_migration , 200 , None ), # changes: field tables
585570 (self .__apply_db201_migration , 201 , 200 ), # changes: field tables
586571 (self .__apply_db202_migration , 202 , None ), # changes: tag_parents
572+ (self .__apply_db203_migration , 203 , None ), # changes: deletes folders
587573 ]
588574 for migration , v , iv in migrations :
589575 if loaded_db_version < v and (iv is None or initial_db_version < iv ):
@@ -601,22 +587,6 @@ def open_sqlite_library(
601587 )
602588 logger .info (f"[Library] Library migrated to DB version { DB_VERSION } " )
603589
604- with Session (self .engine ) as session :
605- # TODO: the folder logic has no use and was never finished, remove it
606- # check if folder matching current path exists already
607- # NOTE: this has been causing new Folders to be created when the library is moved, since
608- # its introduction
609- self .folder = session .scalar (select (Folder ).where (Folder .path == library_dir ))
610- if not self .folder :
611- folder = Folder (
612- path = library_dir ,
613- uuid = str (uuid4 ()),
614- )
615- session .add (folder )
616- session .expunge (folder )
617- session .commit ()
618- self .folder = folder
619-
620590 # everything is fine, set the library path
621591 self .library_dir = library_dir
622592 return LibraryStatus (success = True , library_path = library_dir )
@@ -906,6 +876,42 @@ def __apply_db202_migration(self, session: Session, library_dir: Path):
906876 session .flush ()
907877 logger .info ("[Library][Migration][202] Verified TagParent table data" )
908878
879+ def __apply_db203_migration (self , session : Session , library_dir : Path ):
880+ ## remove folder_id column from entries table
881+ # create new table in the desired scheme (without folder_id column)
882+ session .execute (
883+ text ("""
884+ CREATE TABLE entries_new (
885+ id INTEGER NOT NULL,
886+ path VARCHAR NOT NULL,
887+ suffix VARCHAR NOT NULL,
888+ date_created DATETIME,
889+ date_modified DATETIME,
890+ date_added DATETIME, filename TEXT NOT NULL DEFAULT '',
891+ PRIMARY KEY (id),
892+ UNIQUE (path)
893+ )
894+ """ )
895+ )
896+ session .flush ()
897+ # transfer data to new table
898+ session .execute (
899+ text ("""
900+ INSERT INTO entries_new (id, path, suffix, date_created, date_modified, date_added)
901+ SELECT id, path, suffix, date_created, date_modified, date_added
902+ FROM entries
903+ """ )
904+ )
905+ # delete old table
906+ session .execute (text ("DROP TABLE entries" ))
907+ # rename new table to old table
908+ session .execute (text ("ALTER TABLE entries_new RENAME TO entries" ))
909+ session .flush ()
910+
911+ ## drop table "folders"
912+ session .execute (text ("DROP TABLE folders" ))
913+ session .flush ()
914+
909915 @property
910916 def field_templates (self ) -> Sequence [BaseFieldTemplate ]:
911917 with Session (self .engine ) as session :
0 commit comments