How can I ignore certain schemas with alembic --autogenerate

与世无争的帅哥 提交于 2019-12-07 08:42:55

问题


I have a library that's part of a bigger project. The library uses its own schema in a (PostgreSQL) database shared with the larger project.

I want to use alembic revision --autogenerate to only generate migrations for the library's schema and ignore changes to the tables in the main/default schema. Is there any option for doing this?

FWIW, I've tried the include_schemas=False parameter to context.configure in env.py, but it doesn't seem to do anything.


回答1:


It seems I can use include_object in conjunction with include_schemas

In alembic/env.py:

def include_object(object, name, type_, reflected, compare_to):
    if type_ == 'table' and object.schema != MY_SCHEMA:
        return False

    return True

...
context.configure(..., include_object=include_object, ...)



回答2:


Based on Oin response, finally a method which ignores tables while running db revision --autogenerate

In alembic/env.py or migrations/env.py:

def include_object(object, name, type_, reflected, compare_to):
    if (type_ == "table" and object.schema == "exclude_from_migrations"):
        return False
    else:
       return True

In alembic/env.py or migrations/env.py:

def run_migrations_online():
   ....
   context.configure(connection=connection,
                  target_metadata=target_metadata,
                  include_object = include_object,
                  process_revision_directives=process_revision_directives,
                  **current_app.extensions['migrate'].configure_args)
   ...

Now in the tables that you want to ignore:

class MyClass(db.Model):
__tablename__='my_class'
__table_args__ = {"schema": "exclude_from_migrations"}


来源:https://stackoverflow.com/questions/35342367/how-can-i-ignore-certain-schemas-with-alembic-autogenerate

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