Skip to content

Commit

Permalink
suppress stack traces on command errors
Browse files Browse the repository at this point in the history
Fixes #204
  • Loading branch information
miguelgrinberg committed Jun 13, 2018
1 parent 52ac599 commit 2dd45d0
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions flask_migrate/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import os
import argparse
from functools import wraps
import logging
import os
import sys
from flask import current_app
try:
from flask_script import Manager
Expand All @@ -8,9 +11,10 @@
from alembic import __version__ as __alembic_version__
from alembic.config import Config as AlembicConfig
from alembic import command

from alembic.util.exc import CommandError

alembic_version = tuple([int(v) for v in __alembic_version__.split('.')[0:3]])
log = logging.getLogger(__name__)


class _MigrateConfig(object):
Expand Down Expand Up @@ -84,6 +88,17 @@ def get_config(self, directory=None, x_arg=None, opts=None):
return self.call_configure_callbacks(config)


def catch_errors(f):
@wraps(f)
def wrapped(*args, **kwargs):
try:
f(*args, **kwargs)
except (CommandError, RuntimeError) as exc:
log.error('Error: ' + str(exc))

This comment has been minimized.

Copy link
@cancan101

cancan101 Oct 5, 2018

I think this is problematic. The way loggers are set up using the current ini file, errors logged by the flask_migrate logger are not sent to std out / err (ie they are silent). I think this is leading to a number of bug reports: #225, #220, #214 and maybe #229.

This comment has been minimized.

Copy link
@miguelgrinberg

miguelgrinberg Oct 5, 2018

Author Owner

Thanks, I'll look into it.

This comment has been minimized.

Copy link
@mark-anders

mark-anders Oct 19, 2018

I just ran into this. I was hitting an error but with no error message shown. Based on this thread, I set a breakpoint in catch_errors and was able to see that it was just that the db I was attempting to migrate hadn't been upgraded to the latest version yet.

It would be good to fix this.

This comment has been minimized.

Copy link
@miguelgrinberg

miguelgrinberg Oct 19, 2018

Author Owner

@mark-anders what version of Flask-Migrate do you have? This is fixed, try the latest and let me know if you still get silent errors.

This comment has been minimized.

Copy link
@mark-anders

mark-anders Oct 19, 2018

@miguelgrinberg - I'm using 2.2.1, which is the most recent version on pypi. Does it take a while for it to be available there?

This comment has been minimized.

Copy link
@miguelgrinberg

miguelgrinberg Oct 19, 2018

Author Owner

@mark-anders apologies, I fixed this problem a few days ago, but forgot to make the pypi release. Please try to upgrade now.

This comment has been minimized.

Copy link
@mark-anders

mark-anders Oct 20, 2018

That seems to have fixed it. Thanks!

sys.exit(1)
return wrapped


