Clean up dbconfig session handling

This commit is contained in:
Keith Edmunds 2022-03-19 23:24:18 +00:00
parent 75b814e26c
commit 0d865f05ac

View File

@ -4,6 +4,7 @@ import sqlalchemy
from config import Config
from contextlib import contextmanager
from log import DEBUG
from sqlalchemy.orm import (sessionmaker, scoped_session)
MM_ENV = os.environ.get('MM_ENV', 'PRODUCTION')
@ -12,7 +13,7 @@ testing = False
if MM_ENV == 'PRODUCTION':
dbname = os.environ.get('MM_PRODUCTION_DBNAME', 'musicmuster_prod')
dbuser = os.environ.get('MM_PRODUCTION_DBUSER', 'musicmuster')
dbpw = os.environ.get('MM_PRODUCTION_DBPW', 'musicmuster')
dbpw = os.environ.get('MM_PRODUCTION_DBPW', 'xxxmusicmuster')
dbhost = os.environ.get('MM_PRODUCTION_DBHOST', 'localhost')
elif MM_ENV == 'TESTING':
dbname = os.environ.get('MM_TESTING_DBNAME', 'musicmuster_testing')
@ -45,12 +46,9 @@ def Session():
file = frame.filename
function = frame.function
lineno = frame.lineno
print(f"Session acquired, {file=}, {function=}, {lineno=}")
# yield scoped_session(sessionmaker(bind=engine))
DEBUG(f"Session acquired, {file=}, {function=}, {lineno=}")
Session = scoped_session(sessionmaker(bind=engine))
yield Session
print(" Session released")
DEBUG(" Session released")
Session.commit()
print(" Session committed")
Session.close()
print(" Session closed")