单独模式中的 Alembic 版本 table

Alembic version table in a separate schemas

我有 100 多个 alembic 版本 tables 用于 postgres 中的不同应用程序。一些应用程序使用不同的用户名进行迁移,并且可以在 postgres 中为这些应用程序迁移设置 search_path。基于数据库用户名,由于 search_path,版本 tables 是在不同的 postgres 架构中创建的。一些应用程序使用通用用户名,最终在 public 架构中出现版本 table 名称冲突,因为它们没有将 search_path 设置为特定架构。我如何启用 alembic 以使用特定的 postgres 架构来创建版本 table?

Alembic EnvironmentContext.configure 方法接受一个参数 version_table_schema,它允许您指定将版本 table 放入的模式。(Docs)

例如在你的 env.py

from alembic import context
...

def run_migrations_online():
    connectable = config.attributes.get('connection', None)
    with connectable.connect() as connection:
        context.configure(
            connection=connection,
            target_metadata=target_metadata,
            version_table='alembic_version',
            version_table_schema="my_schema",  # <-- Set the schema name here
        )
    ...