diff --git a/backend/routes/dashboard_routes.py b/backend/routes/dashboard_routes.py index 44acd16..6df9154 100644 --- a/backend/routes/dashboard_routes.py +++ b/backend/routes/dashboard_routes.py @@ -6,7 +6,7 @@ from backend.routes.common import handle_request from backend.services.dashboard_service import DashboardService from core.identity import RequestContext from core.security import require_authenticated -from database.db_helper import Database +from database.db_helper import Database, load_database_settings def get_dashboard_summary( @@ -14,7 +14,7 @@ def get_dashboard_summary( service: DashboardService | None = None, database: Database | None = None, ) -> dict: - dashboard_service = service or DashboardService(database or Database()) + dashboard_service = service or DashboardService(database or Database(load_database_settings())) def action() -> dict: require_authenticated(ctx) diff --git a/main.py b/main.py index a815454..8b22ffc 100644 --- a/main.py +++ b/main.py @@ -30,6 +30,7 @@ from adapters.webui_adapter import get_web_mount_manifest from backend.route_registry import ROUTES from backend.services.ui_service import API_BASE from core.identity import RequestContext +from database.db_helper import Database, DatabaseSettings, load_database_settings PLUGIN_NAME = "astrbot_plugin_imagestudio" @@ -75,6 +76,12 @@ def get_runtime_health() -> dict[str, Any]: return runtime_health() +def initialize_runtime_database(settings: DatabaseSettings | None = None) -> dict[str, Any]: + database = Database(settings or load_database_settings()) + database.initialize() + return {"initialized": True, "backend": database.backend} + + def dispatch_web_request( ctx: RequestContext, method: str, @@ -158,10 +165,13 @@ class Main(AstrBotStar): super().__init__() self.context = context self.registration_result: dict[str, Any] | None = None + self.database_result: dict[str, Any] | None = None async def initialize(self) -> None: if self.context is not None: self.registration_result = register_plugin(self.context) + if self.registration_result.get("bridge_api", {}).get("registered"): + self.database_result = initialize_runtime_database() return None async def terminate(self) -> None: diff --git a/tests/test_plugin_entrypoint.py b/tests/test_plugin_entrypoint.py index d848e91..b8c6521 100644 --- a/tests/test_plugin_entrypoint.py +++ b/tests/test_plugin_entrypoint.py @@ -1,11 +1,13 @@ from __future__ import annotations import asyncio +import tempfile import unittest from pathlib import Path from backend.route_registry import ROUTES from core.identity import RequestContext +from database.db_helper import DatabaseSettings, SqliteSettings from main import ( Main, PLUGIN_NAME, @@ -13,6 +15,7 @@ from main import ( dispatch_web_request, get_plugin_manifest, get_runtime_health, + initialize_runtime_database, register_bridge_api, register_plugin, ) @@ -102,6 +105,15 @@ class PluginEntrypointTests(unittest.TestCase): self.assertEqual(response["status"], "ok") self.assertEqual(response["data"]["plugin_name"], PLUGIN_NAME) + def test_initialize_runtime_database_runs_migrations(self): + with tempfile.TemporaryDirectory() as tmp: + db_path = Path(tmp) / "imagestudio.db" + result = initialize_runtime_database(DatabaseSettings(sqlite=SqliteSettings(path=str(db_path)))) + + self.assertTrue(result["initialized"]) + self.assertEqual(result["backend"], "sqlite") + self.assertTrue(db_path.exists()) + def test_astrbot_main_class_exposes_manifest(self): plugin = Main(context=object())