Skip to content
Open
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
6 changes: 4 additions & 2 deletions src/tagstudio/core/library/alchemy/migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,15 +445,17 @@ def run(cls, conn: Connection, library_dir: Path, fmt_log: LoggingMethod):
""")

# Add field templates tables
# NOTE: v9.6.0-9.6.2 created these empty tables before migrating, so backup library files
# or libraries that failed mid-migration may already have them.
conn.execute("""
CREATE TABLE text_field_templates (
CREATE TABLE IF NOT EXISTS text_field_templates (
id INTEGER NOT NULL PRIMARY KEY,
is_multiline BOOLEAN NOT NULL,
name VARCHAR NOT NULL
)
""")
conn.execute("""
CREATE TABLE datetime_field_templates (
CREATE TABLE IF NOT EXISTS datetime_field_templates (
id INTEGER NOT NULL PRIMARY KEY,
name VARCHAR NOT NULL
)
Expand Down
34 changes: 34 additions & 0 deletions tests/core/library/test_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,37 @@ def test_library_migrations(path: str):
raise (e)
finally:
shutil.rmtree(temp_path)


def test_migration_with_existing_field_template_tables(tmp_path: Path):
"""DB_VERSION 104 library that already has the (empty) field template tables.

v9.6.0-9.6.2 created these empty tables before migrating, so backup library files
or libraries that failed mid-migration may already have them.

This tests for a specific scenario where a backup library file from a
v9.5.6 (DB102) -> v9.6.2 (DB300) migration is opened in a newer version.
Historically this would cause breakage in v9.6.3 (see issue #1500).
"""
fixture = CWD.parents[2] / FIXTURES / "issue-1500" / TS_FOLDER_NAME / SQL_FILENAME
(tmp_path / TS_FOLDER_NAME).mkdir()
shutil.copy(fixture, tmp_path / TS_FOLDER_NAME / SQL_FILENAME)

library = Library()
try:
assert library.open_library(library_dir=tmp_path).success
expected = ["Title", "Author", "Artist", "URL", "Description", "Notes", "Comments", "Date"]
assert [t.name for t in library.field_templates] == expected

assert library.entries_count == 2
entry = library.get_entry_full(entry_id=2)
assert entry
assert {f.name: f.value for f in entry.text_fields} == {
"Title": "Mario",
"Description": "This is a cat.",
}
assert {f.name: f.value for f in entry.datetime_fields} == {
"Date": None,
}
finally:
library.close()
Binary file not shown.
Loading