Skip to content

Commit b29e612

Browse files
refactor: almost final migrations cleanup (#1456)
* fix: backup library before making any changes * refactor: inline make_tables + minor cleanup * refactor: remove unnecessary assurance Bumping the auto increment value has been done since the original sql PR, so it doesn't need to be done on migrations. See e5e7b8a. * refactor: don't blindly create all tables in the beginning The only table that has been added since DB version 6 (the earliest supported version), is the versions table in DB version 101. This commit removes the "create all tables" statement, and instead creates the versions table in the 101 migration. See 12e074b. * refactor: don't require setting library_dir to create a backup * refactor: move migrations to different file * refactor: package each migration in a class * fix: some syntax errors had slipped through * fix: allow set_version to fail, but don't commit in that case * refactor: condense imports * fix: add override decorators * refactor: move set_version to DBMigrations * refactor: remove unnecessary assignment * refactor: use _ instead of __ * fix: add missing field templates tables
1 parent 5852c59 commit b29e612

5 files changed

Lines changed: 645 additions & 492 deletions

File tree

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
from sqlalchemy import text
66

7+
from tagstudio.core.library.alchemy.fields import (
8+
DatetimeFieldTemplate,
9+
TextFieldTemplate,
10+
)
11+
712
SQL_FILENAME: str = "ts_library.sqlite"
813
JSON_FILENAME: str = "ts_library.json"
914

@@ -32,3 +37,15 @@
3237
)
3338
SELECT tag_id FROM ChildTags;
3439
""")
40+
41+
42+
DEFAULT_FIELD_TEMPLATES = (
43+
TextFieldTemplate(name="Title"),
44+
TextFieldTemplate(name="Author"),
45+
TextFieldTemplate(name="Artist"),
46+
TextFieldTemplate(name="URL"),
47+
TextFieldTemplate(name="Description", is_multiline=True),
48+
TextFieldTemplate(name="Notes", is_multiline=True),
49+
TextFieldTemplate(name="Comments", is_multiline=True),
50+
DatetimeFieldTemplate(name="Date"),
51+
)

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

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@
66
from typing import override
77

88
import structlog
9-
from sqlalchemy import Dialect, Engine, String, TypeDecorator, create_engine, text
10-
from sqlalchemy.exc import OperationalError
9+
from sqlalchemy import Dialect, String, TypeDecorator
1110
from sqlalchemy.orm import DeclarativeBase
1211

13-
from tagstudio.core.constants import RESERVED_TAG_END
14-
1512
logger = structlog.getLogger(__name__)
1613

1714

@@ -34,38 +31,3 @@ def process_result_value(self, value: str | None, dialect: Dialect):
3431

3532
class Base(DeclarativeBase):
3633
type_annotation_map = {Path: PathType}
37-
38-
39-
def make_engine(connection_string: str) -> Engine:
40-
return create_engine(connection_string)
41-
42-
43-
def make_tables(engine: Engine) -> None:
44-
logger.info("[Library] Creating DB tables...")
45-
with engine.connect() as conn:
46-
# TODO: this should instead be migrations that create the exact tables that were added in
47-
# the respective DB versions
48-
Base.metadata.create_all(conn)
49-
conn.commit()
50-
51-
# TODO: this needs to be a migration
52-
# tag IDs < 1000 are reserved
53-
# create tag and delete it to bump the autoincrement sequence
54-
# TODO - find a better way
55-
# is this the better way?
56-
result = conn.execute(text("SELECT SEQ FROM sqlite_sequence WHERE name='tags'"))
57-
autoincrement_val = result.scalar()
58-
if not autoincrement_val or autoincrement_val <= RESERVED_TAG_END:
59-
try:
60-
conn.execute(
61-
text(
62-
"INSERT INTO tags "
63-
"(id, name, color_namespace, color_slug, is_category, is_hidden) VALUES "
64-
f"({RESERVED_TAG_END}, 'temp', NULL, NULL, false, false)"
65-
)
66-
)
67-
conn.execute(text(f"DELETE FROM tags WHERE id = {RESERVED_TAG_END}"))
68-
conn.commit()
69-
except OperationalError as e:
70-
logger.error("Could not initialize built-in tags", error=e)
71-
conn.rollback()

0 commit comments

Comments
 (0)