mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-09-06 16:07:01 +08:00
refactor(db): 修复异步连接池无界增长,并完成 SQLAlchemy 2.0 迁移与分层归位 (#6320)
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
from typing import Any, Optional
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import Column, Integer, String, JSON, select
|
||||
from sqlalchemy import Integer, String, JSON, delete, select
|
||||
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, get_id_column, Base, async_db_query
|
||||
|
||||
@@ -13,24 +14,24 @@ class SiteStatistic(Base):
|
||||
"""
|
||||
id = get_id_column()
|
||||
# 域名Key
|
||||
domain = Column(String, index=True)
|
||||
domain: Mapped[Optional[str]] = mapped_column(String, index=True)
|
||||
# 成功次数
|
||||
success = Column(Integer)
|
||||
success: Mapped[Optional[int]] = mapped_column(Integer)
|
||||
# 失败次数
|
||||
fail = Column(Integer)
|
||||
fail: Mapped[Optional[int]] = mapped_column(Integer)
|
||||
# 平均耗时 秒
|
||||
seconds = Column(Integer)
|
||||
seconds: Mapped[Optional[int]] = mapped_column(Integer)
|
||||
# 最后一次访问状态 0-成功 1-失败
|
||||
lst_state = Column(Integer)
|
||||
lst_state: Mapped[Optional[int]] = mapped_column(Integer)
|
||||
# 最后访问时间
|
||||
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"))
|
||||
# 耗时记录 Json
|
||||
note = Column(JSON)
|
||||
note: Mapped[Optional[Any]] = mapped_column(JSON)
|
||||
|
||||
@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
|
||||
@@ -41,4 +42,4 @@ class SiteStatistic(Base):
|
||||
@classmethod
|
||||
@db_update
|
||||
def reset(cls, db: Session):
|
||||
db.query(cls).delete()
|
||||
db.execute(delete(cls))
|
||||
|
||||
Reference in New Issue
Block a user