"""Mysql Connector.""" from sqlalchemy import create_engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker from grass import config DEFAULT_POOL_RECYCLE_TIMEOUT = 3600 def get_pool_recycle_timeout(conf): is_dev_environment = conf.environment == conf.DEV_ENVIRONMENT if is_dev_environment and conf.DB_POOL_RECYCLE_TIMEOUT: return int(conf.DB_POOL_RECYCLE_TIMEOUT) else: return DEFAULT_POOL_RECYCLE_TIMEOUT if config.environment == config.TEST_ENVIRONMENT: # For the test environment the database used is a in-memory sqlite db. This # db gets dropped immediately after the execution of the tests. engine = create_engine('sqlite://') else: # For all other environments we use pooled connections like ows-users. engine = create_engine( config.DB_URL, pool_size=5, max_overflow=-1, pool_recycle=get_pool_recycle_timeout(config), connect_args={}, ) BaseModel = declarative_base() # A DBSession() instance establishes all conversations with the database # and represents a "staging zone" for all the objects loaded into the # database session object. Any change made against the objects in the # session won't be persisted into the database until you call # session.commit(). If you're not happy about the changes, you can # revert all of them back to the last commit by calling # session.rollback() # # Description taken from: # pythoncentral.io/introductory-tutorial-python-sqlalchemy/ session = sessionmaker(bind=engine, expire_on_commit=False)