Skip to content

Commit 88a9019

Browse files
committed
♻️ refactor(alembic): support unload or uninstall used plugins
1 parent 7b455d4 commit 88a9019

File tree

7 files changed

+204
-175
lines changed

7 files changed

+204
-175
lines changed

nonebot_plugin_orm/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
else:
3232
from typing_extensions import Annotated
3333

34+
require("nonebot_plugin_localstore")
35+
from nonebot_plugin_localstore import get_data_dir
36+
3437
__all__ = (
3538
# __init__
3639
"init_orm",
@@ -63,6 +66,7 @@
6366
_plugins: dict[str, Plugin]
6467
_session_factory: sa_async.async_sessionmaker[AsyncSession]
6568

69+
_data_dir = get_data_dir(__plugin_meta__.name)
6670
_driver = get_driver()
6771

6872

@@ -162,17 +166,13 @@ def _init_engines():
162166
import aiosqlite
163167

164168
del aiosqlite
165-
require("nonebot_plugin_localstore")
166-
from nonebot_plugin_localstore import get_data_file
167169
except (ImportError, RuntimeError):
168170
raise ValueError(
169171
'必须指定一个默认数据库 (SQLALCHEMY_DATABASE_URL 或 SQLALCHEMY_BINDS[""]). '
170172
"可以通过 `pip install nonebot-plugin-orm[default]` 获得开箱即用的数据库配置."
171173
) from None
172174

173-
_engines[""] = _create_engine(
174-
f"sqlite+aiosqlite:///{get_data_file(__plugin_meta__.name, 'db.sqlite3')}"
175-
)
175+
_engines[""] = _create_engine(f"sqlite+aiosqlite:///{_data_dir / 'db.sqlite3'}")
176176

177177

178178
def _init_table():

nonebot_plugin_orm/env.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from __future__ import annotations
2+
3+
from alembic import context
4+
from sqlalchemy.sql.schema import SchemaItem
5+
6+
from nonebot_plugin_orm import migrate
7+
8+
9+
def no_drop_table(
10+
_, __, type_: str, reflected: bool, compare_to: SchemaItem | None
11+
) -> bool:
12+
return not (
13+
getattr(context.config.cmd_opts, "cmd", (None,))[0] == migrate.check
14+
and type_ == "table"
15+
and reflected
16+
and compare_to is None
17+
)

0 commit comments

Comments
 (0)