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

feat: Centralize database connection settings and deprecate old parameters #5960

Merged
merged 6 commits into from
Jan 29, 2025
Prev Previous commit
Next Next commit
fix: Resolve SQLAlchemy async engine pool configuration for SQLite
Explicitly set AsyncAdaptedQueuePool for SQLite connections to address potential async engine configuration issues. This ensures proper pool handling when creating database connections, particularly for SQLite databases.
ogabrielluiz committed Jan 27, 2025
commit 854c4633bf34b0733790e1c62722291062acd8f1
11 changes: 8 additions & 3 deletions src/backend/base/langflow/services/database/service.py
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@
from alembic import command, util
from alembic.config import Config
from loguru import logger
from sqlalchemy import event, exc, inspect
from sqlalchemy import AsyncAdaptedQueuePool, event, exc, inspect
from sqlalchemy.dialects import sqlite as dialect_sqlite
from sqlalchemy.engine import Engine
from sqlalchemy.exc import OperationalError
@@ -39,6 +39,7 @@ class DatabaseService(Service):
name = "database_service"

def __init__(self, settings_service: SettingsService):
self._logged_pragma = False
self.settings_service = settings_service
if settings_service.settings.database_url is None:
msg = "No database URL provided"
@@ -67,8 +68,6 @@ def __init__(self, settings_service: SettingsService):
else:
self.alembic_log_path = Path(langflow_dir) / alembic_log_file

self._logged_pragma = False

async def initialize_alembic_log_file(self):
# Ensure the directory and file for the alembic log file exists
await anyio.Path(self.alembic_log_path.parent).mkdir(parents=True, exist_ok=True)
@@ -119,13 +118,19 @@ def _create_engine(self) -> AsyncEngine:

if url_components[0].startswith("sqlite"):
scheme = "sqlite+aiosqlite"
# Even though the docs say this is the default, it raises an error
# if we don't specify it.
# https://docs.sqlalchemy.org/en/20/errors.html#pool-class-cannot-be-used-with-asyncio-engine-or-vice-versa
pool = AsyncAdaptedQueuePool
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: could do

kwargs["poolclass"] = AsyncAdaptedQueuePool

and remove pool = None in the else branch.

else:
scheme = "postgresql+psycopg" if url_components[0].startswith("postgresql") else url_components[0]
pool = None

database_url = f"{scheme}://{url_components[1]}"
return create_async_engine(
database_url,
connect_args=self._get_connect_args(),
poolclass=pool,
**kwargs,
)