"""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 os import sqlite3 from dataclasses import dataclass from pathlib import Path from typing import Any, Iterable, Mapping REPO_ROOT = Path(__file__).resolve().parents[1] @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) conn = sqlite3.connect(db_path) conn.row_factory = sqlite3.Row 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 if self._migration_applied(conn, version): continue self._execute_script(conn, migration_path.read_text(encoding="utf-8")) self._mark_migration_applied(conn, version) if self.backend == "postgresql": conn.commit() else: conn.commit() finally: conn.close() def _resolve_sqlite_path(self) -> Path: db_path = Path(self.settings.sqlite.path) if not db_path.is_absolute(): db_path = REPO_ROOT / db_path return db_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 _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: path = Path(config_path) if not path.is_absolute(): path = REPO_ROOT / path if not path.exists(): return DatabaseSettings() payload = _load_yaml_subset(path.read_text(encoding="utf-8")) return DatabaseSettings.from_mapping(payload) 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 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 _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 = raw_line.split("#", 1)[0].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] and value[0] in {"'", '"'}: return value[1:-1] 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)