Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'alembic history' ignores env.py #447

Closed
sqlalchemy-bot opened this issue Sep 6, 2017 · 12 comments
Closed

'alembic history' ignores env.py #447

sqlalchemy-bot opened this issue Sep 6, 2017 · 12 comments

Comments

@sqlalchemy-bot
Copy link

@sqlalchemy-bot sqlalchemy-bot commented Sep 6, 2017

Migrated issue, originally created by rkrzr (@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-bot
Copy link
Author

@sqlalchemy-bot sqlalchemy-bot commented Sep 6, 2017

Michael Bayer (@zzzeek) 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)

@sqlalchemy-bot
Copy link
Author

@sqlalchemy-bot sqlalchemy-bot commented Sep 6, 2017

Changes by Michael Bayer (@zzzeek):

  • removed labels: bug
  • added labels: feature
@sqlalchemy-bot
Copy link
Author

@sqlalchemy-bot sqlalchemy-bot commented Sep 6, 2017

Changes by Michael Bayer (@zzzeek):

  • added labels: command interface
@sqlalchemy-bot
Copy link
Author

@sqlalchemy-bot sqlalchemy-bot commented Sep 6, 2017

Changes by Michael Bayer (@zzzeek):

  • set milestone to "fasttrack"
@sqlalchemy-bot
Copy link
Author

@sqlalchemy-bot sqlalchemy-bot commented Sep 6, 2017

Michael Bayer (@zzzeek) wrote:

bzzt, try again

diff --git a/alembic/command.py b/alembic/command.py
index 0280528..8675005 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",
@@ -357,6 +361,8 @@ def history(config, rev_range=None, verbose=False):
                 _display_history(config, script, base, rev)
             elif base is None:
                 _display_history(config, script, rev, head)
+            else:
+                _display_history(config, script, base, head)
             return []
 
         with EnvironmentContext(
@@ -370,6 +376,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)

@sqlalchemy-bot
Copy link
Author

@sqlalchemy-bot sqlalchemy-bot commented Sep 6, 2017

@sqlalchemy-bot
Copy link
Author

@sqlalchemy-bot sqlalchemy-bot commented Sep 6, 2017

Michael Bayer (@zzzeek) wrote:

Use env in history when revision_history=true

The alembic history command will now make use of the revision
environment env.py unconditionally if the revision_environment
configuration flag is set to True. Previously, the environment would
only be invoked if the history specification were against a database-stored
revision token.

Change-Id: I2f9dc2554c8a4523f93248859587e8e276fe4d4a
Fixes: #447

4effc81

@sqlalchemy-bot
Copy link
Author

@sqlalchemy-bot sqlalchemy-bot commented Sep 6, 2017

Changes by Michael Bayer (@zzzeek):

  • changed status to closed
@sqlalchemy-bot
Copy link
Author

@sqlalchemy-bot sqlalchemy-bot commented Sep 6, 2017

rkrzr (@rkrzr) wrote:

Thanks for the quick fix! I can confirm that alembic history now works as expected.

@sqlalchemy-bot
Copy link
Author

@sqlalchemy-bot sqlalchemy-bot commented Oct 18, 2017

Mischa (@mischas) wrote:

"heads" doesn't seem to be running env.py either, with or without revision_history=true

@sqlalchemy-bot
Copy link
Author

@sqlalchemy-bot sqlalchemy-bot commented Oct 18, 2017

Michael Bayer (@zzzeek) wrote:

Probably not open a new issue please

@sqlalchemy-bot
Copy link
Author

@sqlalchemy-bot sqlalchemy-bot commented Oct 20, 2017

Michael Bayer (@zzzeek) wrote:

looking at #460 I'm wondering if I just acted too quickly here. Wouldn't it be easier to just have your PYTHONPATH set up in your alembic.ini file if such an option could be added?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.