Alembic: When autogenerating migrations how to ignore database tables by other products

半世苍凉 提交于 2019-12-06 19:40:02

问题


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.


回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!