Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alembic, SQLAlchemy and multiple "apps" inside a project

Coming to SQLAlchemy from the world of Django, I would like to have a project split into several apps, where models and views are bunched together according to their purpose.

For example, I am writing a planner app, I would like to have Users and Plans to live in separate apps inside my project. I want to make sure that if I have to split this project into smaller ones, I would have the least amount of work to do possible.

I also would like to be able to generate and run migrations with one command correspondingly, so that I don't have to reconfigure my CI builds every time I add a model or an "app".

So far what I could come up with in this respect is

project structure:

my_planner
  +- users
  |  +- migrations
  |  |  +- versions
  |  |  |  +- 0000_initial.py
  |  |  +- __init__.py
  |  |  +- env.py
  |  |  +- script.py.mako
  |  +- __init__.py
  |  +- models.py
  +- plans (same structure as users)

alembic.ini:

[alembic]
script_location = alembic/migrations # this doesn't seem to matter
version_locations = users/migrations/versions plans/migrations/options

[users]
script_location = users/migrations

[plans]
script_location = plans/migrations

And then I had to place env.py files into my migrations folders and import the models I want to be included into automatic migrations.

However, when I generate my automatic migrations, I have to specify the exact "apps" for which I generate them and specify a branch.

PYTHONPATH=. alembic --name users revision -m 'initial'  --rev-id='0000' --branch-label users --autogenerate

And when I apply the migrations, I have to apply them separately for each of the branches.

Is there a way to do these things in one command correspondingly without writing my own custom scripts? Or is there another approach to managing migrations?

like image 870
Ibolit Avatar asked Jul 09 '26 19:07

Ibolit


1 Answers

Unfortunately, I've not found an out of the box way to do this. I wrote about the approach we went with here.

The gist of it is;

  • Create common migration functionality
  • Add a migrations directory with app specific information to each app(schema, metadata)
  • Add your relevant app sections to the alembic.ini file, referring to each directory
  • Use branches starting from the base to track each app's migrations separately
like image 118
Karuhanga Avatar answered Jul 11 '26 19:07

Karuhanga