style: 统一 Ruff 代码格式
This commit is contained in:
@@ -32,7 +32,9 @@ def _get_database_url() -> str:
|
||||
db_user = os.getenv("DB_USER", "douyu_login").strip()
|
||||
db_password = os.getenv("DB_PASSWORD", "")
|
||||
if not db_name or not db_user or not db_password:
|
||||
raise RuntimeError("使用 DB_HOST 时必须同时设置 DB_NAME、DB_USER 和 DB_PASSWORD")
|
||||
raise RuntimeError(
|
||||
"使用 DB_HOST 时必须同时设置 DB_NAME、DB_USER 和 DB_PASSWORD"
|
||||
)
|
||||
return (
|
||||
f"mysql+pymysql://{quote_plus(db_user)}:{quote_plus(db_password)}"
|
||||
f"@{db_host}:{db_port}/{db_name}?charset=utf8mb4"
|
||||
@@ -69,6 +71,7 @@ engine = create_engine(DATABASE_URL, **engine_options)
|
||||
|
||||
|
||||
if DATABASE_URL.startswith("sqlite"):
|
||||
|
||||
@event.listens_for(engine, "connect")
|
||||
def _set_sqlite_pragmas(dbapi_connection, connection_record):
|
||||
"""提升 SQLite 并发写入稳定性。"""
|
||||
@@ -78,6 +81,7 @@ if DATABASE_URL.startswith("sqlite"):
|
||||
cursor.execute("PRAGMA foreign_keys=ON")
|
||||
cursor.close()
|
||||
|
||||
|
||||
SessionLocal = sessionmaker(bind=engine, autocommit=False, autoflush=False)
|
||||
Base = declarative_base()
|
||||
|
||||
@@ -104,7 +108,9 @@ def run_migrations():
|
||||
from alembic.config import Config
|
||||
|
||||
config = Config(str(PROJECT_ROOT / "alembic.ini"))
|
||||
config.set_main_option("script_location", str(PROJECT_ROOT / "web" / "backend" / "migrations"))
|
||||
config.set_main_option(
|
||||
"script_location", str(PROJECT_ROOT / "web" / "backend" / "migrations")
|
||||
)
|
||||
config.set_main_option("sqlalchemy.url", DATABASE_URL)
|
||||
# 标记为应用内嵌调用,env.py 据此跳过 fileConfig,避免覆盖 uvicorn 日志配置。
|
||||
os.environ["ALEMBIC_EMBEDDED"] = "1"
|
||||
|
||||
Reference in New Issue
Block a user