""" MySQL Connector. Manages interactions with MySQL for art_relations and direct_delivery databse. """ from contextlib import contextmanager from sqlalchemy import create_engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker from assets import config ar_db_engine = create_engine(config.AR_DB_URL, poolclass=config.POOL_CLASS) ar_database_session = sessionmaker(bind=ar_db_engine) dd_db_engine = create_engine(config.DD_DB_URL, poolclass=config.POOL_CLASS) dd_database_session = sessionmaker(bind=dd_db_engine) au_db_engine = create_engine(config.RDS_DB_URL, poolclass=config.POOL_CLASS) au_database_session = sessionmaker(bind=au_db_engine) baseModel = declarative_base() DdModel = declarative_base() ArModel = declarative_base() AuModel = declarative_base() @contextmanager def ar_db_session(): """ Provide transactional scope around series of operations for art_relations. Taken from http://docs.sqlalchemy.org/en/latest/orm/session_basics.html. This handles rollback and closing of session, so there is no need to do that throughout the code. Usage: with ar_database_session() as session: session.execute(query) """ session = ar_database_session() try: yield session session.commit() except: session.rollback() raise finally: session.close() @contextmanager def dd_db_session(): """ Provide a transactional scope around a series of operations. for direct_delivery database Taken from http://docs.sqlalchemy.org/en/latest/orm/session_basics.html. This handles rollback and closing of session, so there is no need to do that throughout the code. Usage: with dd_database_session() as session: session.execute(query) """ session = dd_database_session() try: yield session session.commit() except: session.rollback() raise finally: session.close() @contextmanager def au_db_session(): """ Provide a transactional scope around a series of operations. for asset_upload database Taken from http://docs.sqlalchemy.org/en/latest/orm/session_basics.html. This handles rollback and closing of session, so there is no need to do that throughout the code. Usage: with au_database_session() as session: session.execute(query) """ session = au_database_session() try: yield session session.commit() except: session.rollback() raise finally: session.close()