0

I integrated Flask-Migrate into my project. When I'm using development mode (FLASK_ENV='development') I would normally call flask db migrate to apply changes to SQLite database. But, in testing mode (FLASK_ENV='testing') I'm using internal memory storage (sqlite:///:memory:) and it has no sense to call db migrate because it will end up throwing error. Is there some way to create "pre_execute" hook in Flask CLI to check which ENV is used before executing command? So for example if FLASK_ENV is set to testing than calling flask db init will result in aborting execution of command. I've tried something like this but it didn't work:

@click.group(cls=FlaskGroup, create_app=create_app)
def cli():
    '''
    Main entry point.
    '''
    if app.config.ENV == ENV.TESTING:
        print('Running in TESTING mode...Aborting!')
        sys.exit(1)

Question: How I can abort execution of cli command under certain FLASK_ENV setting?

Edit: I'm loading FLASK_ENV value from .env file.

devaerial
  • 2,069
  • 3
  • 19
  • 33
  • Not sure if this is related to the question or not, but you seem to be using the wrong commands. The `init` command is only used once, when the project is created. The `migrate` command is used when you need to record changes to the database. The `upgrade` command is used to upgrade your database with those changes. I think you need to explain better what you are doing and why, because it isn't clear. – Miguel Grinberg Feb 18 '19 at 21:10
  • @Miguel I'm trying to forbid execution of Flask-Migrate commands if Flask application has `FLASJ_ENV` set to `testing`. – devaerial Feb 19 '19 at 08:25
  • I got that you are trying to do that, but why? Isn't it easier to just not call the command when you don't want it to execute? Still very confused about your problem. – Miguel Grinberg Feb 19 '19 at 21:35
  • @Miguel It's not a problem to not to execute command, but I would rather like to explicitly handle error and show message that explains why command is failing to execute. If developer will only see stacktrace message he will be confused and won't understand the cause of error right away. – devaerial Feb 20 '19 at 08:04
  • Still confused. Now you mention a stack trace, for the first time. Are you getting errors? Why didn't you include that in your question? Seems important. – Miguel Grinberg Feb 20 '19 at 17:57
  • I reread your question, not I see that you linked to an issue with an error, so I suppose that is what you are talking about when you mention a stack trace. But the problem in that issue you linked was not initializing the Flask-Migrate extension appropriately. You should never receive an error like that, even if you use an in-memory SQLite database. Even though it obviously makes no sense to use Flask-Migrate in such case. – Miguel Grinberg Feb 20 '19 at 18:01

1 Answers1

0

Okay, maybe at first I tried to solve problem with wrong approach, but I finally found way to deal with an error mentioned in my question. Because I load value of FLASK_ENV from file I need manually change it every time I want to switch environment. So what I did is I modified my test CLI command to set value of FLASK_ENV to testing every time before executing pytest:

@click.command()
def test():
    '''
    Run tests.
    '''
    os.environ['FLASK_ENV'] = ENV.TESTING
    pytest.main(['--rootdir', './tests'])

Now even if FLASK_ENV set to development in .env file I still can run tests in testing mode without changing value in the file.

devaerial
  • 2,069
  • 3
  • 19
  • 33