这是Alembic的解决方案,用于忽略自动生成的空世代:https://alembic.sqlalchemy.org/en/latest/cookbook.html#don-t-generate-empty-migrations-with-autogenerate
当我在多数据库模板项目上使用此解决方案时,出现此错误,提示IndexError: list index out of range
。它总是在第二个引擎上发生,并且如果我只有一个引擎,则可以正常工作。
INFO [alembic.env] Migrating database engine1
INFO [alembic.runtime.migration] Context impl MySQLImpl.
INFO [alembic.runtime.migration] Will assume non-transactional DDL.
INFO [alembic.env] Migrating database engine2
INFO [alembic.runtime.migration] Context impl MySQLImpl.
INFO [alembic.runtime.migration] Will assume non-transactional DDL.
Traceback (most recent call last):
File "/venv/bin/alembic", line 11, in <module>
load_entry_point('alembic==1.4.2', 'console_scripts', 'alembic')()
File "/venv/lib/python3.6/site-packages/alembic/config.py", line 577, in main
CommandLine(prog=prog).main(argv=argv)
File "/venv/lib/python3.6/site-packages/alembic/config.py", line 571, in main
self.run_cmd(cfg, options)
File "/venv/lib/python3.6/site-packages/alembic/config.py", line 551, in run_cmd
**dict((k, getattr(options, k, None)) for k in kwarg)
File "/venv/lib/python3.6/site-packages/alembic/command.py", line 214, in revision
script_directory.run_env()
File "/venv/lib/python3.6/site-packages/alembic/script/base.py", line 489, in run_env
util.load_python_file(self.dir, "env.py")
File "/venv/lib/python3.6/site-packages/alembic/util/pyfiles.py", line 98, in load_python_file
module = load_module_py(module_id, path)
File "/venv/lib/python3.6/site-packages/alembic/util/compat.py", line 184, in load_module_py
spec.loader.exec_module(module)
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "./migrations/env.py", line 151, in <module>
run_migrations_online()
File "./migrations/env.py", line 131, in run_migrations_online
context.run_migrations(engine_name=name)
File "<string>", line 8, in run_migrations
File "/venv/lib/python3.6/site-packages/alembic/runtime/environment.py", line 846, in run_migrations
self.get_context().run_migrations(**kw)
File "/venv/lib/python3.6/site-packages/alembic/runtime/migration.py", line 509, in run_migrations
for step in self._migrations_fn(heads, self):
File "/venv/lib/python3.6/site-packages/alembic/command.py", line 190, in retrieve_migrations
revision_context.run_autogenerate(rev, context)
File "/venv/lib/python3.6/site-packages/alembic/autogenerate/api.py", line 442, in run_autogenerate
self._run_environment(rev, migration_context, True)
File "/venv/lib/python3.6/site-packages/alembic/autogenerate/api.py", line 461, in _run_environment
migration_script = self.generated_revisions[-1]
IndexError: list index out of range
这是我的alembic.ini
。
##other default config here
databases = engine1, engine2
[engine1]
sqlalchemy.url = #DB_URI/db1
[engine2]
sqlalchemy.url = #DB_URI/db2
这是我的env.py
来自具有alembic解决方案的multidb模板。
def run_migrations_online():
#from alembic solution
def process_revision_directives(context, revision, directives):
if config.cmd_opts.autogenerate:
script = directives[0]
if script.upgrade_ops.is_empty():
directives[:] = []
#other code here
try:
for name, rec in engines.items():
logger.info("Migrating database %s" % name)
context.configure(
connection=rec["connection"],
upgrade_token="%s_upgrades" % name,
downgrade_token="%s_downgrades" % name,
target_metadata=target_metadata.get(name),
process_revision_directives=process_revision_directives # solution
)
context.run_migrations(engine_name=name)
#other code here
您可以通过下面的命令并通过Alembic解决方案来复制此代码。
alembic init --template multidb .