3

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?

Ibolit
  • 9,218
  • 7
  • 52
  • 96
  • As I am still thinking about this, a possible solution would be to keep separate "alembic root folders" in each of the app folders. And then have a separate build script to find all such folders and run commands against them. Still, doesn't seem like an elegant solution. (Just tried that, it did the same as I did manually copying the alembic folders into neighbouring projects). – Ibolit Feb 28 '20 at 09:05

1 Answers1

1

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
karuhanga
  • 3,010
  • 1
  • 27
  • 30