I am using SQLite as an application file format (see here for why you would want to do this) for my PySide-based desktop application. That is, when a user uses my app, their
I'm not using Flask so I couldn't make use of the Flask-Alembic library that was already recommended. Instead after quite a bit of tinkering I coded up the following short function to run all of the applicable migrations. I keep all of my alembic-related files under a submodule (folder) called migrations. I actually keep the alembic.ini
together with the env.py
, which is perhaps a little bit unorthodox. Here's a snippet from my alembic.ini
file to adjust for that:
[alembic]
script_location = .
Then I added the following file in the same directory and named it run.py
. But wherever you keep your scripts, all you'd need to do is modify the code below to point to the correct paths:
from alembic.command import upgrade
from alembic.config import Config
import os
def run_sql_migrations():
# retrieves the directory that *this* file is in
migrations_dir = os.path.dirname(os.path.realpath(__file__))
# this assumes the alembic.ini is also contained in this same directory
config_file = os.path.join(migrations_dir, "alembic.ini")
config = Config(file_=config_file)
config.set_main_option("script_location", migrations_dir)
# upgrade the database to the latest revision
upgrade(config, "head")
Then with that run.py
file in place, it allows me to do this in my main code:
from mymodule.migrations.run import run_sql_migrations
run_sql_migrations()