From d5e8054e124bf2eaad0af55a747f3a0906cd2d3f Mon Sep 17 00:00:00 2001 From: John Smith Date: Sat, 29 Jul 2023 01:07:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=87=E7=BA=A7SQLAlchemy=E5=88=B02.0.19?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- models/bilibili.py | 9 ++++++--- models/database.py | 16 ++++++++-------- requirements.txt | 2 +- services/avatar.py | 13 ++++++++----- 4 files changed, 23 insertions(+), 17 deletions(-) diff --git a/models/bilibili.py b/models/bilibili.py index fa92497..91d2839 100644 --- a/models/bilibili.py +++ b/models/bilibili.py @@ -1,11 +1,14 @@ # -*- coding: utf-8 -*- +import datetime + import sqlalchemy +from sqlalchemy.orm import Mapped, mapped_column import models.database class BilibiliUser(models.database.OrmBase): __tablename__ = 'bilibili_users' - uid = sqlalchemy.Column(sqlalchemy.BigInteger, primary_key=True) # 创建表后最好手动改成unsigned - avatar_url = sqlalchemy.Column(sqlalchemy.String(100)) - update_time = sqlalchemy.Column(sqlalchemy.DateTime) + uid: Mapped[int] = mapped_column(sqlalchemy.BigInteger, primary_key=True) # 创建表后最好手动改成unsigned + avatar_url: Mapped[str] = mapped_column(sqlalchemy.String(100)) + update_time: Mapped[datetime.datetime] diff --git a/models/database.py b/models/database.py index 3e48aa3..758a8a8 100644 --- a/models/database.py +++ b/models/database.py @@ -1,25 +1,25 @@ # -*- coding: utf-8 -*- from typing import * -import sqlalchemy.ext.declarative import sqlalchemy.orm import config -OrmBase = sqlalchemy.ext.declarative.declarative_base() -_engine = None -_DbSession: Optional[Type[sqlalchemy.orm.Session]] = None +_engine: Optional[sqlalchemy.Engine] = None + + +class OrmBase(sqlalchemy.orm.DeclarativeBase): + pass def init(_debug): cfg = config.get_config() - global _engine, _DbSession + global _engine # engine = sqlalchemy.create_engine(cfg.database_url, echo=debug) _engine = sqlalchemy.create_engine(cfg.database_url) - _DbSession = sqlalchemy.orm.sessionmaker(bind=_engine) OrmBase.metadata.create_all(_engine) -def get_session(): - return _DbSession() +def get_session() -> sqlalchemy.orm.Session: + return sqlalchemy.orm.Session(_engine) diff --git a/requirements.txt b/requirements.txt index a9bd4f9..8290d85 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -r blivedm/requirements.txt pycryptodome==3.10.1 -sqlalchemy==1.4.31 +sqlalchemy==2.0.19 tornado==6.3.2 diff --git a/services/avatar.py b/services/avatar.py index f76612c..c5b8a87 100644 --- a/services/avatar.py +++ b/services/avatar.py @@ -8,7 +8,6 @@ import urllib.parse from typing import * import aiohttp -import sqlalchemy import sqlalchemy.exc import config @@ -80,8 +79,10 @@ def get_avatar_url_from_database(user_id) -> Awaitable[Optional[str]]: def _do_get_avatar_url_from_database(user_id): try: with models.database.get_session() as session: - user = session.query(bl_models.BilibiliUser).filter( - bl_models.BilibiliUser.uid == user_id + user = session.scalars( + sqlalchemy.select(bl_models.BilibiliUser).filter( + bl_models.BilibiliUser.uid == user_id + ) ).one_or_none() if user is None: return None @@ -285,8 +286,10 @@ def _update_avatar_cache_in_memory(user_id, avatar_url): def _update_avatar_cache_in_database(user_id, avatar_url): try: with models.database.get_session() as session: - user = session.query(bl_models.BilibiliUser).filter( - bl_models.BilibiliUser.uid == user_id + user = session.scalars( + sqlalchemy.select(bl_models.BilibiliUser).filter( + bl_models.BilibiliUser.uid == user_id + ) ).one_or_none() if user is None: user = bl_models.BilibiliUser(