mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2026-05-09 18:27:56 +08:00
💥 适配最新的 DataStore 插件,并切换模型为 SQLModel (#178)
* 使用 SQLModel * 处理数据库迁移 * 与之前的模型相匹配 * sqlmodel 和 sqlalchemy 的导入移入测试函数内 并且使用 init_db 且测试前加载插件 * 重命名 alembic_version 表之前先检查是否存在且 version_num 属于插件 * 降级应该是把名称重新命名回去而不是删掉 * 不再设置 arbitrary_types_allowed 为 True
This commit is contained in:
@@ -1,16 +1,14 @@
|
||||
import pytest
|
||||
from nonebug.app import App
|
||||
from sqlalchemy.ext.asyncio.session import AsyncSession
|
||||
from sqlalchemy.sql.functions import func
|
||||
from sqlmodel.sql.expression import select
|
||||
|
||||
|
||||
async def test_add_subscribe(app: App, init_scheduler):
|
||||
|
||||
from nonebot_bison.config.db_config import config
|
||||
from nonebot_bison.config.db_model import Subscribe, Target, User
|
||||
from nonebot_bison.types import Target as TTarget
|
||||
from nonebot_plugin_datastore.db import get_engine
|
||||
from sqlalchemy.ext.asyncio.session import AsyncSession
|
||||
from sqlmodel.sql.expression import select
|
||||
|
||||
await config.add_subscribe(
|
||||
user=123,
|
||||
@@ -74,7 +72,6 @@ async def test_add_subscribe(app: App, init_scheduler):
|
||||
|
||||
|
||||
async def test_add_dup_sub(init_scheduler):
|
||||
|
||||
from nonebot_bison.config.db_config import SubscribeDupException, config
|
||||
from nonebot_bison.types import Target as TTarget
|
||||
|
||||
@@ -102,9 +99,12 @@ async def test_add_dup_sub(init_scheduler):
|
||||
|
||||
async def test_del_subsribe(init_scheduler):
|
||||
from nonebot_bison.config.db_config import config
|
||||
from nonebot_bison.config.db_model import Subscribe, Target, User
|
||||
from nonebot_bison.config.db_model import Subscribe, Target
|
||||
from nonebot_bison.types import Target as TTarget
|
||||
from nonebot_plugin_datastore.db import get_engine
|
||||
from sqlalchemy.ext.asyncio.session import AsyncSession
|
||||
from sqlalchemy.sql.functions import func
|
||||
from sqlmodel.sql.expression import select
|
||||
|
||||
await config.add_subscribe(
|
||||
user=123,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
async def test_migration(use_legacy_config):
|
||||
from nonebot_bison.config.config_legacy import config as config_legacy
|
||||
from nonebot_bison.config.db import upgrade_db
|
||||
from nonebot_bison.config.db_config import config
|
||||
from nonebot_plugin_datastore.db import init_db
|
||||
|
||||
config_legacy.add_subscribe(
|
||||
user=123,
|
||||
@@ -31,7 +31,7 @@ async def test_migration(use_legacy_config):
|
||||
tags=[],
|
||||
)
|
||||
# await data_migrate()
|
||||
await upgrade_db()
|
||||
await init_db()
|
||||
user123_config = await config.list_subscribe(123, "group")
|
||||
assert len(user123_config) == 2
|
||||
for c in user123_config:
|
||||
@@ -55,8 +55,8 @@ async def test_migration(use_legacy_config):
|
||||
|
||||
async def test_migrate_dup(use_legacy_config):
|
||||
from nonebot_bison.config.config_legacy import config as config_legacy
|
||||
from nonebot_bison.config.db import upgrade_db
|
||||
from nonebot_bison.config.db_config import config
|
||||
from nonebot_plugin_datastore.db import init_db
|
||||
|
||||
config_legacy.add_subscribe(
|
||||
user=123,
|
||||
@@ -77,6 +77,6 @@ async def test_migrate_dup(use_legacy_config):
|
||||
tags=[],
|
||||
)
|
||||
# await data_migrate()
|
||||
await upgrade_db()
|
||||
await init_db()
|
||||
user123_config = await config.list_subscribe(123, "group")
|
||||
assert len(user123_config) == 1
|
||||
|
||||
+3
-3
@@ -22,6 +22,7 @@ async def app(nonebug_init: None, tmp_path: Path, monkeypatch: pytest.MonkeyPatc
|
||||
config.superusers = {"10001"}
|
||||
config.log_level = "TRACE"
|
||||
config.bison_filter_log = False
|
||||
nonebot.require("nonebot_bison")
|
||||
return App(monkeypatch)
|
||||
|
||||
|
||||
@@ -35,11 +36,10 @@ def dummy_user_subinfo(app: App):
|
||||
|
||||
@pytest.fixture
|
||||
async def db_migration(app: App):
|
||||
from nonebot_bison.config.db import upgrade_db
|
||||
from nonebot_bison.config.db_model import Subscribe, Target, User
|
||||
from nonebot_plugin_datastore.db import get_engine
|
||||
from nonebot_plugin_datastore.db import get_engine, init_db
|
||||
|
||||
await upgrade_db()
|
||||
await init_db()
|
||||
async with AsyncSession(get_engine()) as sess:
|
||||
await sess.execute(delete(User))
|
||||
await sess.execute(delete(Subscribe))
|
||||
|
||||
Reference in New Issue
Block a user