mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2025-06-08 12:53:00 +08:00
🎨 调整旧版数据库加载逻辑 (#212)
This commit is contained in:
parent
6a28f1175e
commit
9581887994
@ -25,8 +25,6 @@ def get_config_path() -> tuple[str, str]:
|
|||||||
else:
|
else:
|
||||||
working_dir = os.getcwd()
|
working_dir = os.getcwd()
|
||||||
data_dir = path.join(working_dir, "data")
|
data_dir = path.join(working_dir, "data")
|
||||||
if not path.isdir(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")
|
deprecated_maker_path = path.join(data_dir, "bison.json.deprecated")
|
||||||
@ -36,6 +34,7 @@ def get_config_path() -> tuple[str, str]:
|
|||||||
|
|
||||||
|
|
||||||
def drop():
|
def drop():
|
||||||
|
config = Config()
|
||||||
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:
|
||||||
@ -252,7 +251,7 @@ def start_up():
|
|||||||
return
|
return
|
||||||
if not (search_res := config.kv_config.search(Query().name == "version")):
|
if not (search_res := config.kv_config.search(Query().name == "version")):
|
||||||
config.kv_config.insert({"name": "version", "value": config.migrate_version})
|
config.kv_config.insert({"name": "version", "value": config.migrate_version})
|
||||||
elif search_res[0].get("value") < config.migrate_version:
|
elif search_res[0].get("value") < config.migrate_version: # type: ignore
|
||||||
query = Query()
|
query = Query()
|
||||||
version_query = query.name == "version"
|
version_query = query.name == "version"
|
||||||
cur_version = search_res[0].get("value")
|
cur_version = search_res[0].get("value")
|
||||||
@ -268,6 +267,3 @@ def start_up():
|
|||||||
config.kv_config.update({"value": config.migrate_version}, version_query)
|
config.kv_config.update({"value": config.migrate_version}, version_query)
|
||||||
# do migration
|
# do migration
|
||||||
config.update_send_cache()
|
config.update_send_cache()
|
||||||
|
|
||||||
|
|
||||||
config = Config()
|
|
||||||
|
@ -2,11 +2,12 @@ from nonebot.log import logger
|
|||||||
from nonebot_plugin_datastore.db import get_engine
|
from nonebot_plugin_datastore.db import get_engine
|
||||||
from sqlalchemy.ext.asyncio.session import AsyncSession
|
from sqlalchemy.ext.asyncio.session import AsyncSession
|
||||||
|
|
||||||
from .config_legacy import ConfigContent, config, drop
|
from .config_legacy import Config, ConfigContent, drop
|
||||||
from .db_model import Subscribe, Target, User
|
from .db_model import Subscribe, Target, User
|
||||||
|
|
||||||
|
|
||||||
async def data_migrate():
|
async def data_migrate():
|
||||||
|
config = Config()
|
||||||
if config.available:
|
if config.available:
|
||||||
logger.warning("You are still using legacy db, migrating to sqlite")
|
logger.warning("You are still using legacy db, migrating to sqlite")
|
||||||
all_subs: list[ConfigContent] = list(
|
all_subs: list[ConfigContent] = list(
|
||||||
|
@ -17,11 +17,10 @@ def config_legacy(app: App, use_legacy_config):
|
|||||||
from nonebot_bison.config import config_legacy as config
|
from nonebot_bison.config import config_legacy as config
|
||||||
|
|
||||||
config.start_up()
|
config.start_up()
|
||||||
|
|
||||||
yield config.Config()
|
yield config.Config()
|
||||||
|
|
||||||
config.Config().db.close()
|
config.Config().db.close()
|
||||||
legacy_config = Path(config.get_config_path()[0])
|
|
||||||
legacy_config.unlink(missing_ok=True)
|
|
||||||
|
|
||||||
|
|
||||||
def test_create_and_get(config_legacy: "Config", app: App):
|
def test_create_and_get(config_legacy: "Config", app: App):
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
async def test_migration(use_legacy_config):
|
async def test_migration(use_legacy_config):
|
||||||
from nonebot_plugin_datastore.db import init_db
|
from nonebot_plugin_datastore.db import init_db
|
||||||
|
|
||||||
from nonebot_bison.config.config_legacy import config as config_legacy
|
from nonebot_bison.config.config_legacy import Config
|
||||||
from nonebot_bison.config.db_config import config
|
from nonebot_bison.config.db_config import config
|
||||||
|
|
||||||
|
config_legacy = Config()
|
||||||
config_legacy.add_subscribe(
|
config_legacy.add_subscribe(
|
||||||
user=123,
|
user=123,
|
||||||
user_type="group",
|
user_type="group",
|
||||||
@ -57,9 +58,10 @@ async def test_migration(use_legacy_config):
|
|||||||
async def test_migrate_dup(use_legacy_config):
|
async def test_migrate_dup(use_legacy_config):
|
||||||
from nonebot_plugin_datastore.db import init_db
|
from nonebot_plugin_datastore.db import init_db
|
||||||
|
|
||||||
from nonebot_bison.config.config_legacy import config as config_legacy
|
from nonebot_bison.config.config_legacy import Config
|
||||||
from nonebot_bison.config.db_config import config
|
from nonebot_bison.config.db_config import config
|
||||||
|
|
||||||
|
config_legacy = Config()
|
||||||
config_legacy.add_subscribe(
|
config_legacy.add_subscribe(
|
||||||
user=123,
|
user=123,
|
||||||
user_type="group",
|
user_type="group",
|
||||||
|
@ -84,9 +84,20 @@ async def init_scheduler(app: App):
|
|||||||
async def use_legacy_config(app: App):
|
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
|
||||||
|
from nonebot_bison.utils import Singleton
|
||||||
|
|
||||||
async with aiofiles.open(get_config_path()[0], "w") as f:
|
# 默认不创建配置所在的文件夹
|
||||||
|
# 如果需要测试需要手动创建相关文件夹
|
||||||
|
path = Path(get_config_path()[0])
|
||||||
|
path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
async with aiofiles.open(path, "w") as f:
|
||||||
await f.write("{}")
|
await f.write("{}")
|
||||||
|
|
||||||
config._do_init()
|
Config()._do_init()
|
||||||
|
|
||||||
|
yield
|
||||||
|
|
||||||
|
# 清除单例的缓存
|
||||||
|
Singleton._instances.clear()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user