diff --git a/data/database.py b/data/database.py index dba84fa8d..c5b5e7e88 100644 --- a/data/database.py +++ b/data/database.py @@ -1,4 +1,5 @@ import string +import logging from random import SystemRandom from datetime import datetime @@ -8,10 +9,26 @@ from peewee import create_model_tables from app import app +logger = logging.getLogger(__name__) db = app.config['DB_DRIVER'](app.config['DB_NAME'], **app.config['DB_CONNECTION_ARGS']) +def connect_db(): + logger.debug('Connectin to database.') + db.connect() + + +def close_db(exc): + if not db.is_closed(): + logger.debug('Disconnecting from database.') + db.close() + + +app.before_request(connect_db) +app.teardown_request(close_db) + + class BaseModel(Model): class Meta: database = db