diff --git a/reflex/config.py b/reflex/config.py index 346300e79..7a29c811a 100644 --- a/reflex/config.py +++ b/reflex/config.py @@ -625,7 +625,7 @@ class Config(Base): db_url: Optional[str] = "sqlite:///reflex.db" # The async database url used by rx.Model. - async_db_url: Optional[str] = "sqlite+aiosqlite:///reflex.db" + async_db_url: Optional[str] = None # The redis url redis_url: Optional[str] = None diff --git a/reflex/model.py b/reflex/model.py index c49e89e66..334c15987 100644 --- a/reflex/model.py +++ b/reflex/model.py @@ -2,6 +2,7 @@ from __future__ import annotations +import re from collections import defaultdict from typing import Any, ClassVar, Optional, Type, Union @@ -26,6 +27,21 @@ _ENGINE: dict[str, sqlalchemy.engine.Engine] = {} _ASYNC_ENGINE: dict[str, sqlalchemy.engine.Engine] = {} _AsyncSessionLocal: dict[str | None, sqlalchemy.orm.sessionmaker] = {} +# Import asyncio session _after_ reflex.utils.compat +import sqlmodel.ext.asyncio.session # noqa: E402 + + +def _safe_db_url_for_logging(url: str) -> str: + """Remove username and password from the database URL for logging. + + Args: + url: The database URL. + + Returns: + The database URL with the username and password removed. + """ + return re.sub(r"://[^@]+@", "://:@", url) + def get_engine_args(url: str | None = None) -> dict[str, Any]: """Get the database engine arguments. @@ -94,8 +110,18 @@ def get_async_engine(url: str | None) -> sqlalchemy.engine.Engine: Raises: ValueError: If the async database url is None. """ - conf = get_config() - url = url or conf.async_db_url + if url is None: + conf = get_config() + url = conf.async_db_url + if url is not None and conf.db_url is not None: + async_db_url_tail = url.partition("://")[2] + db_url_tail = conf.db_url.partition("://")[2] + if async_db_url_tail != db_url_tail: + console.warn( + f"async_db_url `{_safe_db_url_for_logging(url)}` " + "should reference the same database as " + f"db_url `{_safe_db_url_for_logging(conf.db_url)}`." + ) if url is None: raise ValueError("No async database url configured")