data migration

This commit is contained in:
felinae98 2022-03-24 13:33:54 +08:00
parent 06e4c9ec1b
commit e202b4bd7a
No known key found for this signature in database
GPG Key ID: 00C8B010587FF610
3 changed files with 57 additions and 2 deletions

View File

@ -48,7 +48,7 @@ class SubscribeContent(TypedDict):
class ConfigContent(TypedDict):
user: str
user: int
user_type: Literal["group", "private"]
subs: list[SubscribeContent]

View File

@ -1,3 +1,4 @@
import json
from pathlib import Path
import nonebot
@ -9,7 +10,7 @@ from nonebot_plugin_datastore import PluginData, create_session, db
from sqlalchemy.engine.base import Connection
from .config_legacy import ConfigContent, config
from .db_model import Base
from .db_model import Base, Subscribe, Target, User
DATA = PluginData("bison")
@ -20,6 +21,53 @@ async def data_migrate():
all_subs: list[ConfigContent] = list(
map(lambda item: ConfigContent(**item), config.get_all_subscribe().all())
)
print(all_subs)
sess = create_session()
user_to_create = []
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"])
user_to_create.append(db_user)
for sub in user["subs"]:
target = sub["target"]
platform_name = sub["target_type"]
target_name = sub["target_name"]
key = f"{target}-{platform_name}"
if key in platform_target_map.keys():
target_obj, ext_user_type, ext_user = platform_target_map[key]
if target_obj.target_name != target_name:
# GG
logger.error(
f"你的旧版本数据库中存在数据不一致问题,请完成迁移后执行重新添加{platform_name}平台的{target}"
f"它的名字可能为{target_obj.target_name}{target_name}"
)
else:
target_obj = Target(
platform_name=platform_name,
target_name=target_name,
target=target,
)
platform_target_map[key] = (
target_obj,
user["user_type"],
user["user"],
)
subscribe_obj = Subscribe(
user=db_user,
target=target_obj,
categories=json.dumps(sub["cats"]),
tags=json.dumps(sub["tags"]),
)
subscribe_to_create.append(subscribe_obj)
sess.add_all(
user_to_create
+ list(map(lambda x: x[0], platform_target_map.values()))
+ subscribe_to_create
)
await sess.commit()
logger.info("migrate success")
@nonebot.get_driver().on_startup

View File

@ -14,6 +14,13 @@ class User:
user_type: Literal["group", "private"]
@dataclass(eq=True, frozen=True)
class PlatformTarget:
target: str
platform_name: str
target_name: str
class UserSubInfo(NamedTuple):
user: User
category_getter: Callable[[Target], list[Category]]