Исключение при игнорировании пустых миграций с автогенерацией - PullRequest
0 голосов
/ 31 марта 2020

Это решение от Alembi c для игнорирования пустых поколений с автогенерацией: https://alembic.sqlalchemy.org/en/latest/cookbook.html#don -t-generate-empty-migrations-with-autogenerate

Когда я использую это Решение в моем проекте на основе multidb, я получаю эту ошибку, говоря 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 из multidb шаблон w / alembi c решение.

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

Вы можете повторить этот код с помощью команды ниже и выполнив решение из Alembi c.

 alembic init --template multidb .
...