我有一个 Flask 应用程序,并试图在单个数据库中使用多个模式使其成为多租户应用程序。
当需要对数据库进行更改时,例如添加列、添加表和其他更改。我需要通过每个迁移到模式。我改变了我的 migrations/env.py
如下
def run_migrations_online():
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
engine = engine_from_config(
config.get_section(config.config_ini_section),
prefix='sqlalchemy.',
poolclass=pool.NullPool)
# schemas = set([prototype_schema,None])
connection = engine.connect()
context.configure(
connection=connection,
target_metadata=target_metadata,
include_schemas=True, #schemas,
# include_object=include_schemas([None,prototype_schema])
include_object=include_schemas([None])
)
try:
domains = ['public', 'test', 'some_schema_name']
for domain in domains:
connection.execute('set search_path to "{}", public'.format(domain))
with context.begin_transaction():
context.run_migrations()
finally:
connection.close()
迁移仅影响数组中的第一个架构。此处仅迁移 public
。我需要跨所有架构迁移。