单独模式中的 Alembic 版本表

Tej*_*sad 2 python-3.x alembic

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

Gri*_*cey 6

AlembicEnvironmentContext.configure方法采用一个参数version_table_schema,该参数允许您指定将版本表放入的架构。(文档

例如在你的 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
        )
    ...
Run Code Online (Sandbox Code Playgroud)