2

I have got this weird issue I ran into when downgrading my migration versions in Flask using Flask-Migrate.

I have this merged head revision which happened from branching and an alembic merge heads.

"""empty message

Revision ID: 3ad527666180
Revises: ecd1f453d08e, 7112dcc79711
Create Date: 2021-11-11 12:17:31.973196

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '3ad527666180'
down_revision = ('7112dcc79711', 'ecd1f453d08e')
branch_labels = None
depends_on = None


def upgrade():
    pass


def downgrade():
    pass

When trying to downgrade to preceding revision which is 7112dcc79711, I get this error

docker exec -it users python manage.py db downgrade
/usr/local/lib/python3.7/site-packages/authlib/integrations/flask_oauth2/authorization_server.py:73: AuthlibDeprecationWarning: Define "get_jwt_config" in OpenID Connect grants
It will be compatible before version 1.0.
  deprecate('Define "get_jwt_config" in OpenID Connect grants', '1.0')
/usr/local/lib/python3.7/site-packages/authlib/integrations/flask_oauth2/authorization_server.py:73: AuthlibDeprecationWarning: Define "get_jwt_config" in OpenID Connect grants
It will be compatible before version 1.0.
  deprecate('Define "get_jwt_config" in OpenID Connect grants', '1.0')
INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO  [alembic.runtime.migration] Will assume transactional DDL.
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/alembic/script/base.py", line 227, in _catch_revision_errors
    yield
  File "/usr/local/lib/python3.7/site-packages/alembic/script/base.py", line 457, in _downgrade_revs
    for script in revs
  File "/usr/local/lib/python3.7/site-packages/alembic/script/base.py", line 454, in <listcomp>
    migration.MigrationStep.downgrade_from_script(
  File "/usr/local/lib/python3.7/site-packages/alembic/script/revision.py", line 798, in iterate_revisions
    assert_relative_length=assert_relative_length,
  File "/usr/local/lib/python3.7/site-packages/alembic/script/revision.py", line 1295, in _collect_downgrade_revisions
    assert_relative_length=assert_relative_length,
  File "/usr/local/lib/python3.7/site-packages/alembic/script/revision.py", line 1154, in _parse_downgrade_target
    no_overwalk=assert_relative_length,
  File "/usr/local/lib/python3.7/site-packages/alembic/script/revision.py", line 1069, in _walk
    raise RevisionError("Ambiguous walk")
alembic.script.revision.RevisionError: Ambiguous walk

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 7, in <module>
    cli()
  File "/usr/local/lib/python3.7/site-packages/click/core.py", line 764, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/flask/cli.py", line 380, in main
    return AppGroup.main(self, *args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/click/core.py", line 717, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.7/site-packages/click/core.py", line 1137, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/lib/python3.7/site-packages/click/core.py", line 1137, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/lib/python3.7/site-packages/click/core.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.7/site-packages/click/core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/click/decorators.py", line 17, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/flask/cli.py", line 257, in decorator
    return __ctx.invoke(f, *args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/click/core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/flask_migrate/cli.py", line 152, in downgrade
    _downgrade(directory, revision, sql, tag, x_arg)
  File "/usr/local/lib/python3.7/site-packages/flask_migrate/__init__.py", line 282, in downgrade
    command.downgrade(config, revision, sql=sql, tag=tag)
  File "/usr/local/lib/python3.7/site-packages/alembic/command.py", line 366, in downgrade
    script.run_env()
  File "/usr/local/lib/python3.7/site-packages/alembic/script/base.py", line 563, in run_env
    util.load_python_file(self.dir, "env.py")
  File "/usr/local/lib/python3.7/site-packages/alembic/util/pyfiles.py", line 92, in load_python_file
    module = load_module_py(module_id, path)
  File "/usr/local/lib/python3.7/site-packages/alembic/util/pyfiles.py", line 108, in load_module_py
    spec.loader.exec_module(module)  # type: ignore
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "migrations/env.py", line 87, in <module>
    run_migrations_online()
  File "migrations/env.py", line 80, in run_migrations_online
    context.run_migrations()
  File "<string>", line 8, in run_migrations
  File "/usr/local/lib/python3.7/site-packages/alembic/runtime/environment.py", line 851, in run_migrations
    self.get_context().run_migrations(**kw)
  File "/usr/local/lib/python3.7/site-packages/alembic/runtime/migration.py", line 608, in run_migrations
    for step in self._migrations_fn(heads, self):
  File "/usr/local/lib/python3.7/site-packages/alembic/command.py", line 355, in downgrade
    return script._downgrade_revs(revision, rev)
  File "/usr/local/lib/python3.7/site-packages/alembic/script/base.py", line 457, in _downgrade_revs
    for script in revs
  File "/usr/local/lib/python3.7/contextlib.py", line 130, in __exit__
    self.gen.throw(type, value, traceback)
  File "/usr/local/lib/python3.7/site-packages/alembic/script/base.py", line 261, in _catch_revision_errors
    raise util.CommandError(err.args[0]) from err
alembic.util.exc.CommandError: Ambiguous walk

What is the safe way to get past this issue?

I have tried removing one of the revisions which come from a production branch, leaving the other which tallies with my development branch and it didn't produce the error.

Is there an automatic and more feasible way to just downgrade without this issue?

davidism
  • 121,510
  • 29
  • 395
  • 339
A. Josh
  • 41
  • 5

1 Answers1

0

When I got such an error I tried to find another close revision that will work In this case:

85c43587349b -> 69914284f0b4,
3a9ce166afbb -> 85c43587349b,
885269456833, 3a9ce166afbb -> a42162b3f858 (mergepoint)
3a9ce166afbb -> 885269456833,
f725b3be6238 -> 3a9ce166afbb (branchpoint)
6c164d07441f -> f725b3be6238

When I run alembic downgrade -1 it failed and also alembic downgrade f725b3be6238 But when I tried another version it worked alembic downgrade 3a9ce166afbb

Oded BD
  • 2,788
  • 27
  • 30