Skip to content

Commit 69dce93

Browse files
committed
refactor: remove dead folders table
1 parent f252a86 commit 69dce93

3 files changed

Lines changed: 38 additions & 46 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
DB_VERSION_CURRENT_KEY: str = "CURRENT"
1111
DB_VERSION_INITIAL_KEY: str = "INITIAL"
12-
DB_VERSION: int = 202
12+
DB_VERSION: int = 203
1313

1414
TAG_CHILDREN_QUERY = text("""
1515
WITH RECURSIVE ChildTags AS (

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

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
from os import makedirs
1919
from pathlib import Path
2020
from typing import TYPE_CHECKING
21-
from uuid import uuid4
2221

2322
import sqlalchemy
2423
import structlog
@@ -95,7 +94,6 @@
9594
from tagstudio.core.library.alchemy.joins import TagEntry, TagParent
9695
from 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:

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -182,23 +182,11 @@ def __ge__(self, other: "Tag") -> bool:
182182
return self.name >= other.name
183183

184184

185-
class Folder(Base):
186-
__tablename__ = "folders"
187-
188-
# TODO - implement this
189-
id: Mapped[int] = mapped_column(primary_key=True)
190-
path: Mapped[Path] = mapped_column(PathType, unique=True)
191-
uuid: Mapped[str] = mapped_column(unique=True)
192-
193-
194185
class Entry(Base):
195186
__tablename__ = "entries"
196187

197188
id: Mapped[int] = mapped_column(primary_key=True)
198189

199-
folder_id: Mapped[int] = mapped_column(ForeignKey("folders.id"))
200-
folder: Mapped[Folder] = relationship("Folder")
201-
202190
path: Mapped[Path] = mapped_column(PathType, unique=True)
203191
filename: Mapped[str] = mapped_column()
204192
suffix: Mapped[str] = mapped_column()
@@ -235,7 +223,6 @@ def is_archived(self) -> bool:
235223
def __init__(
236224
self,
237225
path: Path,
238-
folder: Folder,
239226
fields: list[BaseField],
240227
id: int | None = None,
241228
date_created: dt | None = None,
@@ -244,7 +231,6 @@ def __init__(
244231
) -> None:
245232
super().__init__()
246233
self.path = path
247-
self.folder = folder
248234
self.id = id # pyright: ignore[reportAttributeAccessIssue]
249235
self.filename = path.name
250236
self.suffix = path.suffix.lstrip(".").lower()

0 commit comments

Comments
 (0)