单模式 Postgres Alembic 自动生成迁移

Single Schema Postgres Alembic Autogenerate Migration

我有一台 Postgres 服务器,其中有一个数据库。这个数据库就是我们的数据仓库。我们在此数据库中为每个软件应用程序提供一个模式。

我正在开发一个新项目,我正在使用 sqlalchemy 的 alembic 创建模式迁移。但是,由于我的数据库设置方式...看起来修订生成器的 --autogenerate 选项正在扫描数据库中的所有模式。

我找不到将检查限制为仅一个模式的选项。我发现的唯一选择是创建一个函数以传递到 alembic 上下文中的 inclue_object 参数。因此 alembic 将扫描所有模式,但仅在该函数 returns 为真时才使用 schema/tables。这不太理想,因为我有数百张桌子...所以这个过程很慢。

def include_object(object, name, type_, reflected, compare_to):
    print(object, name, type_, reflected, compare_to)
    if type_ == 'table' and object.schema != 'leads_manager':
        print('returning false')
        return False
    else:
        print('returning true')
        return True

def run_migrations_offline():
    url = get_db_uri()
    context.configure(
        url=url,
        target_metadata=target_metadata,
        include_object=include_object,
    )

    with context.begin_transaction():
        context.execute('SET search_path TO leads_manager')
        context.run_migrations()

有人知道如何在 postgres 中将 alembic 自动生成限制为仅一个模式吗?

我最终创建了一个只能访问我感兴趣的架构的用户。 Alembic --autogenerate 然后仅检查该模式,因为它无权访问任何其他模式。