mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2026-05-09 18:27:56 +08:00
🚧 remove User type
This commit is contained in:
@@ -4,15 +4,14 @@ from datetime import datetime, time
|
||||
from typing import Awaitable, Callable, Optional, Sequence
|
||||
|
||||
from nonebot_plugin_datastore import create_session
|
||||
from nonebot_plugin_saa import PlatformTarget
|
||||
from sqlalchemy import delete, func, select
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.orm import selectinload
|
||||
|
||||
from ..types import Category, PlatformWeightConfigResp, Tag
|
||||
from ..types import Target as T_Target
|
||||
from ..types import TimeWeightConfig
|
||||
from ..types import User as T_User
|
||||
from ..types import UserSubInfo, WeightConfig
|
||||
from ..types import TimeWeightConfig, UserSubInfo, WeightConfig
|
||||
from .db_model import ScheduleTimeWeight, Subscribe, Target, User
|
||||
from .utils import NoSuchTargetException
|
||||
|
||||
@@ -40,8 +39,7 @@ class DBConfig:
|
||||
|
||||
async def add_subscribe(
|
||||
self,
|
||||
user: int,
|
||||
user_type: str,
|
||||
user: PlatformTarget,
|
||||
target: T_Target,
|
||||
target_name: str,
|
||||
platform_name: str,
|
||||
@@ -49,12 +47,10 @@ class DBConfig:
|
||||
tags: list[Tag],
|
||||
):
|
||||
async with create_session() as session:
|
||||
db_user_stmt = (
|
||||
select(User).where(User.uid == user).where(User.type == user_type)
|
||||
)
|
||||
db_user_stmt = select(User).where(User.user_target == user.dict())
|
||||
db_user: Optional[User] = await session.scalar(db_user_stmt)
|
||||
if not db_user:
|
||||
db_user = User(uid=user, type=user_type)
|
||||
db_user = User(user_target=user.dict())
|
||||
session.add(db_user)
|
||||
db_target_stmt = (
|
||||
select(Target)
|
||||
@@ -85,11 +81,11 @@ class DBConfig:
|
||||
raise SubscribeDupException()
|
||||
raise e
|
||||
|
||||
async def list_subscribe(self, user: int, user_type: str) -> Sequence[Subscribe]:
|
||||
async def list_subscribe(self, user: PlatformTarget) -> Sequence[Subscribe]:
|
||||
async with create_session() as session:
|
||||
query_stmt = (
|
||||
select(Subscribe)
|
||||
.where(User.type == user_type, User.uid == user)
|
||||
.where(User.user_target == user.dict())
|
||||
.join(User)
|
||||
.options(selectinload(Subscribe.target))
|
||||
)
|
||||
@@ -109,11 +105,11 @@ class DBConfig:
|
||||
return subs
|
||||
|
||||
async def del_subscribe(
|
||||
self, user: int, user_type: str, target: str, platform_name: str
|
||||
self, user: PlatformTarget, target: str, platform_name: str
|
||||
):
|
||||
async with create_session() as session:
|
||||
user_obj = await session.scalar(
|
||||
select(User).where(User.uid == user, User.type == user_type)
|
||||
select(User).where(User.user_target == user.dict())
|
||||
)
|
||||
target_obj = await session.scalar(
|
||||
select(Target).where(
|
||||
@@ -142,8 +138,7 @@ class DBConfig:
|
||||
|
||||
async def update_subscribe(
|
||||
self,
|
||||
user: int,
|
||||
user_type: str,
|
||||
user: PlatformTarget,
|
||||
target: str,
|
||||
target_name: str,
|
||||
platform_name: str,
|
||||
@@ -154,8 +149,7 @@ class DBConfig:
|
||||
subscribe_obj: Subscribe = await sess.scalar(
|
||||
select(Subscribe)
|
||||
.where(
|
||||
User.uid == user,
|
||||
User.type == user_type,
|
||||
User.user_target == user.dict(),
|
||||
Target.target == target,
|
||||
Target.platform_name == platform_name,
|
||||
)
|
||||
@@ -272,7 +266,7 @@ class DBConfig:
|
||||
return list(
|
||||
map(
|
||||
lambda subscribe: UserSubInfo(
|
||||
T_User(subscribe.user.uid, subscribe.user.type),
|
||||
PlatformTarget.deserialize(subscribe.user.user_target),
|
||||
subscribe.categories,
|
||||
subscribe.tags,
|
||||
),
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from nonebot.log import logger
|
||||
from nonebot_plugin_datastore.db import get_engine
|
||||
from nonebot_plugin_saa import TargetQQGroup, TargetQQPrivate
|
||||
from sqlalchemy.ext.asyncio.session import AsyncSession
|
||||
|
||||
from .config_legacy import Config, ConfigContent, drop
|
||||
@@ -21,7 +22,11 @@ async def data_migrate():
|
||||
subscribe_to_create = []
|
||||
platform_target_map: dict[str, tuple[Target, str, int]] = {}
|
||||
for user in all_subs:
|
||||
db_user = User(uid=user["user"], type=user["user_type"])
|
||||
if user["user_type"] == "group":
|
||||
user_target = TargetQQGroup(group_id=user["user"])
|
||||
else:
|
||||
user_target = TargetQQPrivate(user_id=user["user"])
|
||||
db_user = User(user_target=user_target.dict())
|
||||
user_to_create.append(db_user)
|
||||
user_sub_set = set()
|
||||
for sub in user["subs"]:
|
||||
|
||||
@@ -14,15 +14,15 @@ get_plugin_data().set_migration_dir(Path(__file__).parent / "migrations")
|
||||
|
||||
|
||||
class User(Model):
|
||||
__table_args__ = (UniqueConstraint("type", "uid", name="unique-user-constraint"),)
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
type: Mapped[str] = mapped_column(String(20))
|
||||
uid: Mapped[int]
|
||||
user_target: Mapped[Optional[dict]] = mapped_column(JSON, nullable=True)
|
||||
user_target: Mapped[dict] = mapped_column(JSON)
|
||||
|
||||
subscribes: Mapped[list["Subscribe"]] = relationship(back_populates="user")
|
||||
|
||||
@property
|
||||
def saa_target(self) -> PlatformTarget:
|
||||
return PlatformTarget.deserialize(self.user_target)
|
||||
|
||||
|
||||
class Target(Model):
|
||||
__table_args__ = (
|
||||
|
||||
@@ -18,6 +18,7 @@ depends_on = None
|
||||
def upgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
with op.batch_alter_table("nonebot_bison_user", schema=None) as batch_op:
|
||||
batch_op.drop_constraint("unique-user-constraint", type_="unique")
|
||||
batch_op.add_column(sa.Column("user_target", sa.JSON(), nullable=True))
|
||||
|
||||
# ### end Alembic commands ###
|
||||
@@ -27,5 +28,6 @@ def downgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
with op.batch_alter_table("nonebot_bison_user", schema=None) as batch_op:
|
||||
batch_op.drop_column("user_target")
|
||||
batch_op.create_unique_constraint("unique-user-constraint", ["type", "uid"])
|
||||
|
||||
# ### end Alembic commands ###
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
"""make user_target not nullable
|
||||
|
||||
Revision ID: 67c38b3f39c2
|
||||
Revises: a5466912fad0
|
||||
Create Date: 2023-03-20 11:08:42.883556
|
||||
|
||||
"""
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
from sqlalchemy.dialects import sqlite
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = "67c38b3f39c2"
|
||||
down_revision = "a5466912fad0"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
with op.batch_alter_table("nonebot_bison_user", schema=None) as batch_op:
|
||||
batch_op.alter_column(
|
||||
"user_target", existing_type=sqlite.JSON(), nullable=False
|
||||
)
|
||||
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
with op.batch_alter_table("nonebot_bison_user", schema=None) as batch_op:
|
||||
batch_op.alter_column("user_target", existing_type=sqlite.JSON(), nullable=True)
|
||||
|
||||
# ### end Alembic commands ###
|
||||
@@ -0,0 +1,33 @@
|
||||
"""remove uid and type
|
||||
|
||||
Revision ID: 8d3863e9d74b
|
||||
Revises: 67c38b3f39c2
|
||||
Create Date: 2023-03-20 15:38:20.220599
|
||||
|
||||
"""
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = "8d3863e9d74b"
|
||||
down_revision = "67c38b3f39c2"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
with op.batch_alter_table("nonebot_bison_user", schema=None) as batch_op:
|
||||
batch_op.drop_column("uid")
|
||||
batch_op.drop_column("type")
|
||||
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
with op.batch_alter_table("nonebot_bison_user", schema=None) as batch_op:
|
||||
batch_op.add_column(sa.Column("type", sa.VARCHAR(length=20), nullable=False))
|
||||
batch_op.add_column(sa.Column("uid", sa.INTEGER(), nullable=False))
|
||||
|
||||
# ### end Alembic commands ###
|
||||
Reference in New Issue
Block a user