Consolidate setup persistence into bootstrap metadata and runtime DB

This commit is contained in:
2026-04-11 20:57:55 +02:00
parent cd69550053
commit ffe7ada469
6 changed files with 82 additions and 43 deletions

View File

@@ -82,22 +82,11 @@ async def run_setup(
# Run in a thread executor so the blocking bcrypt operation does not stall
# the asyncio event loop.
password_bytes = master_password.encode()
loop = asyncio.get_running_loop()
hashed: str = await run_blocking(
lambda: bcrypt.hashpw(password_bytes, bcrypt.gensalt()).decode()
)
await settings_repo.set_setting(db, _KEY_PASSWORD_HASH, hashed)
await settings_repo.set_setting(db, _KEY_DATABASE_PATH, database_path)
await settings_repo.set_setting(db, _KEY_FAIL2BAN_SOCKET, fail2ban_socket)
await settings_repo.set_setting(db, _KEY_TIMEZONE, timezone)
await settings_repo.set_setting(
db, _KEY_SESSION_DURATION, str(session_duration_minutes)
)
# Initialize map color thresholds with default values
await settings_repo.set_setting(db, _KEY_MAP_COLOR_THRESHOLD_HIGH, "100")
await settings_repo.set_setting(db, _KEY_MAP_COLOR_THRESHOLD_MEDIUM, "50")
await settings_repo.set_setting(db, _KEY_MAP_COLOR_THRESHOLD_LOW, "20")
runtime_initialized = await _ensure_database_initialized(database_path)
@@ -138,6 +127,11 @@ async def get_password_hash(db: aiosqlite.Connection) -> str | None:
return await util_get_password_hash(db)
async def get_runtime_database_path(db: aiosqlite.Connection) -> str | None:
"""Return the runtime database path persisted during initial setup."""
return await settings_repo.get_setting(db, _KEY_DATABASE_PATH)
async def get_persisted_runtime_settings(db: aiosqlite.Connection) -> dict[str, str | int]:
"""Return runtime configuration values persisted during initial setup."""
runtime_settings: dict[str, str | int] = {}

View File

@@ -82,19 +82,27 @@ async def startup_shared_resources(
log.debug("setup_completion_cached", completed=setup_complete)
if setup_complete:
persisted_runtime_settings = (
await setup_service.get_persisted_runtime_settings(startup_db)
)
if persisted_runtime_settings:
updated_settings = settings.model_copy(update=persisted_runtime_settings)
if Path(updated_settings.database_path).resolve() != original_db_path:
await _ensure_database_schema(updated_settings.database_path)
set_runtime_settings(app, updated_settings)
settings = updated_settings
log.info(
"runtime_settings_overridden_from_setup",
overrides=persisted_runtime_settings,
)
runtime_database_path = await setup_service.get_runtime_database_path(startup_db)
if runtime_database_path:
if Path(runtime_database_path).resolve() != original_db_path:
await _ensure_database_schema(runtime_database_path)
runtime_db = await open_db(runtime_database_path)
try:
persisted_runtime_settings = (
await setup_service.get_persisted_runtime_settings(runtime_db)
)
finally:
await runtime_db.close()
if persisted_runtime_settings:
updated_settings = settings.model_copy(update=persisted_runtime_settings)
set_runtime_settings(app, updated_settings)
settings = updated_settings
log.info(
"runtime_settings_overridden_from_setup",
overrides=persisted_runtime_settings,
)
if Path(settings.database_path).resolve() != original_db_path:
runtime_db = await open_db(settings.database_path)