def _start(connection=None): """Starts a db session. :param connection: Either a db connection string or a SQLAlchemy db engine. :type connection: str | sqlalchemy.Engine """ global sa_engine global _sa_session global _sa_connection # Set default connection. if connection is None: connection = config.db # Set engine. if _sa_connection != connection: _sa_connection = connection sa_engine = create_engine(connection, echo=False) # sa_engine = create_engine(connection, # echo=False, # connect_args={"options": "-c timezone=utc"}) logger.log_db("db engine instantiated: {}".format(id(sa_engine))) # Set session. _sa_session = sessionmaker(bind=sa_engine)()
def _main(): """Main entry point. """ logger.log_db("db setup begins : db = {0}".format(config.db)) # Run setup in context of a session. with db.session.create(config.db): db.setup.execute() logger.log_db("db setup ends : db = {0}".format(config.db))
def _main(args): """Main entry point. """ if not os.path.exists(args.input_dir): raise ValueError("Input directory is invalid.") with db.session.create(): for issue in _yield_issues(args.input_dir): try: db.session.insert(issue) except sqlalchemy.exc.IntegrityError: logger.log_db("skipping issue {} :: already inserted".format(issue.uid)) db.session.rollback()
def _main(args): """Main entry point. """ if not os.path.exists(args.input_dir): raise ValueError("Input directory is invalid.") with db.session.create(): for issue in _yield_issues(args.input_dir): try: db.session.insert(issue) except sqlalchemy.exc.IntegrityError: logger.log_db("skipping issue {} :: already inserted".format( issue.uid)) db.session.rollback()
def _main(): """Main entry point. """ logger.log_db("db setup begins : db = {0}".format(config.db)) db_connection = config.db if os.getenv("ERRATA_DB_PWD"): db_connection = db_connection.replace("ENV_ERRATA_DB_PWD", os.getenv("ERRATA_DB_PWD")) with db.session.create(db_connection): db.setup.execute() logger.log_db("db setup ends : db = {0}".format(config.db))
def _main(): """Main entry point. """ # Db connection must be admin. connection = config.db.replace(db.constants.DB_USER, db.constants.DB_ADMIN_USER) logger.log_db("db setup begins : db = {0}".format(connection)) # Run setup in context of a session. with db.session.create(connection): db.setup.execute() logger.log_db("db setup ends : db = {0}".format(connection))
def _start(connection=None): """Starts a db session. :param connection: Either a db connection string or a SQLAlchemy db engine. :type connection: str | sqlalchemy.Engine """ global sa_engine global _sa_session global _sa_connection # Set default connection. if connection is None: connection = config.db # Set engine. if _sa_connection != connection: _sa_connection = connection sa_engine = create_engine(connection, echo=False) logger.log_db("db engine instantiated: {}".format(id(sa_engine))) # Set session. _sa_session = sessionmaker(bind=sa_engine)()
def create(connection=None, commitable=False): """Starts & manages a db session. :param connection: DB connection information. :type connection: str | sqlalchemy.Engine :param bool commitable: Flag indicating whether to auto-commit. """ _start(connection) logger.log_db("db connection [{}] opened".format(id(_sa_session))) try: yield except Exception as err: msg = "An unhandled exception occurred within context of a {} database connection: {}." msg = msg.format("writeable" if commitable else "readonly", err) # logger.log_db(msg) raise err else: if commitable: commit() logger.log_db("db connection [{}] closed".format(id(_sa_session))) finally: _end()
def create(connection=None, commitable=False): """Starts & manages a db session. :param connection: DB connection information. :type connection: str | sqlalchemy.Engine :param bool commitable: Flag indicating whether to auto-commit. """ _start(connection) logger.log_db("db connection [{}] opened".format(id(_sa_session))) try: yield except Exception as err: msg = "An unhandled exception occurred within context of a {} database connection: {}." msg = msg.format("writeable" if commitable else "readonly", err) logger.log_db(msg) raise err else: if commitable: commit() logger.log_db("db connection [{}] closed".format(id(_sa_session))) finally: _end()