单独架构中的Alembic版本表

时间:2019-03-07 20:50:13

标签: python-3.x alembic

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

1 个答案:

答案 0 :(得分:1)

Alembic EnvironmentContext.configure方法采用参数version_table_schema,该参数允许您指定用于放置版本表的模式。(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
        )
    ...