| import os |
| import logging |
| import json |
| from contextlib import contextmanager |
|
|
| from peewee_migrate import Router |
| from apps.webui.internal.wrappers import register_connection |
|
|
| from typing import Optional, Any |
| from typing_extensions import Self |
|
|
| from sqlalchemy import create_engine, types, Dialect |
| from sqlalchemy.ext.declarative import declarative_base |
| from sqlalchemy.orm import sessionmaker, scoped_session |
| from sqlalchemy.sql.type_api import _T |
|
|
| from config import SRC_LOG_LEVELS, DATA_DIR, DATABASE_URL, BACKEND_DIR |
|
|
| log = logging.getLogger(__name__) |
| log.setLevel(SRC_LOG_LEVELS["DB"]) |
|
|
|
|
| class JSONField(types.TypeDecorator): |
| impl = types.Text |
| cache_ok = True |
|
|
| def process_bind_param(self, value: Optional[_T], dialect: Dialect) -> Any: |
| return json.dumps(value) |
|
|
| def process_result_value(self, value: Optional[_T], dialect: Dialect) -> Any: |
| if value is not None: |
| return json.loads(value) |
|
|
| def copy(self, **kw: Any) -> Self: |
| return JSONField(self.impl.length) |
|
|
| def db_value(self, value): |
| return json.dumps(value) |
|
|
| def python_value(self, value): |
| if value is not None: |
| return json.loads(value) |
|
|
|
|
| |
| if os.path.exists(f"{DATA_DIR}/ollama.db"): |
| |
| os.rename(f"{DATA_DIR}/ollama.db", f"{DATA_DIR}/webui.db") |
| log.info("Database migrated from Ollama-WebUI successfully.") |
| else: |
| pass |
|
|
|
|
| |
| |
| def handle_peewee_migration(DATABASE_URL): |
| try: |
| |
| db = register_connection( |
| DATABASE_URL.replace("postgresql://", "postgres://").replace("%40", "@") |
| ) |
| migrate_dir = BACKEND_DIR / "apps" / "webui" / "internal" / "migrations" |
| router = Router(db, logger=log, migrate_dir=migrate_dir) |
| router.run() |
| db.close() |
|
|
| |
|
|
| except Exception as e: |
| log.error(f"Failed to initialize the database connection: {e}") |
| raise |
|
|
| finally: |
| |
| if db and not db.is_closed(): |
| db.close() |
|
|
| |
| assert db.is_closed(), "Database connection is still open." |
|
|
|
|
| handle_peewee_migration(DATABASE_URL) |
|
|
|
|
| SQLALCHEMY_DATABASE_URL = DATABASE_URL |
| if "sqlite" in SQLALCHEMY_DATABASE_URL: |
| engine = create_engine( |
| SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False} |
| ) |
| else: |
| engine = create_engine(SQLALCHEMY_DATABASE_URL, pool_pre_ping=True) |
|
|
|
|
| SessionLocal = sessionmaker( |
| autocommit=False, autoflush=False, bind=engine, expire_on_commit=False |
| ) |
| Base = declarative_base() |
| Session = scoped_session(SessionLocal) |
|
|
|
|
| |
| def get_session(): |
| db = SessionLocal() |
| try: |
| yield db |
| finally: |
| db.close() |
|
|
|
|
| get_db = contextmanager(get_session) |
|
|