Move database loading into the db module

This commit is contained in:
Danny Robson 2020-06-14 09:48:02 +10:00
parent eb88f55cef
commit 13e135491c
2 changed files with 12 additions and 5 deletions

View File

@ -121,11 +121,7 @@ if __name__ == '__main__':
args = parser.parse_args() args = parser.parse_args()
dirname = os.path.dirname(os.path.realpath(__file__)) session = db.load_db()
dbpath = os.path.realpath(os.path.join(dirname, '..', '..', 'db', 'data.sqlite'))
db = sa.create_engine(f"sqlite:///{dbpath}")
session = sa.orm.Session(db)
# logging.basicConfig() # logging.basicConfig()
# logger = logging.getLogger('sqlalchemy.engine') # logger = logging.getLogger('sqlalchemy.engine')

View File

@ -2,6 +2,8 @@ import sqlalchemy as sa
import sqlalchemy.ext.declarative.base import sqlalchemy.ext.declarative.base
import sqlalchemy.orm import sqlalchemy.orm
import os.path
############################################################################### ###############################################################################
Base = sqlalchemy.ext.declarative.declarative_base() Base = sqlalchemy.ext.declarative.declarative_base()
@ -32,3 +34,12 @@ class Task(Base):
secondaryjoin=id == depends_association.c.second_id, secondaryjoin=id == depends_association.c.second_id,
backref="parents" backref="parents"
) )
###############################################################################
def load_db() -> sa.orm.Session:
dirname = os.path.dirname(os.path.realpath(__file__))
dbpath = os.path.realpath(os.path.join(dirname, '..', '..', 'db', 'data.sqlite'))
db = sa.create_engine(f"sqlite:///{dbpath}")
return sa.orm.Session(db)