weixin_39890652 2020-11-30 11:37
浏览 0

'alembic history' ignores env.py

Migrated issue, originally created by rkrzr ()

We rely on env.py to set the correct PYTHONPATH, so that we can import our modules in an alembic migration. This works for all commands (e.g. alembic current, alembic upgrade, alembic downgrade), except for alembic history.

For alembic history it fails with: ModuleNotFoundError: No module named 'bla'

It seems that alembic history does not read in env.py.

The workaround for this is to run PYTHONPATH=. alembic history, which works correctly.

该提问来源于开源项目:sqlalchemy/alembic

  • 写回答

12条回答 默认 最新

  • weixin_39890652 2020-11-30 11:37
    关注

    Michael Bayer () wrote:

    make sure you have revision_environment=True in your alembic.ini and let me know if this patch provides everything:

    
    diff --git a/alembic/command.py b/alembic/command.py
    index 0280528..503c6a9 100644
    --- a/alembic/command.py
    +++ b/alembic/command.py
    @@ -342,6 +342,10 @@ def history(config, rev_range=None, verbose=False):
         else:
             base = head = None
    
    +    environment = util.asbool(
    +        config.get_main_option("revision_environment")
    +    )
    +
         def _display_history(config, script, base, head):
             for sc in script.walk_revisions(
                     base=base or "base",
    @@ -370,6 +374,8 @@ def history(config, rev_range=None, verbose=False):
             _display_history_w_current(config, script, head=head)
         elif head == "current":
             _display_history_w_current(config, script, base=base)
    +    elif environment:
    +        _display_history_w_current(config, script, base, head)
         else:
             _display_history(config, script, base, head)
    
    
    评论

报告相同问题?