【问题标题】:Single Schema Postgres Alembic Autogenerate Migration单模式 Postgres Alembic 自动生成迁移
【发布时间】:2020-08-06 13:43:31
【问题描述】:

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

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

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

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()

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

【问题讨论】:

标签: alembic


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2012-06-26
    • 2020-11-05
    • 1970-01-01
    • 2013-06-16
    • 2016-05-21
    • 2018-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多