123456789101112131415161718192021222324252627 |
- from sqlalchemy import create_engine
- from app.config import dataSettings, platformSettings, connectionPool,dataBase
- from app.logger import logger
- DATABASE_URLS = {
- dataBase.DATA_DB: f"mysql+pymysql://{dataSettings.db_user}:{dataSettings.db_password}@{dataSettings.db_host}/{dataSettings.db_name}",
- dataBase.PLATFORM_DB: f"mysql+pymysql://{platformSettings.db_user}:{platformSettings.db_password}@{platformSettings.db_host}/{platformSettings.db_name}"
- }
- databases = {}
- for db_name, db_url in DATABASE_URLS.items():
- databases[db_name] = create_engine(db_url,
- pool_size=connectionPool.POOL_SIZE,
- max_overflow=connectionPool.MAX_OVERFLOW,
- pool_timeout=connectionPool.POOL_TIMEOUT,
- pool_recycle=connectionPool.POOL_RECYCLE)
- def get_engine(db_name: str):
- database = databases.get(db_name, None)
- try:
- return database
- except Exception as e:
- logger.error(f"Database '{db_name}' not found. {str(e)}")
- raise
|