db.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import os
  2. import json
  3. import logging
  4. from contextlib import contextmanager
  5. from typing import Any, Optional
  6. from open_webui.internal.wrappers import register_connection
  7. from open_webui.env import (
  8. OPEN_WEBUI_DIR,
  9. DATABASE_URL,
  10. DATABASE_SCHEMA,
  11. SRC_LOG_LEVELS,
  12. DATABASE_POOL_MAX_OVERFLOW,
  13. DATABASE_POOL_RECYCLE,
  14. DATABASE_POOL_SIZE,
  15. DATABASE_POOL_TIMEOUT,
  16. )
  17. from peewee_migrate import Router
  18. from sqlalchemy import Dialect, create_engine, MetaData, types
  19. from sqlalchemy.ext.declarative import declarative_base
  20. from sqlalchemy.orm import scoped_session, sessionmaker
  21. from sqlalchemy.pool import QueuePool, NullPool
  22. from sqlalchemy.sql.type_api import _T
  23. from typing_extensions import Self
  24. log = logging.getLogger(__name__)
  25. log.setLevel(SRC_LOG_LEVELS["DB"])
  26. class JSONField(types.TypeDecorator):
  27. impl = types.Text
  28. cache_ok = True
  29. def process_bind_param(self, value: Optional[_T], dialect: Dialect) -> Any:
  30. return json.dumps(value)
  31. def process_result_value(self, value: Optional[_T], dialect: Dialect) -> Any:
  32. if value is not None:
  33. return json.loads(value)
  34. def copy(self, **kw: Any) -> Self:
  35. return JSONField(self.impl.length)
  36. def db_value(self, value):
  37. return json.dumps(value)
  38. def python_value(self, value):
  39. if value is not None:
  40. return json.loads(value)
  41. # Workaround to handle the peewee migration
  42. # This is required to ensure the peewee migration is handled before the alembic migration
  43. def handle_peewee_migration(DATABASE_URL):
  44. # db = None
  45. try:
  46. # Replace the postgresql:// with postgres:// to handle the peewee migration
  47. db = register_connection(DATABASE_URL.replace("postgresql://", "postgres://"))
  48. migrate_dir = OPEN_WEBUI_DIR / "internal" / "migrations"
  49. router = Router(db, logger=log, migrate_dir=migrate_dir)
  50. router.run()
  51. db.close()
  52. except Exception as e:
  53. log.error(f"Failed to initialize the database connection: {e}")
  54. log.warning(
  55. "Hint: If your database password contains special characters, you may need to URL-encode it."
  56. )
  57. raise
  58. finally:
  59. # Properly closing the database connection
  60. if db and not db.is_closed():
  61. db.close()
  62. # Assert if db connection has been closed
  63. assert db.is_closed(), "Database connection is still open."
  64. handle_peewee_migration(DATABASE_URL)
  65. SQLALCHEMY_DATABASE_URL = DATABASE_URL
  66. # Handle SQLCipher URLs
  67. if SQLALCHEMY_DATABASE_URL.startswith("sqlite+sqlcipher://"):
  68. database_password = os.environ.get("DATABASE_PASSWORD")
  69. if not database_password or database_password.strip() == "":
  70. raise ValueError(
  71. "DATABASE_PASSWORD is required when using sqlite+sqlcipher:// URLs"
  72. )
  73. # Extract database path from SQLCipher URL
  74. db_path = SQLALCHEMY_DATABASE_URL.replace("sqlite+sqlcipher://", "")
  75. if db_path.startswith("/"):
  76. db_path = db_path[1:] # Remove leading slash for relative paths
  77. # Create a custom creator function that uses sqlcipher3
  78. def create_sqlcipher_connection():
  79. import sqlcipher3
  80. conn = sqlcipher3.connect(db_path, check_same_thread=False)
  81. conn.execute(f"PRAGMA key = '{database_password}'")
  82. return conn
  83. engine = create_engine(
  84. "sqlite://", # Dummy URL since we're using creator
  85. creator=create_sqlcipher_connection,
  86. echo=False,
  87. )
  88. log.info("Connected to encrypted SQLite database using SQLCipher")
  89. elif "sqlite" in SQLALCHEMY_DATABASE_URL:
  90. engine = create_engine(
  91. SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
  92. )
  93. else:
  94. if isinstance(DATABASE_POOL_SIZE, int):
  95. if DATABASE_POOL_SIZE > 0:
  96. engine = create_engine(
  97. SQLALCHEMY_DATABASE_URL,
  98. pool_size=DATABASE_POOL_SIZE,
  99. max_overflow=DATABASE_POOL_MAX_OVERFLOW,
  100. pool_timeout=DATABASE_POOL_TIMEOUT,
  101. pool_recycle=DATABASE_POOL_RECYCLE,
  102. pool_pre_ping=True,
  103. poolclass=QueuePool,
  104. )
  105. else:
  106. engine = create_engine(
  107. SQLALCHEMY_DATABASE_URL, pool_pre_ping=True, poolclass=NullPool
  108. )
  109. else:
  110. engine = create_engine(SQLALCHEMY_DATABASE_URL, pool_pre_ping=True)
  111. SessionLocal = sessionmaker(
  112. autocommit=False, autoflush=False, bind=engine, expire_on_commit=False
  113. )
  114. metadata_obj = MetaData(schema=DATABASE_SCHEMA)
  115. Base = declarative_base(metadata=metadata_obj)
  116. Session = scoped_session(SessionLocal)
  117. def get_session():
  118. db = SessionLocal()
  119. try:
  120. yield db
  121. finally:
  122. db.close()
  123. get_db = contextmanager(get_session)