Handling multiple .py models with alembic

Our web application is based on sqlalchemy in a pyramid structure, and we strive to use alembic to manage database migrations. A web application consists of various packages that work with a single database. This means that we have several models.py that need to be ported. I am confused as to how to handle this. I could make some progress using the following in my env.py

from pkg_a.app.models import Base as pkg_a_base
from pkg_b.app.models import Base as pkg_b_base
from pkg_c.app.models import Base as pkg_c_base

def combine_metadata(*args):
    m = MetaData()
    for metadata in args:
        for t in metadata.tables.values():
            t.tometadata(m)
    return m
target_metadata = combine_metadata(pkg_a_base, pkg_b_base, pkg_c_base)

This works great for the first time. However, if I add another model later, simply adding that there is not much to this list. I expected launch

alembic revision -m "added a new model pkg_d.models" --version-path=migrations/versions --autogenerate

will create a new version file that will have code for adding tables from pkg_d.models. But this is not so. What am I doing wrong here.

+4
1

, - (pkg_a.migrations, pkg_b.migrations ..), , , alembic.ini -n alembic, , :

[pkg_a]
# path to migration scripts
script_location = migrations_a
sqlalchemy.url = xxx

[pkg_b]
script_location = migrations_b
sqlalchemy.url = xxx

[pkg_c]
script_location = migrations_c
sqlalchemy.url = xxx

alembic revision -n pkg_a -m "added a new model pkg_a.models"

, , - , - , SQLAlchemy models.py, ? "" , , SQLAlchemy, .

+4

Source: https://habr.com/ru/post/1650816/


All Articles