mirror of
https://pagure.io/fm-orchestrator.git
synced 2026-04-13 23:40:02 +08:00
Give the Database a context manager interface.
This commit is contained in:
2
rida.py
2
rida.py
@@ -50,7 +50,7 @@ app.config.from_envvar("RIDA_SETTINGS", silent=True)
|
||||
conf = rida.config.from_file("rida.conf")
|
||||
rida.logger.init_logging(conf)
|
||||
|
||||
db = rida.database.Database()
|
||||
db = rida.database.Database(conf)
|
||||
|
||||
@app.route("/rida/module-builds/", methods=["POST"])
|
||||
def submit_build():
|
||||
|
||||
@@ -62,17 +62,24 @@ Base = declarative_base(cls=RidaBase)
|
||||
class Database(object):
|
||||
"""Class for handling database connections."""
|
||||
|
||||
def __init__(self, rdburl=None, debug=False):
|
||||
def __init__(self, config, debug=False):
|
||||
"""Initialize the database object."""
|
||||
if not isinstance(rdburl, str):
|
||||
rdburl = "sqlite:///rida.db"
|
||||
engine = create_engine(rdburl, echo=debug)
|
||||
Session = sessionmaker(bind=engine)
|
||||
self._session = Session()
|
||||
self.engine = create_engine(config.db, echo=debug)
|
||||
self._session = None # Lazilly created..
|
||||
|
||||
def __enter__(self):
|
||||
return self.session()
|
||||
|
||||
def __exit__(self, *args, **kwargs):
|
||||
self._session.close()
|
||||
self._session = None
|
||||
|
||||
@property
|
||||
def session(self):
|
||||
"""Database session object."""
|
||||
if not self._session:
|
||||
Session = sessionmaker(bind=self.engine)
|
||||
self._session = Session()
|
||||
return self._session
|
||||
|
||||
@classmethod
|
||||
|
||||
Reference in New Issue
Block a user