Skip to content

fix: drop type_key column from text_fields and datetime_fields tables#1370

Open
CyanVoxel wants to merge 3 commits into
mainfrom
fix-1367
Open

fix: drop type_key column from text_fields and datetime_fields tables#1370
CyanVoxel wants to merge 3 commits into
mainfrom
fix-1367

Conversation

@CyanVoxel
Copy link
Copy Markdown
Member

Summary

This PR fixes #1367 by dropping the leftover type_key columns from the text_fields and datetime_fields tables. Because SQLite does not allow for the removal of columns with foreign key constraints, I've had to create new versions of these tables without the columns and copy the data from the old tables to the new ones, as outlined in the SQLite documentation.

I've also made a related change to enforce the ordering of columns inside of both of these tables. SQLAlchemy by default orders these columns strangely as it combines fields from the base fields definition, and I did not wish to canonize that ordering in raw SQL statements for the migration.

Tasks Completed

  • Platforms Tested:
    • Windows x86
    • Windows ARM
    • macOS x86
    • macOS ARM
    • Linux x86
    • Linux ARM
  • Tested For:
    • Basic functionality
    • PyInstaller executable

@CyanVoxel CyanVoxel added this to the Alpha v9.6.0 milestone May 13, 2026
@CyanVoxel CyanVoxel added the Priority: Critical An issue that requires immediate attention label May 13, 2026
@CyanVoxel CyanVoxel added TagStudio: Library Relating to the TagStudio library system Status: Review Needed A review of this is needed Type: Fix A fix for a bug, typo, or other issue labels May 13, 2026
@CyanVoxel CyanVoxel moved this to 🏓 Ready for Review in TagStudio Development May 13, 2026
Copy link
Copy Markdown
Collaborator

@Computerdores Computerdores left a comment

Choose a reason for hiding this comment

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

haven't tested so far but found one major issue

Comment on lines +891 to +917
logger.info("[Library][Migration][201] Dropping type_key from text_fields table...")
session.execute(text("ALTER TABLE text_fields RENAME TO text_fields_old"))
session.flush()
session.execute(create_text_fields_table)
session.flush()
session.execute(
text("""
INSERT INTO text_fields (id, name, entry_id, value, is_multiline)
SELECT id, name, entry_id, value, is_multiline
FROM text_fields_old
""")
)
session.execute(text("DROP TABLE text_fields_old"))

logger.info("[Library][Migration][201] Dropping type_key from datetime_fields table...")
session.execute(text("ALTER TABLE datetime_fields RENAME TO datetime_fields_old"))
session.flush()
session.execute(create_datetime_fields_table)
session.flush()
session.execute(
text("""
INSERT INTO datetime_fields (id, name, entry_id, value)
SELECT id, name, entry_id, value
FROM datetime_fields_old
""")
)
session.execute(text("DROP TABLE datetime_fields_old"))
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

According to the sqlite docs this can lead to DB corruption. The docs also describe an alternate way of the same complexity that should work fine though

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ahh I see, I missed the importance of this before. I've changed it to now use the recommended renaming ordering
image

@Computerdores Computerdores moved this from 🏓 Ready for Review to 👀 In review in TagStudio Development May 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Priority: Critical An issue that requires immediate attention Status: Review Needed A review of this is needed TagStudio: Library Relating to the TagStudio library system Type: Fix A fix for a bug, typo, or other issue

Projects

Status: 👀 In review

Development

Successfully merging this pull request may close these issues.

[Bug]: Can't Add Fields to Entries in Migrated Libraries

2 participants