if Manager is not None:
MigrateCommand = Manager(usage='Perform database migrations')
else:
Expand All @@ -103,6 +118,7 @@ def decorator(f):
default=False,
help=("Multiple databases migraton (default is "
"False)"))
@catch_errors
def init(directory=None, multidb=False):
"""Creates a new migration repository"""
if directory is None:
Expand Down Expand Up @@ -147,6 +163,7 @@ def init(directory=None, multidb=False):
@MigrateCommand.option('-d', '--directory', dest='directory', default=None,
help=("migration script directory (default is "
"'migrations')"))
@catch_errors
def revision(directory=None, message=None, autogenerate=False, sql=False,
head='head', splice=False, branch_label=None, version_path=None,
rev_id=None):
Expand Down Expand Up @@ -186,6 +203,7 @@ def revision(directory=None, message=None, autogenerate=False, sql=False,
@MigrateCommand.option('-x', '--x-arg', dest='x_arg', default=None,
action='append', help=("Additional arguments consumed "
"by custom env.py scripts"))
@catch_errors
def migrate(directory=None, message=None, sql=False, head='head', splice=False,
branch_label=None, version_path=None, rev_id=None, x_arg=None):
"""Alias for 'revision --autogenerate'"""
Expand All @@ -204,6 +222,7 @@ def migrate(directory=None, message=None, sql=False, head='head', splice=False,
@MigrateCommand.option('-d', '--directory', dest='directory', default=None,
help=("migration script directory (default is "
"'migrations')"))
@catch_errors
def edit(directory=None, revision='current'):
"""Edit current revision."""
if alembic_version >= (0, 8, 0):
Expand All @@ -226,6 +245,7 @@ def edit(directory=None, revision='current'):
@MigrateCommand.option('-d', '--directory', dest='directory', default=None,
help=("migration script directory (default is "
"'migrations')"))
@catch_errors
def merge(directory=None, revisions='', message=None, branch_label=None,
rev_id=None):
"""Merge two revisions together. Creates a new migration file"""
Expand All @@ -252,6 +272,7 @@ def merge(directory=None, revisions='', message=None, branch_label=None,
@MigrateCommand.option('-x', '--x-arg', dest='x_arg', default=None,
action='append', help=("Additional arguments consumed "
"by custom env.py scripts"))
@catch_errors
def upgrade(directory=None, revision='head', sql=False, tag=None, x_arg=None):
"""Upgrade to a later version"""
config = current_app.extensions['migrate'].migrate.get_config(directory,
Expand All @@ -273,6 +294,7 @@ def upgrade(directory=None, revision='head', sql=False, tag=None, x_arg=None):
@MigrateCommand.option('-x', '--x-arg', dest='x_arg', default=None,
action='append', help=("Additional arguments consumed "
"by custom env.py scripts"))
@catch_errors
def downgrade(directory=None, revision='-1', sql=False, tag=None, x_arg=None):
"""Revert to a previous version"""
config = current_app.extensions['migrate'].migrate.get_config(directory,
Expand All @@ -287,6 +309,7 @@ def downgrade(directory=None, revision='-1', sql=False, tag=None, x_arg=None):
@MigrateCommand.option('-d', '--directory', dest='directory', default=None,
help=("migration script directory (default is "
"'migrations')"))
@catch_errors
def show(directory=None, revision='head'):
"""Show the revision denoted by the given symbol."""
if alembic_version >= (0, 7, 0):
Expand All @@ -304,6 +327,7 @@ def show(directory=None, revision='head'):
@MigrateCommand.option('-d', '--directory', dest='directory', default=None,
help=("migration script directory (default is "
"'migrations')"))
@catch_errors
def history(directory=None, rev_range=None, verbose=False):
"""List changeset scripts in chronological order."""
config = current_app.extensions['migrate'].migrate.get_config(directory)
Expand All @@ -321,6 +345,7 @@ def history(directory=None, rev_range=None, verbose=False):
@MigrateCommand.option('-d', '--directory', dest='directory', default=None,
help=("migration script directory (default is "
"'migrations')"))
@catch_errors
def heads(directory=None, verbose=False, resolve_dependencies=False):
"""Show current available heads in the script directory"""
if alembic_version >= (0, 7, 0):
Expand All @@ -337,6 +362,7 @@ def heads(directory=None, verbose=False, resolve_dependencies=False):
@MigrateCommand.option('-d', '--directory', dest='directory', default=None,
help=("migration script directory (default is "
"'migrations')"))
@catch_errors
def branches(directory=None, verbose=False):
"""Show current branch points"""
config = current_app.extensions['migrate'].migrate.get_config(directory)
Expand All @@ -354,6 +380,7 @@ def branches(directory=None, verbose=False):
@MigrateCommand.option('-d', '--directory', dest='directory', default=None,
help=("migration script directory (default is "
"'migrations')"))
@catch_errors
def current(directory=None, verbose=False, head_only=False):
"""Display the current revision for each database."""
config = current_app.extensions['migrate'].migrate.get_config(directory)
Expand All @@ -373,6 +400,7 @@ def current(directory=None, verbose=False, head_only=False):
@MigrateCommand.option('-d', '--directory', dest='directory', default=None,
help=("migration script directory (default is "
"'migrations')"))
@catch_errors
def stamp(directory=None, revision='head', sql=False, tag=None):
"""'stamp' the revision table with the given revision; don't run any
migrations"""
Expand Down

0 comments on commit 2dd45d0

Please sign in to comment.