From 42d859534a6c525f4e9c65792a1e4e8a8b73c102 Mon Sep 17 00:00:00 2001 From: Grey Li Date: Thu, 16 Nov 2023 21:08:57 +0800 Subject: [PATCH] Remove the use of before_first_request --- example/app.py | 9 ++++----- test/basic_app.py | 11 +++++------ 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/example/app.py b/example/app.py index 77c688e..cd35b9e 100644 --- a/example/app.py +++ b/example/app.py @@ -28,11 +28,6 @@ class ExampleModel(db.Model): value = db.Column(db.String(100), primary_key=True) -@app.before_first_request -def setup(): - db.create_all() - - @app.route('/') def index(): app.logger.info("Hello there") @@ -45,3 +40,7 @@ def redirect_example(): response = redirect(url_for('index')) response.set_cookie('test_cookie', '1') return response + + +with app.app_context(): + db.create_all() diff --git a/test/basic_app.py b/test/basic_app.py index 6ec1ddd..689d14a 100644 --- a/test/basic_app.py +++ b/test/basic_app.py @@ -6,7 +6,7 @@ app = Flask('basic_app') app.config['SECRET_KEY'] = 'abc123' app.config['SQLALCHEMY_RECORD_QUERIES'] = True -app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db' +app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:' # This is no longer needed for Flask-SQLAlchemy 3.0+, if you're using 2.X you'll want to define this: # app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False @@ -23,12 +23,11 @@ class Foo(db.Model): id = db.Column(db.Integer, primary_key=True) -@app.before_first_request -def setup(): - db.create_all() - - @app.route('/') def index(): Foo.query.filter_by(id=1).all() return render_template('basic_app.html') + + +with app.app_context(): + db.create_all()