Articles

The database migrations for the desktop application with Alembic

· 1 min readquantum

Stack: Python, PyInstaller, Sqlite, Alembic

This short post shows how to perform an application database upgrade with new version of the application. It covers also the problem of the application packaging.

Database migration are performed with Alembic library. The database is checked and upgraded (if needed) during application startup. Migration files are distributed with the release (exe/rpm) of the application.

First install the alembic library for the database migration, and init in inside your project:

$ pip install alembic
$ alembic init alembic

Update the PyInstaller .spec file to include the migration in the new package:

a = Analysis( 
    datas=[('alembic/env.py', 'alembic'), 
    ('alembic/versions/*', 'alembic/versions'), 
    ('alembic.ini', '.')],

Comment lines in generated alembic/env.py, to work properly in the programmatic database migration approach:

# from logging.config import fileConfig
...
# fileConfig(config.config_file_name)

Implement the upgrade of the database during application startup:


def upgrade_db():
    alembic_cfg = Config('alembic.ini')
    alembic_cfg.set_main_option('script_location', str(Path(get_app_global_path()) / 'alembic'))
    alembic_cfg.set_main_option('sqlalchemy.url', get_db_url())
    command.upgrade(alembic_cfg, 'head')

if __name__ == "__main__":
    upgrade_db()

This approach was used implementing the Savings project.

All articles

See also

quantumPL

QBronze

QBronze to szkolenie przygotowane przez organizację QWorld, które wprowadza w świat komputerów kwantowych. Na początku poznajemy zagadnienia matematyczne, czyli głównie operacje macierzowe, które są…

quantumPL

Technologie kwantowe konferencja UODO

Czy Quantum Act to wróżenie z fusów, czy klucz do przyszłości? Świat się nie skończy. Ale się zmieni. I jeśli nie zrobimy nic, to zostaniemy z tyłu w wyścigu, który już trwa – choć większość jeszcze…

quantumPL

Krótka historia komputerów kwantowych cz. 1

Wyobraź sobie, że jest piątek wieczór. Po całym tygodniu pracy lub nauki należy Ci się chwila relaksu. Zaparzasz herbatę, rozsiadasz się wygodnie przed komputerem i włączasz grę, żeby się odprężyć.