Merge pull request #120 from felinae98/fix-migration-error

使用新的文件来标志 legacy db 已弃用
This commit is contained in:
felinae98 2022-10-10 23:14:07 +08:00 committed by GitHub
commit 14e5f2666b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 8 deletions

View File

@ -1,5 +1,7 @@
import json
import os import os
from collections import defaultdict from collections import defaultdict
from datetime import datetime
from os import path from os import path
from pathlib import Path from pathlib import Path
from typing import DefaultDict, Literal, Mapping, TypedDict from typing import DefaultDict, Literal, Mapping, TypedDict
@ -17,7 +19,7 @@ from .utils import NoSuchSubscribeException, NoSuchUserException
supported_target_type = platform_manager.keys() supported_target_type = platform_manager.keys()
def get_config_path() -> str: def get_config_path() -> tuple[str, str]:
if plugin_config.bison_config_path: if plugin_config.bison_config_path:
data_dir = plugin_config.bison_config_path data_dir = plugin_config.bison_config_path
else: else:
@ -27,9 +29,10 @@ def get_config_path() -> str:
os.makedirs(data_dir) os.makedirs(data_dir)
old_path = path.join(data_dir, "hk_reporter.json") old_path = path.join(data_dir, "hk_reporter.json")
new_path = path.join(data_dir, "bison.json") new_path = path.join(data_dir, "bison.json")
deprecated_maker_path = path.join(data_dir, "bison.json.deprecated")
if os.path.exists(old_path) and not os.path.exists(new_path): if os.path.exists(old_path) and not os.path.exists(new_path):
os.rename(old_path, new_path) os.rename(old_path, new_path)
return new_path return new_path, deprecated_maker_path
def drop(): def drop():
@ -39,11 +42,15 @@ def drop():
working_dir = os.getcwd() working_dir = os.getcwd()
data_dir = path.join(working_dir, "data") data_dir = path.join(working_dir, "data")
old_path = path.join(data_dir, "bison.json") old_path = path.join(data_dir, "bison.json")
new_path = path.join(data_dir, "bison-legacy.json") deprecated_marker_path = path.join(data_dir, "bison.json.deprecated")
if os.path.exists(old_path): if os.path.exists(old_path):
config.db.close() config.db.close()
config.available = False config.available = False
os.rename(old_path, new_path) with open(deprecated_marker_path, "w") as file:
content = {
"migration_time": datetime.now().isoformat(),
}
file.write(json.dumps(content))
return True return True
return False return False
@ -71,10 +78,12 @@ class Config(metaclass=Singleton):
self._do_init() self._do_init()
def _do_init(self): def _do_init(self):
path = get_config_path() path, deprecated_marker_path = get_config_path()
if Path(path).exists(): if Path(deprecated_marker_path).exists():
self.available = False
elif Path(path).exists():
self.available = True self.available = True
self.db = TinyDB(get_config_path(), encoding="utf-8") self.db = TinyDB(path, encoding="utf-8")
self.kv_config = self.db.table("kv") self.kv_config = self.db.table("kv")
self.user_target = self.db.table("user_target") self.user_target = self.db.table("user_target")
self.target_user_cache: dict[str, defaultdict[Target, list[User]]] = {} self.target_user_cache: dict[str, defaultdict[Target, list[User]]] = {}

View File

@ -60,7 +60,7 @@ async def use_legacy_config(app: App):
import aiofiles import aiofiles
from nonebot_bison.config.config_legacy import config, get_config_path from nonebot_bison.config.config_legacy import config, get_config_path
async with aiofiles.open(get_config_path(), "w") as f: async with aiofiles.open(get_config_path()[0], "w") as f:
await f.write("{}") await f.write("{}")
config._do_init() config._do_init()