|
|
@@ -5,6 +5,7 @@
|
|
|
"""
|
|
|
|
|
|
import os
|
|
|
+from dotenv import load_dotenv
|
|
|
from typing import Generator
|
|
|
from sqlalchemy import create_engine
|
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
|
@@ -36,11 +37,13 @@ def get_database_url() -> str:
|
|
|
DB_PASSWORD: 数据库密码 (必需)
|
|
|
DB_NAME: 数据库名称 (默认: content-deconstruction)
|
|
|
"""
|
|
|
+ load_dotenv()
|
|
|
+ env = (os.getenv("APP_ENV") or os.getenv("ENV") or "local").lower()
|
|
|
host = os.getenv("DB_HOST", "rm-t4nh1xx6o2a6vj8qu3o.mysql.singapore.rds.aliyuncs.com")
|
|
|
port = os.getenv("DB_PORT", "3306")
|
|
|
user = os.getenv("DB_USER", "content_rw")
|
|
|
password = os.getenv("DB_PASSWORD", "bC1aH4bA1lB0")
|
|
|
- database = os.getenv("DB_NAME", "content-deconstruction")
|
|
|
+ database = os.getenv("DB_NAME", "content-deconstruction-test" if env in ("local","dev","development") else "content-deconstruction")
|
|
|
|
|
|
if not password:
|
|
|
raise ValueError("DB_PASSWORD environment variable is required")
|
|
|
@@ -65,7 +68,7 @@ def get_engine():
|
|
|
pool_pre_ping=True, # 连接前检查连接是否有效
|
|
|
echo=False, # 设置为 True 可以打印 SQL 语句,用于调试
|
|
|
)
|
|
|
- logger.info(f"Database engine created for database: {os.getenv('DB_NAME', 'content-deconstruction')}")
|
|
|
+ logger.info(f"Database engine created for database: {os.getenv('DB_NAME', database)}")
|
|
|
return _engine
|
|
|
|
|
|
|
|
|
@@ -123,4 +126,3 @@ def drop_db():
|
|
|
engine = get_engine()
|
|
|
Base.metadata.drop_all(bind=engine)
|
|
|
logger.warning("All database tables dropped")
|
|
|
-
|