Skip to content

refactor: split out sql migrations from library#1432

Draft
Computerdores wants to merge 27 commits into
mainfrom
refactor-split-out-sql-migrations
Draft

refactor: split out sql migrations from library#1432
Computerdores wants to merge 27 commits into
mainfrom
refactor-split-out-sql-migrations

Conversation

@Computerdores

@Computerdores Computerdores commented Jul 5, 2026

Copy link
Copy Markdown
Collaborator

Summary

Currently creation of newly libraries, opening of existing libraries and migration of old libraries are all intertwined in the Library.open_sqlite_library method.
In order to make this code more maintainable, I want to split it such that:

  • open library triggers migration if necessary and then opens normally either way
  • creation of new libraries is handled separately
  • the migration code
    • is outside of the Library class
    • doesn't use SQLAlchemy
    • is modular with a Migration class that has a list of all migrations (each migration would be a separate class with some metadata)

Since this will inevitably end up being a rather large refactor, I will endeavor to make commits that can be independently reviewed and be found to make sense.
This way the reviewer can review in smaller chunks.
In order to assist with this, I plan to add descriptions to the commit when I think it is necessary or helpful for understanding the intention behind the commit.
Also, I won't force-push on this PR at all, so reviewing the existing progress while the PR is still a draft and in progress shouldn't be a problem.

I tested each migration on this test library: ts_library.sqlite.zip

Tasks Completed

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

Functionality is entirely unchanged, but entire method is duplicated.
Removing dead code from either copy is next.
@Computerdores Computerdores moved this to 🚧 In progress in TagStudio Development Jul 5, 2026
@Computerdores Computerdores added Type: Refactor Code that needs to be restructured or cleaned up TagStudio: Library Relating to the TagStudio library system labels Jul 5, 2026
@Computerdores Computerdores self-assigned this Jul 5, 2026
Comment thread src/tagstudio/core/library/alchemy/library.py Outdated
Assurance 1 was introduced with library version 101, specifically commmit 12e074b.
Thus all libraries created on version 101 and above already fullfill it and don't need it.
Furthermore, it only fails if the library didn't need the assurance, meaning the try-except and warning catching can be removed
All of these were introduced to account for the differences between new libraries and existing ones,
which makes them unnecessary after this split.
The folder assurance has been present since the very first SQL commit e5e7b8a,
and thus only has an effect when the library is moved, meaning that is semantically not part of the migrations.
Assurance 3 was introduced in commit 47c3d53
shortly (24h 20min) after the DB version had been bumped to 200.
Since it was also included in the first release with DB version 200,
all libraries created on DB version 200 and above
can be presumed to have already had this applied on creation.
Moving the assurance after the migrations 7, 8, 9, and 100 is fine because it doesn't affect those.
Since every migration migrates the library to a certain DB_VERSION, we can simply update the library version after such a successfull migration.
This way if a migration fails, the previous migrations won't be rerun the next time the library is opened.
None of the content of the migrations is changed, only the `with session:` statements are removed.
Also removes various try-except statements in the migrations.
These were introduced to catch the case where the migration is run twice due to a later migration failing,
this is not necessary anymore as every migration will only be executed at most once per library.
DB Migration 8 was previously adding all colors except for the neon ones,
however, all but three of those have been around since DB version 4,
meaning that they don't need to be added at all (which caused the tests to fail).
@Computerdores

Copy link
Copy Markdown
Collaborator Author

Ok, the spaghetti code has been largely untangled.
And from what I have found I only made one small semantics mistake that wasn't in the migration code and would have only affected the tests, so I am rather happy in that regard so far.

By default in SQLAlchemy schema changes are automatically committed,
even if auto-commit is turned off.
This commit turns off auto-commit completely,
which caused some schema to not be applied correctly,
but those were fixed here as well.
@Computerdores

Copy link
Copy Markdown
Collaborator Author

@CyanVoxel I have now tested the full chain of migrations step by step and fixed two issues.
The one fixed in 81734c1 was actually already present before this PR...

@CyanVoxel CyanVoxel moved this from 🚧 In progress to 👀 In review in TagStudio Development Jul 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

TagStudio: Library Relating to the TagStudio library system Type: Refactor Code that needs to be restructured or cleaned up

Projects

Status: 👀 In review

Development

Successfully merging this pull request may close these issues.

2 participants