Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev #3669

Merged
merged 30 commits into from
Jul 6, 2024
Merged

dev #3669

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift click to select a range
4e433d9
wip: citations via __event_emitter__
michaelpoluektov Jul 3, 2024
f6dcffa
fix: pinned chat delete issue
tjbck Jul 4, 2024
8646460
refac
tjbck Jul 4, 2024
37a5d2c
Update db.py
tjbck Jul 4, 2024
8fe2a7b
fix
tjbck Jul 4, 2024
8b13755
Update auths.py
tjbck Jul 4, 2024
9a6cbaf
fix: user valves
tjbck Jul 4, 2024
740b6f5
fix: pull model
tjbck Jul 4, 2024
0527755
use data field
michaelpoluektov Jul 4, 2024
d20601d
feat: Add custom Collapsible component for collapsible content
ricky-davis Jul 4, 2024
2389c36
refactor: Update WebSearchResults.svelte to use new CollapsibleComponent
ricky-davis Jul 4, 2024
d5c0876
refactor: fixed new Collapsible Component to allow passed in classes
ricky-davis Jul 4, 2024
db58bb5
refactor: Removed dependency
ricky-davis Jul 4, 2024
78ba18a
refactor: Update Collapsible component to include dynamic margin for …
ricky-davis Jul 4, 2024
f611533
i18n: Update Chinese translation
KarlLee830 Jul 4, 2024
ca3f8e6
chore: format
ricky-davis Jul 4, 2024
55b7c30
simplify citation API
michaelpoluektov Jul 4, 2024
67c2ab0
fix: pipe custom model
tjbck Jul 4, 2024
8381346
enh: add sideways scrolling to settings tabs container
Peter-De-Ath Jul 5, 2024
45fae65
Merge pull request #3630 from KarlLee830/translate
tjbck Jul 5, 2024
8fd128e
i18n: Update Catalan Translation
Jul 5, 2024
983fe4b
refac
tjbck Jul 6, 2024
3928ac1
Merge pull request #3615 from michaelpoluektov/citations-event
tjbck Jul 6, 2024
89e6044
Merge pull request #3657 from aleixdorca/dev
tjbck Jul 6, 2024
c3c15cb
refac
tjbck Jul 6, 2024
70efbef
Merge pull request #3636 from Peter-De-Ath/add-scroll-to-settings-tabs
tjbck Jul 6, 2024
73899e1
refac
tjbck Jul 6, 2024
97a8491
Merge pull request #3631 from ricky-davis/CustomCollapsible
tjbck Jul 6, 2024
d60f066
Merge pull request #3668 from open-webui/dev
tjbck Jul 6, 2024
1436bb7
enh: handle peewee migration
tjbck Jul 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
enh: handle peewee migration
  • Loading branch information
tjbck committed Jul 6, 2024
commit 1436bb7c61b1df4dba2b5b383ecb8c86ec452f37
36 changes: 33 additions & 3 deletions backend/apps/webui/internal/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 2,10 @@
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

Expand Down Expand Up @@ -46,6 50,35 @@ def python_value(self, value):
else:
pass


# Workaround to handle the peewee migration
# This is required to ensure the peewee migration is handled before the alembic migration
def handle_peewee_migration():
try:
db = register_connection(DATABASE_URL)
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."


handle_peewee_migration()


SQLALCHEMY_DATABASE_URL = DATABASE_URL
if "sqlite" in SQLALCHEMY_DATABASE_URL:
engine = create_engine(
Expand All @@ -62,9 95,6 @@ def python_value(self, value):
Session = scoped_session(SessionLocal)


from contextlib import contextmanager


# Dependency
def get_session():
db = SessionLocal()
Expand Down
72 changes: 72 additions & 0 deletions backend/apps/webui/internal/wrappers.py
Original file line number Diff line number Diff line change
@@ -0,0 1,72 @@
from contextvars import ContextVar
from peewee import *
from peewee import PostgresqlDatabase, InterfaceError as PeeWeeInterfaceError

import logging
from playhouse.db_url import connect, parse
from playhouse.shortcuts import ReconnectMixin

from config import SRC_LOG_LEVELS

log = logging.getLogger(__name__)
log.setLevel(SRC_LOG_LEVELS["DB"])

db_state_default = {"closed": None, "conn": None, "ctx": None, "transactions": None}
db_state = ContextVar("db_state", default=db_state_default.copy())


class PeeweeConnectionState(object):
def __init__(self, **kwargs):
super().__setattr__("_state", db_state)
super().__init__(**kwargs)

def __setattr__(self, name, value):
self._state.get()[name] = value

def __getattr__(self, name):
value = self._state.get()[name]
return value


class CustomReconnectMixin(ReconnectMixin):
reconnect_errors = (
# psycopg2
(OperationalError, "termin"),
(InterfaceError, "closed"),
# peewee
(PeeWeeInterfaceError, "closed"),
)


class ReconnectingPostgresqlDatabase(CustomReconnectMixin, PostgresqlDatabase):
pass


def register_connection(db_url):
db = connect(db_url)
if isinstance(db, PostgresqlDatabase):
# Enable autoconnect for SQLite databases, managed by Peewee
db.autoconnect = True
db.reuse_if_open = True
log.info("Connected to PostgreSQL database")

# Get the connection details
connection = parse(db_url)

# Use our custom database class that supports reconnection
db = ReconnectingPostgresqlDatabase(
connection["database"],
user=connection["user"],
password=connection["password"],
host=connection["host"],
port=connection["port"],
)
db.connect(reuse_if_open=True)
elif isinstance(db, SqliteDatabase):
# Enable autoconnect for SQLite databases, managed by Peewee
db.autoconnect = True
db.reuse_if_open = True
log.info("Connected to SQLite database")
else:
raise ValueError("Unsupported database connection")
return db