I'm using Flask-Migrate (Alembic) to manage SQLAlchemy database migrations. I'm working on two different branches with different migrations.
How can I do it easier? Maybe another tool that more like Django's migrations, but for Flask?
Alembic requires the chain of migrations to match the database marker for the current migration. If you create and run some migrations on a branch, then switch to another branch, the database is marked as being on a migration that no longer exists.
To work on multiple branches while using migrations, you'll need to figure out what the latest common migration on the branch you're switching to is, then downgrade to that version first. Then checkout the branch, and run any migrations that are unique to it.
For example, assume you created two branches off the "dev" branch called "feature1" and "feature2", and each have one new migration since "dev". To switch from "feature1" to "feature2":
flask db downgrade -1
.git checkout feature2
flask db upgrade
If you don't want to lose data due to downgrades that remove columns or tables, you'll need to dump and restore the database for each branch instead.
If you're working on "feature1" and merge it into "dev", you need to update "feature2" so it knows about the new migrations that were merged in. Alembic will support having multiple branches as long as all the migrations are present. Once you merge "feature2", you can generate a merge migration to consolidate the two migration branches back to one.
git checkout dev
, git merge feature1
git checkout feature2
, git merge dev
flask db upgrade
git checkout dev
, git merge feature2
flask db heads
flask db merge id1 id2
, substituting the ids from the previous step.flask db upgrade
, flask db heads
Unfortunately, this is a manual process. Alembic requires the migration chain to match the database marker, and there is currently no way around that. You may be able to write a git hook to help with this, but it's not something that already exists.