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.

4

1 回答 1

0

好的,也许一开始我试图用错误的方法解决问题,但我终于找到了处理问题中提到的错误的方法。因为我从文件中加载值,FLASK_ENV所以每次我想切换环境时都需要手动更改它。所以我所做的是我修改了我的测试 CLI 命令以在每次执行之前将值FLASK_ENV设置testingpytest

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

现在,即使在文件中FLASK_ENV设置为,我仍然可以在模式下运行测试而无需更改文件中的值。development.envtesting

于 2019-02-20T08:53:10.613 回答