fix: some syntax errors had slipped through

This commit is contained in:
Jann Stute
2026-07-23 22:09:38 +02:00
parent eef0da6901
commit 75c586db79
2 changed files with 51 additions and 44 deletions
@@ -14,6 +14,7 @@ from os import makedirs
from pathlib import Path
from typing import TYPE_CHECKING
import sqlalchemy
import structlog
from humanfriendly import format_timespan # pyright: ignore[reportUnknownVariableType]
from sqlalchemy import (
@@ -1746,6 +1747,49 @@ class Library:
)
session.add(parent_tag)
def get_version(self, key: str) -> int:
"""Get a version value from the DB.
Args:
key(str): The key for the name of the version type to set.
"""
return Library._get_version(self.engine, key)
@staticmethod
def _get_version(engine, key: str) -> int:
with Session(engine) as session:
engine = sqlalchemy.inspect(engine)
try:
# "Version" table added in DB_VERSION 101
if engine and engine.has_table("versions"):
version = session.scalar(select(Version).where(Version.key == key))
assert version
return version.value
# NOTE: The "Preferences" table has been depreciated as of TagStudio 9.5.4
# and is set to be removed in a future release.
else:
return int(
unwrap(
session.scalar(
text("SELECT value FROM preferences WHERE key == 'DB_VERSION'")
)
)
)
except Exception:
return 0
@staticmethod
def _set_version(session: Session, key: str, value: int) -> None:
"""Set a version value to the DB.
Args:
session(Session): The SQLAlchemy DB Session to use.
key(str): The key for the name of the version type to set.
value(int): The version value to set.
"""
# Insert if key has no value yet, otherwise update the value
session.merge(Version(key=key, value=value))
def mirror_entry_fields(self, entries: list[Entry]) -> None:
"""Mirror fields among multiple Entry items."""
all_fields: set[BaseField] = set()
@@ -2,11 +2,9 @@
# SPDX-License-Identifier: MIT
from abc import abstractmethod
from collections.abc import Callable
from pathlib import Path
import sqlalchemy
import structlog
import ujson
from sqlalchemy import (
@@ -59,7 +57,6 @@ class DBMigration:
version: int = None # pyright: ignore[reportAssignmentType]
initial_version: int | None = None
@abstractmethod
@classmethod
def run(cls, session: Session, library_dir: Path, fmt_log: Callable[[str], str]):
raise NotImplementedError
@@ -67,12 +64,14 @@ class DBMigration:
class DBMigrations:
def __init__(self, library_dir: Path, engine: Engine) -> None:
from tagstudio.core.library.alchemy.library import Library
self.library_dir = library_dir
self.engine = engine
# Don't check DB version when creating new library
self.loaded_db_version = self.__get_version(DB_VERSION_CURRENT_KEY)
self.initial_db_version = self.__get_version(DB_VERSION_INITIAL_KEY)
self.loaded_db_version = Library._get_version(engine, DB_VERSION_CURRENT_KEY)
self.initial_db_version = Library._get_version(engine, DB_VERSION_INITIAL_KEY)
# ======================== Library Database Version Checking =======================
# DB_VERSION 6 is the first supported SQLite DB version.
@@ -101,6 +100,8 @@ class DBMigrations:
return self.loaded_db_version < DB_VERSION
def run(self):
from tagstudio.core.library.alchemy.library import Library
# migrate DB step by step from one version to the next
# (migration_method, db_version, initial_db_version)
migrations: list[type[DBMigration]] = [
@@ -131,7 +132,7 @@ class DBMigrations:
lambda msg, v=migration.version: f"[Library][Migration][{v}] {msg}",
)
self.loaded_db_version = migration.version
self.__set_version(session, DB_VERSION_CURRENT_KEY, migration.version)
Library._set_version(session, DB_VERSION_CURRENT_KEY, migration.version)
session.commit()
logger.info(f"[Library][Migration][{migration.version}] Completed DB Migration")
@@ -140,44 +141,6 @@ class DBMigrations:
)
logger.info(f"[Library][Migration] Library migrated to DB version {DB_VERSION}")
def __get_version(self, key: str) -> int:
"""Get a version value from the DB.
Args:
key(str): The key for the name of the version type to set.
"""
with Session(self.engine) as session:
engine = sqlalchemy.inspect(self.engine)
try:
# "Version" table added in DB_VERSION 101
if engine and engine.has_table("versions"):
version = session.scalar(select(Version).where(Version.key == key))
assert version
return version.value
# NOTE: The "Preferences" table has been depreciated as of TagStudio 9.5.4
# and is set to be removed in a future release.
else:
return int(
unwrap(
session.scalar(
text("SELECT value FROM preferences WHERE key == 'DB_VERSION'")
)
)
)
except Exception:
return 0
def __set_version(self, session: Session, key: str, value: int) -> None:
"""Set a version value to the DB.
Args:
session(Session): The SQLAlchemy DB Session to use.
key(str): The key for the name of the version type to set.
value(int): The version value to set.
"""
# Insert if key has no value yet, otherwise update the value
session.merge(Version(key=key, value=value))
class MigrationTo7(DBMigration):
version = 7