mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-09-05 15:38:19 +08:00
refactor(db): 修复异步连接池无界增长,并完成 SQLAlchemy 2.0 迁移与分层归位 (#6320)
This commit is contained in:
+32
-31
@@ -1,8 +1,9 @@
|
||||
from typing import Any, Optional
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import Boolean, Column, Integer, String, JSON, select, delete
|
||||
from sqlalchemy import Boolean, Integer, String, JSON, select, delete
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy.orm import Mapped, Session, mapped_column
|
||||
|
||||
from app.db import db_query, db_update, Base, async_db_query, async_db_update, get_id_column
|
||||
|
||||
@@ -13,52 +14,52 @@ class Site(Base):
|
||||
"""
|
||||
id = get_id_column()
|
||||
# 站点名
|
||||
name = Column(String, nullable=False)
|
||||
name: Mapped[str] = mapped_column(String, nullable=False)
|
||||
# 域名Key
|
||||
domain = Column(String, index=True)
|
||||
domain: Mapped[Optional[str]] = mapped_column(String, index=True)
|
||||
# 站点地址
|
||||
url = Column(String, nullable=False)
|
||||
url: Mapped[str] = mapped_column(String, nullable=False)
|
||||
# 站点优先级
|
||||
pri = Column(Integer, default=1)
|
||||
pri: Mapped[Optional[int]] = mapped_column(Integer, default=1)
|
||||
# RSS地址,未启用
|
||||
rss = Column(String)
|
||||
rss: Mapped[Optional[str]] = mapped_column(String)
|
||||
# Cookie
|
||||
cookie = Column(String)
|
||||
cookie: Mapped[Optional[str]] = mapped_column(String)
|
||||
# User-Agent
|
||||
ua = Column(String)
|
||||
ua: Mapped[Optional[str]] = mapped_column(String)
|
||||
# ApiKey
|
||||
apikey = Column(String)
|
||||
apikey: Mapped[Optional[str]] = mapped_column(String)
|
||||
# Token
|
||||
token = Column(String)
|
||||
token: Mapped[Optional[str]] = mapped_column(String)
|
||||
# 是否使用代理 0-否,1-是
|
||||
proxy = Column(Integer)
|
||||
proxy: Mapped[Optional[int]] = mapped_column(Integer)
|
||||
# 过滤规则
|
||||
filter = Column(String)
|
||||
filter: Mapped[Optional[str]] = mapped_column(String)
|
||||
# 是否渲染
|
||||
render = Column(Integer)
|
||||
render: Mapped[Optional[int]] = mapped_column(Integer)
|
||||
# 是否公开站点
|
||||
public = Column(Integer)
|
||||
public: Mapped[Optional[int]] = mapped_column(Integer)
|
||||
# 附加信息
|
||||
note = Column(JSON)
|
||||
note: Mapped[Optional[Any]] = mapped_column(JSON)
|
||||
# 流控单位周期
|
||||
limit_interval = Column(Integer, default=0)
|
||||
limit_interval: Mapped[Optional[int]] = mapped_column(Integer, default=0)
|
||||
# 流控次数
|
||||
limit_count = Column(Integer, default=0)
|
||||
limit_count: Mapped[Optional[int]] = mapped_column(Integer, default=0)
|
||||
# 流控间隔
|
||||
limit_seconds = Column(Integer, default=0)
|
||||
limit_seconds: Mapped[Optional[int]] = mapped_column(Integer, default=0)
|
||||
# 超时时间
|
||||
timeout = Column(Integer, default=15)
|
||||
timeout: Mapped[Optional[int]] = mapped_column(Integer, default=15)
|
||||
# 是否启用
|
||||
is_active = Column(Boolean(), default=True)
|
||||
is_active: Mapped[Optional[bool]] = mapped_column(Boolean(), default=True)
|
||||
# 创建时间
|
||||
lst_mod_date = Column(String, default=datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
|
||||
lst_mod_date: Mapped[Optional[str]] = mapped_column(String, default=datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
|
||||
# 下载器
|
||||
downloader = Column(String)
|
||||
downloader: Mapped[Optional[str]] = mapped_column(String)
|
||||
|
||||
@classmethod
|
||||
@db_query
|
||||
def get_by_domain(cls, db: Session, domain: str):
|
||||
return db.query(cls).filter(cls.domain == domain).first()
|
||||
return db.execute(select(cls).where(cls.domain == domain)).scalars().first()
|
||||
|
||||
@classmethod
|
||||
@async_db_query
|
||||
@@ -75,34 +76,34 @@ class Site(Base):
|
||||
@classmethod
|
||||
@db_query
|
||||
def get_actives(cls, db: Session):
|
||||
return db.query(cls).filter(cls.is_active).all()
|
||||
return list(db.execute(select(cls).where(cls.is_active.is_(True))).scalars().all())
|
||||
|
||||
@classmethod
|
||||
@async_db_query
|
||||
async def async_get_actives(cls, db: AsyncSession):
|
||||
result = await db.execute(select(cls).where(cls.is_active))
|
||||
return result.scalars().all()
|
||||
result = await db.execute(select(cls).where(cls.is_active.is_(True)))
|
||||
return list(result.scalars().all())
|
||||
|
||||
@classmethod
|
||||
@db_query
|
||||
def list_order_by_pri(cls, db: Session):
|
||||
return db.query(cls).order_by(cls.pri).all()
|
||||
return list(db.execute(select(cls).order_by(cls.pri)).scalars().all())
|
||||
|
||||
@classmethod
|
||||
@async_db_query
|
||||
async def async_list_order_by_pri(cls, db: AsyncSession):
|
||||
result = await db.execute(select(cls).order_by(cls.pri))
|
||||
return result.scalars().all()
|
||||
return list(result.scalars().all())
|
||||
|
||||
@classmethod
|
||||
@db_query
|
||||
def get_domains_by_ids(cls, db: Session, ids: list):
|
||||
return [r[0] for r in db.query(cls.domain).filter(cls.id.in_(ids)).all()]
|
||||
return list(db.execute(select(cls.domain).where(cls.id.in_(ids))).scalars().all())
|
||||
|
||||
@classmethod
|
||||
@db_update
|
||||
def reset(cls, db: Session):
|
||||
db.query(cls).delete()
|
||||
db.execute(delete(cls))
|
||||
|
||||
@classmethod
|
||||
@async_db_update
|
||||
|
||||
Reference in New Issue
Block a user