I am using Alembic to manage migrations for a database. The same database is used by multiple Python packages and each of them have their own migration paths.
How I can tell Alembic to ignore tables from other packages when generating automatic migrations? For example when I run:
alembic -c development.ini revision --autogenerate -m "Initial migration"
My migration Python file contains drop tables for other packages (not in the current Alembic env.py):
def upgrade():
### commands auto generated by Alembic - please adjust! ###
op.drop_table('table_from_another_package`)
I can manually edit migration file and remove drop_table()
and create_table()
entries, but this is manual error prone work. I'd rather avoid generating them in the first place.
Full control over what objects autogenerate considers is here.
If you are trying to run autogenerate in such a way that it considers only individual MetaData objects at a time you probably want to add customization in your env.py file that takes advantage of the "X" argument. Use this argument to receive which sub-component you want to work on, and consult that within your include_object
function to look at just the objects that are relevant to that sub-component.
来源:https://stackoverflow.com/questions/31196631/alembic-when-autogenerating-migrations-how-to-ignore-database-tables-by-other-p