Setup DB properly

When I switched over to `flask run` entrypoint in b92391d177,
I forgot that the `if name==__main__` code no longer triggers.
So the SQLite in-memory database wasn't getting created for the example
app.

This moves the DB creation to a werkzeug/Flask hook that runs before the
first request to the app, so that the DB table is created when we query
it.

Also updated the test which worked fine previously, but this is more
idiomatic.
This commit is contained in:
Jeff Widman
2020-03-09 09:55:40 -07:00
parent dbea74b626
commit 70abd78e55
2 changed files with 10 additions and 6 deletions

View File

@@ -29,6 +29,11 @@ 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")
@@ -38,11 +43,6 @@ def index():
@app.route('/redirect')
def redirect_example():
response = redirect(url_for('index'))
response.set_cookie('test_cookie', '1')
return response
if __name__ == "__main__":
db.create_all()