Add context based session.
Signed-off-by: Jan Losinski <losinski@wh2.tu-dresden.de>
This commit is contained in:
parent
0f56408bc7
commit
8ea47aafa4
|
|
@ -31,14 +31,30 @@ class Job(Base):
|
||||||
format = Column(Enum('pdf', 'mobi'))
|
format = Column(Enum('pdf', 'mobi'))
|
||||||
|
|
||||||
|
|
||||||
def session_maker(uri):
|
class ContextSession:
|
||||||
|
def __init__(self, session_maker):
|
||||||
|
self.session_maker = session_maker
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
self.session = self.session_maker()
|
||||||
|
return self.session
|
||||||
|
|
||||||
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||||
|
self.session.close()
|
||||||
|
|
||||||
|
|
||||||
|
def context_session(config):
|
||||||
|
return ContextSession(session_maker(config))
|
||||||
|
|
||||||
|
|
||||||
|
def session_maker(config):
|
||||||
Session = sessionmaker(autocommit=False,
|
Session = sessionmaker(autocommit=False,
|
||||||
autoflush=False,
|
autoflush=False,
|
||||||
bind=create_engine(uri))
|
bind=create_engine(config.db_uri))
|
||||||
return Session
|
return Session
|
||||||
|
|
||||||
|
|
||||||
def re_create_db(uri):
|
def re_create_db(config):
|
||||||
engine = create_engine(uri)
|
engine = create_engine(config.db_uri)
|
||||||
Base.metadata.drop_all(engine)
|
Base.metadata.drop_all(engine)
|
||||||
Base.metadata.create_all(engine)
|
Base.metadata.create_all(engine)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue