【问题标题】:Why does this CLI app that I've written with the Click library not working properly?为什么我用 Click 库编写的这个 CLI 应用程序不能正常工作?
【发布时间】:2020-06-18 08:27:24
【问题描述】:

我已经创建了这个 CLI 应用程序来初始化 db 等。我什至已经为 db 创建了一个应用程序上下文,但是我遇到了一个错误。

请注意,我使用的是dockerdocker-compose。我知道这个错误与我们有多个应用程序并且数据库不知道要使用哪个应用程序的时间有关。但我不知道如何解决这个问题。

    from click import group, command, option, pass_context

    from sqlalchemy_utils import database_exists, create_database

    from call.app import create_app
    from call.extensions import db
    from call.blueprints.user.models import User

    # Create an app context for the database connection.
    app = create_app()
    db.init_app(app)


    @group()
    def cli():
        """ Run SQLite3 related tasks. """
        pass


    @command()
    @option('--with-testdb/--no-with-testdb', default=False,
                  help='Create a test db too?')
    def init(with_testdb):
        """
        Initialize the database.

        :param with_testdb: Create a test database
        :return: None
        """
        with app.app_context():
            db.drop_all()
            db.create_all()

            if with_testdb:
                db_uri = '{0}_test'.format(app.config['SQLALCHEMY_DATABASE_URI'])

                if not database_exists(db_uri):
                    create_database(db_uri)

        return None


    @command()
    def seed():
        """
        Seed the database with an initial user.

        :return: User instance
        """
        with app.app_context():
            if User.find_by_identity(app.config['SEED_ADMIN_EMAIL']) is not None:
                return None

            params = {
                'role': 'admin',
                'email': app.config['SEED_ADMIN_EMAIL'],
                'password': app.config['SEED_ADMIN_PASSWORD']
            }

        return User(**params).save()


    @command()
    @option('--with-testdb/--no-with-testdb', default=False,
                  help='Create a test db too?')
    @pass_context
    def reset(ctx, with_testdb):
        """
        Init and seed automatically.

        :param with_testdb: Create a test database
        :return: None
        """
        with app.app_context():
            print(ctx)
            ctx.invoke(init, with_testdb=with_testdb)
            ctx.invoke(seed)

        return None


    cli.add_command(init)
    cli.add_command(seed)
    cli.add_command(reset)

这是错误:

/usr/local/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py:813: UserWarning: Neither SQLALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set. Defaulting SQLALCHEMY_DATABASE_URI to "sqlite:///:memory:".
  'Neither SQLALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set. '
/usr/local/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py:834: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True or False to suppress this warning.
  'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
/usr/local/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py:834: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True or False to suppress this warning.
  'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '

【问题讨论】:

    标签: docker docker-compose click flask-sqlalchemy flask-login


    【解决方案1】:

    错误提示app.config['SQLALCHEMY_DATABASE_URI']不可用。

    我建议将其设置为 docker compose 文件中的环境变量。查看烧瓶文档。例子: 码头工人撰写:

    web:
      environment:
        - SECRET_KEY=123
    

    应用:

    import os
    
    _mail_enabled = os.environ.get("MAIL_ENABLED", default="true")
    MAIL_ENABLED = _mail_enabled.lower() in {"1", "t", "true"}
    
    SECRET_KEY = os.environ.get("SECRET_KEY")
    
    if not SECRET_KEY:
        raise ValueError("No SECRET_KEY set for Flask application")
    

    【讨论】:

    • 您的解决方案有效。我认为它无法加载项目设置。
    猜你喜欢
    • 2020-09-19
    • 2016-09-07
    • 2020-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多