Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/tagstudio/core/library/alchemy/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
DB_VERSION_LEGACY_KEY: str = "DB_VERSION"
DB_VERSION_CURRENT_KEY: str = "CURRENT"
DB_VERSION_INITIAL_KEY: str = "INITIAL"
DB_VERSION: int = 101
DB_VERSION: int = 102

TAG_CHILDREN_QUERY = text("""
WITH RECURSIVE ChildTags AS (
Expand Down
51 changes: 50 additions & 1 deletion src/tagstudio/core/library/alchemy/library.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Copyright (C) 2025
# Licensed under the GPL-3.0 License.
# Created for TagStudio: https://github.com/CyanVoxel/TagStudio
Expand Down Expand Up @@ -542,7 +542,11 @@
if loaded_db_version < 9:
self.__apply_db9_filename_population(session)
if loaded_db_version < 100:
self.__apply_db100_parent_repairs(session)
self.__apply_db100_parent_repairs(session)

# This change might break stuff if done before the datachanges.
if loaded_db_version < 102:
self.__apply_db102_schema_changes(session)

# Convert file extension list to ts_ignore file, if a .ts_ignore file does not exist
self.migrate_sql_to_ts_ignore(library_dir)
Expand Down Expand Up @@ -681,6 +685,51 @@
session.commit()
logger.info("[Library][Migration] Refactored TagParent column")

def __apply_db102_schema_changes(self, session: Session):
"""Add a trigger to prevent having a tag being its own parent"""

Check failure on line 689 in src/tagstudio/core/library/alchemy/library.py

View workflow job for this annotation

GitHub Actions / Run Ruff check

Ruff (D415)

src/tagstudio/core/library/alchemy/library.py:689:9: D415 First line should end with a period, question mark, or exclamation point
add_trigger = text(
"""
CREATE TRIGGER `trigger_before_insert_tag_parents` BEFORE INSERT ON `tag_parents` BEGIN
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot FOR EACH ROW...

And updates

WITH RECURSIVE
ChildTags AS (
SELECT
NEW.child_id AS tag_id
UNION
SELECT
tp.child_id AS tag_id
FROM
tag_parents tp
INNER JOIN ChildTags c ON tp.parent_id = c.tag_id
)
SELECT
RAISE (
ABORT,
'Cannot insert a tag as a child when it is already a parent'
)
WHERE
EXISTS (
SELECT
1
FROM
ChildTags
WHERE
ChildTags.`tag_id` = NEW.`parent_id`
);

END;
"""
)
try:
session.execute(add_trigger)
session.commit()
logger.info("[Library][Migration] Added trigger to prevent having a tag being its own parent")

Check failure on line 725 in src/tagstudio/core/library/alchemy/library.py

View workflow job for this annotation

GitHub Actions / Run Ruff check

Ruff (E501)

src/tagstudio/core/library/alchemy/library.py:725:101: E501 Line too long (106 > 100)
except Exception as e:
logger.error(
"[Library][Migration] Could not create trigger to prevent having a tag being its own parent!",

Check failure on line 728 in src/tagstudio/core/library/alchemy/library.py

View workflow job for this annotation

GitHub Actions / Run Ruff check

Ruff (E501)

src/tagstudio/core/library/alchemy/library.py:728:101: E501 Line too long (110 > 100)
error=e,
)
session.rollback()

def migrate_sql_to_ts_ignore(self, library_dir: Path):
# Do not continue if existing '.ts_ignore' file is found
if Path(library_dir / TS_FOLDER_NAME / IGNORE_NAME).exists():
Expand Down
Loading