"""Database connection and migration helpers. SQLite is the zero-config default. PostgreSQL becomes the selected backend when configuration explicitly enables it, or when `backend: postgresql` is set. """ from __future__ import annotations import json import os import re import sqlite3 import ast from dataclasses import dataclass from pathlib import Path from typing import Any, Callable, Iterable, Mapping from core.runtime_paths import ( REPO_ROOT, get_packaged_config_path, is_default_config_path, resolve_config_path, resolve_data_path, ) _RUNTIME_CONFIG_OVERRIDES: dict[str, Any] = {} _RUNTIME_CONFIG_PERSISTER: Callable[[Mapping[str, Any]], Mapping[str, Any] | None] | None = None def set_runtime_config_overrides(payload: Mapping[str, Any] | None) -> None: """Store AstrBot-provided settings as in-process overrides for default config.""" global _RUNTIME_CONFIG_OVERRIDES _RUNTIME_CONFIG_OVERRIDES = _copy_plain_mapping(payload or {}) def get_runtime_config_overrides() -> dict[str, Any]: return _copy_plain_mapping(_RUNTIME_CONFIG_OVERRIDES) def set_runtime_config_persister( persister: Callable[[Mapping[str, Any]], Mapping[str, Any] | None] | None, ) -> None: """Register the AstrBot-owned plugin-config writer for the current runtime.""" global _RUNTIME_CONFIG_PERSISTER _RUNTIME_CONFIG_PERSISTER = persister def persist_runtime_config_values(values: Mapping[str, Any]) -> dict[str, Any]: """Persist flattened ImageStudio paths through AstrBot's config object.""" if not values: return {"available": _RUNTIME_CONFIG_PERSISTER is not None, "saved": False, "paths": []} if _RUNTIME_CONFIG_PERSISTER is None: return {"available": False, "saved": False, "paths": sorted(str(path) for path in values)} result = _RUNTIME_CONFIG_PERSISTER(_copy_plain_mapping(values)) payload = dict(result) if isinstance(result, Mapping) else {} payload.setdefault("available", True) payload.setdefault("saved", True) payload.setdefault("paths", sorted(str(path) for path in values)) return payload def load_config_payload(config_path: str | Path = "config.yaml") -> dict[str, Any]: path = resolve_config_path(config_path) payload = load_config_file_payload(path) if _should_apply_runtime_overrides(path): return _merge_mappings(payload, _RUNTIME_CONFIG_OVERRIDES) return payload def load_config_file_payload(config_path: str | Path = "config.yaml") -> dict[str, Any]: """Load packaged and writable YAML without applying in-process overrides.""" path = resolve_config_path(config_path) payload: dict[str, Any] = {} if _should_apply_runtime_overrides(path): packaged_path = get_packaged_config_path() if not _same_path(path, packaged_path) and packaged_path.exists(): payload = _load_yaml_subset(packaged_path.read_text(encoding="utf-8")) if path.exists(): payload = _merge_mappings(payload, _load_yaml_subset(path.read_text(encoding="utf-8"))) return payload @dataclass(frozen=True) class SqliteSettings: path: str = "data/db/imagestudio.db" @dataclass(frozen=True) class PostgresSettings: enabled: bool = False dsn: str = "" host: str = "" port: int = 5432 database: str = "" user: str = "" password: str = "" @property def is_configured(self) -> bool: return self.enabled and bool(self.dsn or (self.host and self.database and self.user)) @dataclass(frozen=True) class DatabaseSettings: backend: str = "auto" sqlite: SqliteSettings = SqliteSettings() postgresql: PostgresSettings = PostgresSettings() @property def selected_backend(self) -> str: backend = self.backend.lower() if backend in {"postgres", "postgresql", "pg"}: return "postgresql" if backend == "sqlite": return "sqlite" if self.postgresql.is_configured: return "postgresql" return "sqlite" @classmethod def from_mapping(cls, payload: Mapping[str, Any]) -> "DatabaseSettings": database = payload.get("database", payload) if not isinstance(database, Mapping): database = {} sqlite_payload = database.get("sqlite", {}) postgres_payload = database.get("postgresql", {}) if not isinstance(sqlite_payload, Mapping): sqlite_payload = {} if not isinstance(postgres_payload, Mapping): postgres_payload = {} return cls( backend=str(database.get("backend", "auto")), sqlite=SqliteSettings(path=str(sqlite_payload.get("path", "data/db/imagestudio.db"))), postgresql=PostgresSettings( enabled=_as_bool(postgres_payload.get("enabled", False)), dsn=str(postgres_payload.get("dsn", "") or ""), host=str(postgres_payload.get("host", "") or ""), port=int(postgres_payload.get("port", 5432) or 5432), database=str(postgres_payload.get("database", "") or ""), user=str(postgres_payload.get("user", "") or ""), password=str(postgres_payload.get("password", "") or ""), ), ) class Database: def __init__(self, settings: DatabaseSettings | None = None): self.settings = settings or DatabaseSettings() @property def backend(self) -> str: return self.settings.selected_backend def connect(self): if self.backend == "sqlite": db_path = self._resolve_sqlite_path() db_path.parent.mkdir(parents=True, exist_ok=True) # Parallel image tasks briefly overlap while recording queue, # node, image, and audit rows. Give SQLite enough time to hand the # writer lock to the next task instead of failing immediately. conn = sqlite3.connect(db_path, timeout=30.0) conn.row_factory = sqlite3.Row conn.execute("PRAGMA foreign_keys = ON") conn.execute("PRAGMA busy_timeout = 30000") return conn return self._connect_postgresql() def initialize(self, migrations_dir: str | Path | None = None) -> None: conn = self.connect() try: self._ensure_schema_migrations(conn) for migration_path in self._iter_migrations(migrations_dir): version = migration_path.stem script = migration_path.read_text(encoding="utf-8") if self._migration_applied(conn, version) and self._migration_tables_exist(conn, script): continue self._execute_script(conn, script) if not self._migration_applied(conn, version): self._mark_migration_applied(conn, version) if self.backend == "postgresql": conn.commit() else: conn.commit() finally: conn.close() def _resolve_sqlite_path(self) -> Path: return resolve_data_path(self.settings.sqlite.path) def _connect_postgresql(self): try: import psycopg from psycopg.rows import dict_row except ImportError as exc: return self._connect_postgresql_psycopg2(exc) pg = self.settings.postgresql if pg.dsn: return psycopg.connect(pg.dsn, row_factory=dict_row) return psycopg.connect( host=pg.host, port=pg.port, dbname=pg.database, user=pg.user, password=pg.password, row_factory=dict_row, ) def _connect_postgresql_psycopg2(self, original_error: ImportError): try: import psycopg2 from psycopg2.extras import RealDictCursor except ImportError as exc: raise RuntimeError( "PostgreSQL backend requires the optional 'psycopg' or 'psycopg2' package." ) from original_error if original_error else exc pg = self.settings.postgresql if pg.dsn: return _Psycopg2ConnectionAdapter(psycopg2.connect(pg.dsn, cursor_factory=RealDictCursor)) return _Psycopg2ConnectionAdapter( psycopg2.connect( host=pg.host, port=pg.port, dbname=pg.database, user=pg.user, password=pg.password, cursor_factory=RealDictCursor, ) ) def _ensure_schema_migrations(self, conn) -> None: conn.execute( """ CREATE TABLE IF NOT EXISTS schema_migrations ( version TEXT PRIMARY KEY, applied_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ) """ ) def _migration_applied(self, conn, version: str) -> bool: placeholder = self._placeholder row = conn.execute( f"SELECT version FROM schema_migrations WHERE version = {placeholder}", (version,), ).fetchone() return row is not None def _mark_migration_applied(self, conn, version: str) -> None: placeholder = self._placeholder conn.execute( f"INSERT INTO schema_migrations(version) VALUES ({placeholder})", (version,), ) def _migration_tables_exist(self, conn, script: str) -> bool: table_names = re.findall( r"CREATE\s+TABLE\s+IF\s+NOT\s+EXISTS\s+([A-Za-z_][A-Za-z0-9_]*)", script, flags=re.IGNORECASE, ) if not table_names: return True placeholder = self._placeholder if self.backend == "sqlite": query = f"SELECT name FROM sqlite_master WHERE type = 'table' AND name = {placeholder}" else: query = ( "SELECT table_name FROM information_schema.tables " f"WHERE table_schema = current_schema() AND table_name = {placeholder}" ) return all(conn.execute(query, (table_name,)).fetchone() is not None for table_name in table_names) def _execute_script(self, conn, script: str) -> None: if self.backend == "sqlite": conn.executescript(script) return statements = [statement.strip() for statement in script.split(";") if statement.strip()] for statement in statements: conn.execute(statement) @property def _placeholder(self) -> str: return "%s" if self.backend == "postgresql" else "?" def _iter_migrations(self, migrations_dir: str | Path | None) -> Iterable[Path]: directory = Path(migrations_dir) if migrations_dir else REPO_ROOT / "database" / "migrations" return sorted(path for path in directory.glob("*.sql") if path.is_file()) def load_database_settings(config_path: str | Path = "config.yaml") -> DatabaseSettings: return DatabaseSettings.from_mapping(load_config_payload(config_path)) class _Psycopg2ConnectionAdapter: def __init__(self, connection): self.connection = connection def execute(self, query: str, params: tuple[Any, ...] | list[Any] | None = None): cursor = self.connection.cursor() cursor.execute(query, tuple(params or ())) return cursor def commit(self) -> None: self.connection.commit() def rollback(self) -> None: self.connection.rollback() def close(self) -> None: self.connection.close() def _as_bool(value: Any) -> bool: if isinstance(value, bool): return value if isinstance(value, str): return value.strip().lower() in {"1", "true", "yes", "on"} return bool(value) def _should_apply_runtime_overrides(path: Path) -> bool: return is_default_config_path(path) def _same_path(left: Path, right: Path) -> bool: try: return left.resolve() == right.resolve() except OSError: return left.absolute() == right.absolute() def _copy_plain_mapping(value: Mapping[str, Any]) -> dict[str, Any]: copied: dict[str, Any] = {} for key, child in value.items(): if isinstance(child, Mapping): copied[str(key)] = _copy_plain_mapping(child) elif isinstance(child, list): copied[str(key)] = [_copy_plain_item(item) for item in child] else: copied[str(key)] = child return copied def _copy_plain_item(value: Any) -> Any: if isinstance(value, Mapping): return _copy_plain_mapping(value) if isinstance(value, list): return [_copy_plain_item(item) for item in value] return value def _merge_mappings(base: Mapping[str, Any], override: Mapping[str, Any]) -> dict[str, Any]: merged = _copy_plain_mapping(base) for key, value in override.items(): if isinstance(value, Mapping) and isinstance(merged.get(str(key)), Mapping): merged[str(key)] = _merge_mappings(merged[str(key)], value) elif isinstance(value, Mapping): merged[str(key)] = _copy_plain_mapping(value) else: merged[str(key)] = _copy_plain_item(value) return merged def _load_yaml_subset(text: str) -> dict[str, Any]: """Parse the simple YAML shape used by config.yaml without extra deps.""" root: dict[str, Any] = {} stack: list[tuple[int, dict[str, Any]]] = [(-1, root)] last_key_by_indent: dict[int, str] = {} for raw_line in text.splitlines(): line = _strip_yaml_comment(raw_line).rstrip() if not line.strip(): continue indent = len(line) - len(line.lstrip(" ")) stripped = line.strip() if stripped.startswith("- "): list_key = last_key_by_indent.get(indent - 2) if list_key: parent = stack[-1][1] if not isinstance(parent.get(list_key), list) and len(stack) >= 2: previous_parent = stack[-2][1] if previous_parent.get(list_key) is parent or isinstance(previous_parent.get(list_key), list): parent = previous_parent if not isinstance(parent.get(list_key), list): parent[list_key] = [] parent.setdefault(list_key, []).append(_parse_scalar(stripped[2:])) continue if ":" not in stripped: continue key, value = stripped.split(":", 1) key = key.strip() value = value.strip() while stack and indent <= stack[-1][0]: stack.pop() parent = stack[-1][1] if value == "": child: dict[str, Any] = {} parent[key] = child stack.append((indent, child)) last_key_by_indent[indent] = key else: parent[key] = _parse_scalar(value) last_key_by_indent[indent] = key return root def _parse_scalar(value: str) -> Any: if len(value) >= 2 and value[0] == value[-1] == '"': try: parsed = json.loads(value) if isinstance(parsed, str) and parsed.lstrip().startswith(("{", "[")): try: legacy = ast.literal_eval(parsed) except (SyntaxError, ValueError): return parsed if isinstance(legacy, (dict, list)): return legacy return parsed except json.JSONDecodeError: return value[1:-1] if value.startswith(("{", "[")): try: return json.loads(value) except json.JSONDecodeError: pass if len(value) >= 2 and value[0] == value[-1] == "'": return value[1:-1].replace("''", "'") lowered = value.lower() if lowered == "true": return True if lowered == "false": return False if lowered in {"null", "none"}: return None try: return int(value) except ValueError: return os.path.expandvars(value) def _strip_yaml_comment(line: str) -> str: quote: str | None = None escaped = False index = 0 while index < len(line): char = line[index] if quote == '"': if escaped: escaped = False elif char == "\\": escaped = True elif char == quote: quote = None elif quote == "'": if char == quote: if index + 1 < len(line) and line[index + 1] == quote: index += 1 else: quote = None elif char in {"'", '"'}: quote = char elif char == "#" and (index == 0 or line[index - 1].isspace()): return line[:index] index += 1 return line