如何使用alembic --autogenerate忽略某些模式

Oin*_*Oin 4 python sqlalchemy database-migration alembic

我有一个图书馆,它是一个更大项目的一部分.该库在与较大项目共享的(PostgreSQL)数据库中使用自己的模式.

我想用来alembic revision --autogenerate只生成库模式的迁移,并忽略对main/default模式中表的更改.这样做有什么选择吗?

FWIW,我已经include_schemas=False在env.py中尝试了context.configure 的参数,但它似乎没有做任何事情.

Oin*_*Oin 9

好像我可以include_object配合使用include_schemas

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, ...)
Run Code Online (Sandbox Code Playgroud)


小智 9

基于 Oin 响应,最后一种在运行 db revision --autogenerate 时忽略表的方法

在 alembic/env.py 或 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
Run Code Online (Sandbox Code Playgroud)

在 alembic/env.py 或 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)
   ...
Run Code Online (Sandbox Code Playgroud)

现在在您要忽略的表中:

class MyClass(db.Model):
__tablename__='my_class'
__table_args__ = {"schema": "exclude_from_migrations"}
Run Code Online (Sandbox Code Playgroud)