Skip to content

Commit f96ea6b

Browse files
authored
fix: always catch db mismatch (#738)
* fix: always catch db mismatch * chore: remove/modify log statements * translations: translate library version mismatch * style: fix line over col limit
1 parent 19cdd1b commit f96ea6b

File tree

3 files changed

+44
-42
lines changed

3 files changed

+44
-42
lines changed

tagstudio/resources/translations/en.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,9 @@
197197
"status.library_closing": "Closing Library...",
198198
"status.library_save_success": "Library Saved and Closed!",
199199
"status.library_search_query": "Searching Library...",
200+
"status.library_version_expected": "Expected:",
201+
"status.library_version_found": "Found:",
202+
"status.library_version_mismatch": "Library Version Mismatch!",
200203
"status.results_found": "{count} Results Found ({time_span})",
201204
"status.results": "Results",
202205
"tag_manager.title": "Library Tags",

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import structlog
88
from sqlalchemy import Dialect, Engine, String, TypeDecorator, create_engine, text
9+
from sqlalchemy.exc import OperationalError
910
from sqlalchemy.orm import DeclarativeBase
1011
from src.core.constants import RESERVED_TAG_END
1112

@@ -36,7 +37,7 @@ def make_engine(connection_string: str) -> Engine:
3637

3738

3839
def make_tables(engine: Engine) -> None:
39-
logger.info("creating db tables")
40+
logger.info("[Library] Creating DB tables...")
4041
Base.metadata.create_all(engine)
4142

4243
# tag IDs < 1000 are reserved
@@ -47,14 +48,19 @@ def make_tables(engine: Engine) -> None:
4748
result = conn.execute(text("SELECT SEQ FROM sqlite_sequence WHERE name='tags'"))
4849
autoincrement_val = result.scalar()
4950
if not autoincrement_val or autoincrement_val <= RESERVED_TAG_END:
50-
conn.execute(
51-
text(
52-
"INSERT INTO tags (id, name, color_namespace, color_slug, is_category) VALUES "
53-
f"({RESERVED_TAG_END}, 'temp', NULL, NULL, false)"
51+
try:
52+
conn.execute(
53+
text(
54+
"INSERT INTO tags "
55+
"(id, name, color_namespace, color_slug, is_category) VALUES "
56+
f"({RESERVED_TAG_END}, 'temp', NULL, NULL, false)"
57+
)
5458
)
55-
)
56-
conn.execute(text(f"DELETE FROM tags WHERE id = {RESERVED_TAG_END}"))
57-
conn.commit()
59+
conn.execute(text(f"DELETE FROM tags WHERE id = {RESERVED_TAG_END}"))
60+
conn.commit()
61+
except OperationalError as e:
62+
logger.error("Could not initialize built-in tags", error=e)
63+
conn.rollback()
5864

5965

6066
def drop_tables(engine: Engine) -> None:

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

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
selectinload,
4343
)
4444
from src.core.library.json.library import Library as JsonLibrary # type: ignore
45+
from src.qt.translations import Translations
4546

4647
from ...constants import (
4748
BACKUP_FOLDER_NAME,
@@ -295,10 +296,35 @@ def open_sqlite_library(self, library_dir: Path, is_new: bool) -> LibraryStatus:
295296
poolclass = None if self.storage_path == ":memory:" else NullPool
296297

297298
logger.info(
298-
"Opening SQLite Library", library_dir=library_dir, connection_string=connection_string
299+
"[Library] Opening SQLite Library",
300+
library_dir=library_dir,
301+
connection_string=connection_string,
299302
)
300303
self.engine = create_engine(connection_string, poolclass=poolclass)
301304
with Session(self.engine) as session:
305+
# dont check db version when creating new library
306+
if not is_new:
307+
db_version = session.scalar(
308+
select(Preferences).where(Preferences.key == LibraryPrefs.DB_VERSION.name)
309+
)
310+
311+
if not db_version or db_version.value != LibraryPrefs.DB_VERSION.default:
312+
mismatch_text = Translations.translate_formatted(
313+
"status.library_version_mismatch"
314+
)
315+
found_text = Translations.translate_formatted("status.library_version_found")
316+
expected_text = Translations.translate_formatted(
317+
"status.library_version_expected"
318+
)
319+
return LibraryStatus(
320+
success=False,
321+
message=(
322+
f"{mismatch_text}\n"
323+
f"{found_text} v{0 if not db_version else db_version.value}, "
324+
f"{expected_text} v{LibraryPrefs.DB_VERSION.default}"
325+
),
326+
)
327+
302328
make_tables(self.engine)
303329

304330
# TODO: Determine a good way of updating built-in data after updates.
@@ -337,21 +363,6 @@ def open_sqlite_library(self, library_dir: Path, is_new: bool) -> LibraryStatus:
337363
except IntegrityError:
338364
session.rollback()
339365

340-
# dont check db version when creating new library
341-
if not is_new:
342-
db_version = session.scalar(
343-
select(Preferences).where(Preferences.key == LibraryPrefs.DB_VERSION.name)
344-
)
345-
346-
if not db_version:
347-
return LibraryStatus(
348-
success=False,
349-
message=(
350-
"Library version mismatch.\n"
351-
f"Found: v0, expected: v{LibraryPrefs.DB_VERSION.default}"
352-
),
353-
)
354-
355366
for pref in LibraryPrefs:
356367
with catch_warnings(record=True):
357368
try:
@@ -377,24 +388,6 @@ def open_sqlite_library(self, library_dir: Path, is_new: bool) -> LibraryStatus:
377388
logger.debug("ValueType already exists", field=field)
378389
session.rollback()
379390

380-
db_version = session.scalar(
381-
select(Preferences).where(Preferences.key == LibraryPrefs.DB_VERSION.name)
382-
)
383-
# if the db version is different, we cant proceed
384-
if db_version.value != LibraryPrefs.DB_VERSION.default:
385-
logger.error(
386-
"DB version mismatch",
387-
db_version=db_version.value,
388-
expected=LibraryPrefs.DB_VERSION.default,
389-
)
390-
return LibraryStatus(
391-
success=False,
392-
message=(
393-
"Library version mismatch.\n"
394-
f"Found: v{db_version.value}, expected: v{LibraryPrefs.DB_VERSION.default}"
395-
),
396-
)
397-
398391
# check if folder matching current path exists already
399392
self.folder = session.scalar(select(Folder).where(Folder.path == library_dir))
400393
if not self.folder:

0 commit comments

Comments
 (0)