2024-03-02 08:19:24 +00:00
|
|
|
import os
|
2024-03-20 23:11:36 +00:00
|
|
|
import logging
|
2024-05-30 13:44:13 +00:00
|
|
|
import json
|
2024-06-21 12:58:57 +00:00
|
|
|
from contextlib import contextmanager
|
2024-07-06 06:38:53 +00:00
|
|
|
|
|
|
|
from peewee_migrate import Router
|
|
|
|
from apps.webui.internal.wrappers import register_connection
|
|
|
|
|
2024-06-18 13:03:31 +00:00
|
|
|
from typing import Optional, Any
|
|
|
|
from typing_extensions import Self
|
2023-12-26 05:44:28 +00:00
|
|
|
|
2024-06-18 13:03:31 +00:00
|
|
|
from sqlalchemy import create_engine, types, Dialect
|
|
|
|
from sqlalchemy.ext.declarative import declarative_base
|
2024-06-24 08:54:18 +00:00
|
|
|
from sqlalchemy.orm import sessionmaker, scoped_session
|
2024-06-18 13:03:31 +00:00
|
|
|
from sqlalchemy.sql.type_api import _T
|
2024-05-30 11:55:58 +00:00
|
|
|
|
2024-05-30 13:44:13 +00:00
|
|
|
from config import SRC_LOG_LEVELS, DATA_DIR, DATABASE_URL, BACKEND_DIR
|
2024-05-30 11:55:58 +00:00
|
|
|
|
2024-03-20 23:11:36 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
log.setLevel(SRC_LOG_LEVELS["DB"])
|
2024-01-19 20:13:09 +00:00
|
|
|
|
2024-06-20 11:53:23 +00:00
|
|
|
|
2024-06-18 13:03:31 +00:00
|
|
|
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)
|
|
|
|
|
2024-05-21 21:05:16 +00:00
|
|
|
def db_value(self, value):
|
|
|
|
return json.dumps(value)
|
|
|
|
|
|
|
|
def python_value(self, value):
|
|
|
|
if value is not None:
|
|
|
|
return json.loads(value)
|
|
|
|
|
2024-06-20 11:53:23 +00:00
|
|
|
|
2024-03-02 08:19:24 +00:00
|
|
|
# Check if the file exists
|
|
|
|
if os.path.exists(f"{DATA_DIR}/ollama.db"):
|
|
|
|
# Rename the file
|
|
|
|
os.rename(f"{DATA_DIR}/ollama.db", f"{DATA_DIR}/webui.db")
|
2024-04-24 17:10:18 +00:00
|
|
|
log.info("Database migrated from Ollama-WebUI successfully.")
|
2024-03-02 08:19:24 +00:00
|
|
|
else:
|
|
|
|
pass
|
|
|
|
|
2024-07-06 06:38:53 +00:00
|
|
|
|
|
|
|
# Workaround to handle the peewee migration
|
|
|
|
# This is required to ensure the peewee migration is handled before the alembic migration
|
2024-07-08 19:42:52 +00:00
|
|
|
def handle_peewee_migration(DATABASE_URL):
|
2024-07-06 06:38:53 +00:00
|
|
|
try:
|
2024-07-08 19:42:52 +00:00
|
|
|
# Replace the postgresql:// with postgres:// and %40 with @ in the DATABASE_URL
|
|
|
|
db = register_connection(
|
|
|
|
DATABASE_URL.replace("postgresql://", "postgres://").replace("%40", "@")
|
|
|
|
)
|
2024-07-06 06:38:53 +00:00
|
|
|
migrate_dir = BACKEND_DIR / "apps" / "webui" / "internal" / "migrations"
|
|
|
|
router = Router(db, logger=log, migrate_dir=migrate_dir)
|
|
|
|
router.run()
|
|
|
|
db.close()
|
|
|
|
|
|
|
|
# check if db connection has been closed
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
log.error(f"Failed to initialize the database connection: {e}")
|
|
|
|
raise
|
|
|
|
|
|
|
|
finally:
|
|
|
|
# Properly closing the database connection
|
|
|
|
if db and not db.is_closed():
|
|
|
|
db.close()
|
|
|
|
|
|
|
|
# Assert if db connection has been closed
|
|
|
|
assert db.is_closed(), "Database connection is still open."
|
|
|
|
|
|
|
|
|
2024-07-08 19:42:52 +00:00
|
|
|
handle_peewee_migration(DATABASE_URL)
|
2024-07-06 06:38:53 +00:00
|
|
|
|
|
|
|
|
2024-06-18 13:03:31 +00:00
|
|
|
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)
|
2024-07-04 06:32:46 +00:00
|
|
|
|
|
|
|
|
2024-06-24 07:57:08 +00:00
|
|
|
SessionLocal = sessionmaker(
|
|
|
|
autocommit=False, autoflush=False, bind=engine, expire_on_commit=False
|
|
|
|
)
|
2024-06-18 13:03:31 +00:00
|
|
|
Base = declarative_base()
|
2024-06-24 11:06:15 +00:00
|
|
|
Session = scoped_session(SessionLocal)
|
2024-07-04 06:32:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Dependency
|
2024-07-04 06:39:16 +00:00
|
|
|
def get_session():
|
2024-07-04 06:32:46 +00:00
|
|
|
db = SessionLocal()
|
|
|
|
try:
|
|
|
|
yield db
|
|
|
|
finally:
|
|
|
|
db.close()
|
2024-07-04 06:39:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
get_db = contextmanager(get_session)
|