Skip to content

Commit

Permalink
Remove the use of before_first_request (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
greyli authored Nov 17, 2023
2 parents f959951 + 42d8595 commit 2b8bf9c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
9 changes: 4 additions & 5 deletions example/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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()
11 changes: 5 additions & 6 deletions test/basic_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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()

0 comments on commit 2b8bf9c

Please sign in to comment.