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.
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.