# import os # from sqlalchemy import create_engine # from sqlalchemy.ext.declarative import declarative_base # from sqlalchemy.orm import sessionmaker # from sqlalchemy.exc import SQLAlchemyError # # MySQL 데이터베이스 설정 # DATABASE_URL = os.getenv("DATABASE_URL", "mysql+mysqlconnector://root:root@10.10.10.180:3306/chathess") # # SQLAlchemy 엔진 생성 (예외 처리 추가) # try: # engine = create_engine(DATABASE_URL) # print("Database engine created successfully.") # except SQLAlchemyError as e: # print("Failed to create database engine.") # print("Error:", e) # engine = None # # 세션 생성 (엔진이 None이면 세션 초기화 안 함) # if engine: # SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) # else: # SessionLocal = None # # Base 클래스 생성 # Base = declarative_base() # # 데이터베이스 세션 의존성 # def get_db(): # if not SessionLocal: # print("Database session is not available.") # raise RuntimeError("Database is not initialized.") # db = SessionLocal() # try: # yield db # finally: # db.close()