ALittleBot / nonebot-plugin-fishing

你甚至可以电子钓鱼
MIT License
16 stars 2 forks source link

意外地读取了不存在的orm迁移分支:No such revision or branch 'xxxxxxxxxxxxxx' #8

Open RH-Xie opened 6 months ago

RH-Xie commented 6 months ago

描述

首次安装该插件,nb orm upgradenb run 均报错 No such revision or branch 'xxxxxxxxxxxxxx' (报错全部内容详见本层末)

环境

插件版本:0.2.0及以上(使用orm插件后) Windows 10,Python 3.10.9, nonebot cli version 1.0.5,Nonebot(FastAPI)

复现

  1. 其他的虚拟环境先安装了使用 nonebot_plugin_orm 并含有migrations文件夹的插件(如 nonebot_plugin_skland_arksign
  2. 本虚拟环境仅安装本插件的情况下,nb orm upgradenb run都会导致 No such revision or branch 'XXXXXXX'

分析

主观推测是其他插件在表alembic_version中存入了一些迁移版本,本插件读取表后进行upgrade,发现本插件下没有对应的迁移分支,故报错

解决

(临时解决方案)

  1. 0.2.0及以后版本:
  1. 不修复,使用低版本,则安装0.1.4即可,没有商店部分

没有看代码,像是读取了alembic_version表全部的分支然后进行了数据库迁移,不知有没有办法区分alembic_version表下每一项属于哪个插件,或者单独开表? (真没细看,错了请轻喷XD)

错误报告

使用虚拟环境: F:\nb_fishing_fix\.venv\Scripts\python.exe
04-21 00:30:59 [SUCCESS] nonebot | NoneBot is initializing...
04-21 00:30:59 [INFO] nonebot | Current Env: prod
04-21 00:31:01 [SUCCESS] nonebot | Succeeded to load plugin "nonebot_plugin_localstore"
04-21 00:31:01 [SUCCESS] nonebot | Succeeded to load plugin "nonebot_plugin_orm"
04-21 00:31:01 [SUCCESS] nonebot | Succeeded to load plugin "nonebot_plugin_fishing"
04-21 00:31:01 [SUCCESS] nonebot | Running NoneBot...
04-21 00:31:01 [INFO] uvicorn | Started server process [17068]
04-21 00:31:01 [INFO] uvicorn | Waiting for application startup.
04-21 00:31:01 [ERROR] uvicorn | Traceback (most recent call last):
  File "F:\nb_fishing_fix\.venv\lib\site-packages\alembic\script\base.py", line 251, in _catch_revision_errors
    yield
  File "F:\nb_fishing_fix\.venv\lib\site-packages\alembic\script\base.py", line 314, in get_revisions
    self.revision_map.get_revisions(id_),
  File "F:\nb_fishing_fix\.venv\lib\site-packages\alembic\script\revision.py", line 543, in get_revisions
    return sum([self.get_revisions(id_elem) for id_elem in id_], ())
  File "F:\nb_fishing_fix\.venv\lib\site-packages\alembic\script\revision.py", line 543, in <listcomp>
    return sum([self.get_revisions(id_elem) for id_elem in id_], ())
  File "F:\nb_fishing_fix\.venv\lib\site-packages\alembic\script\revision.py", line 566, in get_revisions
    return tuple(
  File "F:\nb_fishing_fix\.venv\lib\site-packages\alembic\script\revision.py", line 567, in <genexpr>
    self._revision_for_ident(rev_id, branch_label)
  File "F:\nb_fishing_fix\.venv\lib\site-packages\alembic\script\revision.py", line 638, in _revision_for_ident
    raise ResolutionError(
alembic.script.revision.ResolutionError: No such revision or branch '46327b837dd8'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "F:\nb_fishing_fix\.venv\lib\site-packages\starlette\routing.py", line 732, in lifespan
    async with self.lifespan_context(app) as maybe_state:
  File "D:\Python 3.10.9\lib\contextlib.py", line 199, in __aenter__
    return await anext(self.gen)
  File "F:\nb_fishing_fix\.venv\lib\site-packages\nonebot\drivers\fastapi.py", line 153, in _lifespan_manager
    await self._lifespan.startup()
  File "F:\nb_fishing_fix\.venv\lib\site-packages\nonebot\internal\driver\_lifespan.py", line 41, in startup
    await self._run_lifespan_func(self._startup_funcs)
  File "F:\nb_fishing_fix\.venv\lib\site-packages\nonebot\internal\driver\_lifespan.py", line 35, in _run_lifespan_func
    await cast(ASYNC_LIFESPAN_FUNC, func)()
  File "F:\nb_fishing_fix\.venv\lib\site-packages\nonebot_plugin_orm\__init__.py", line 84, in init_orm
    await greenlet_spawn(migrate.check, alembic_config)
  File "F:\nb_fishing_fix\.venv\lib\site-packages\sqlalchemy\util\_concurrency_py3k.py", line 201, in greenlet_spawn
    result = context.throw(*sys.exc_info())
  File "F:\nb_fishing_fix\.venv\lib\site-packages\nonebot_plugin_orm\migrate.py", line 540, in check
    script.run_env()
  File "F:\nb_fishing_fix\.venv\lib\site-packages\alembic\script\base.py", line 583, in run_env
    util.load_python_file(self.dir, "env.py")
  File "F:\nb_fishing_fix\.venv\lib\site-packages\alembic\util\pyfiles.py", line 95, in load_python_file
    module = load_module_py(module_id, path)
  File "F:\nb_fishing_fix\.venv\lib\site-packages\alembic\util\pyfiles.py", line 113, in load_module_py
    spec.loader.exec_module(module)  # type: ignore
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "F:\nb_fishing_fix\.venv\lib\site-packages\nonebot_plugin_orm\templates\generic\env.py", line 90, in <module>
    await_only(coro)
  File "F:\nb_fishing_fix\.venv\lib\site-packages\sqlalchemy\util\_concurrency_py3k.py", line 131, in await_only
    return current.driver.switch(awaitable)  # type: ignore[no-any-return]
  File "F:\nb_fishing_fix\.venv\lib\site-packages\sqlalchemy\util\_concurrency_py3k.py", line 196, in greenlet_spawn
    value = await result
  File "F:\nb_fishing_fix\.venv\lib\site-packages\nonebot_plugin_orm\templates\generic\env.py", line 79, in run_migrations_online
    await connection.run_sync(do_run_migrations)
  File "F:\nb_fishing_fix\.venv\lib\site-packages\sqlalchemy\ext\asyncio\engine.py", line 886, in run_sync
    return await greenlet_spawn(
  File "F:\nb_fishing_fix\.venv\lib\site-packages\sqlalchemy\util\_concurrency_py3k.py", line 203, in greenlet_spawn
    result = context.switch(value)
  File "F:\nb_fishing_fix\.venv\lib\site-packages\nonebot_plugin_orm\templates\generic\env.py", line 69, in do_run_migrations
    context.run_migrations()
  File "<string>", line 8, in run_migrations
  File "F:\nb_fishing_fix\.venv\lib\site-packages\alembic\runtime\environment.py", line 948, in run_migrations
    self.get_context().run_migrations(**kw)
  File "F:\nb_fishing_fix\.venv\lib\site-packages\alembic\runtime\migration.py", line 615, in run_migrations
    for step in self._migrations_fn(heads, self):
  File "F:\nb_fishing_fix\.venv\lib\site-packages\nonebot_plugin_orm\migrate.py", line 525, in retrieve_migrations
    if set(script.get_revisions(rev)) != set(script.get_revisions("heads")):
  File "F:\nb_fishing_fix\.venv\lib\site-packages\alembic\script\base.py", line 311, in get_revisions
    with self._catch_revision_errors():
  File "D:\Python 3.10.9\lib\contextlib.py", line 153, in __exit__
    self.gen.throw(typ, value, traceback)
  File "F:\nb_fishing_fix\.venv\lib\site-packages\alembic\script\base.py", line 283, in _catch_revision_errors
    raise util.CommandError(resolution) from re
alembic.util.exc.CommandError: Can't locate revision identified by '46327b837dd8'
ALittleBot commented 6 months ago

如果是首次安装插件,应该不会有太多数据,可尝试删除本地数据库后,安装 0.2.3 版本,即可正常使用。

ALittleBot commented 6 months ago

Linux 环境下的数据库一般为 ~/.local/share/nonebot2/nonebot-plugin-orm/migrations~/.local/share/nonebot2/nonebot-plugin-orm/db.sqlite3,可删除后,安装 0.2.3 版本,执行 nb orm upgrade