Get the async database engine. Args: url: The database url. Returns: The async database engine. Raises: ValueError: If the async database url is None.
(url: str | None)
| 125 | ModelRegistry.get_metadata().create_all(engine) |
| 126 | |
| 127 | def get_async_engine(url: str | None) -> sqlalchemy.ext.asyncio.AsyncEngine: |
| 128 | """Get the async database engine. |
| 129 | |
| 130 | Args: |
| 131 | url: The database url. |
| 132 | |
| 133 | Returns: |
| 134 | The async database engine. |
| 135 | |
| 136 | Raises: |
| 137 | ValueError: If the async database url is None. |
| 138 | """ |
| 139 | if url is None: |
| 140 | conf = get_config() |
| 141 | url = conf.async_db_url |
| 142 | if url is not None and conf.db_url is not None: |
| 143 | async_db_url_tail = url.partition("://")[2] |
| 144 | db_url_tail = conf.db_url.partition("://")[2] |
| 145 | if async_db_url_tail != db_url_tail: |
| 146 | console.warn( |
| 147 | f"async_db_url `{_safe_db_url_for_logging(url)}` " |
| 148 | "should reference the same database as " |
| 149 | f"db_url `{_safe_db_url_for_logging(conf.db_url)}`." |
| 150 | ) |
| 151 | if url is None: |
| 152 | msg = "No async database url configured" |
| 153 | raise ValueError(msg) |
| 154 | |
| 155 | global _ASYNC_ENGINE |
| 156 | if url in _ASYNC_ENGINE: |
| 157 | return _ASYNC_ENGINE[url] |
| 158 | |
| 159 | if not environment.ALEMBIC_CONFIG.get().exists(): |
| 160 | console.warn( |
| 161 | "Database is not initialized, run [bold]reflex db init[/bold] first.", |
| 162 | dedupe=True, |
| 163 | ) |
| 164 | _ASYNC_ENGINE[url] = sqlalchemy.ext.asyncio.create_async_engine( |
| 165 | url, |
| 166 | **get_engine_args(url), |
| 167 | ) |
| 168 | return _ASYNC_ENGINE[url] |
| 169 | |
| 170 | def sqla_session(url: str | None = None) -> sqlalchemy.orm.Session: |
| 171 | """Get a bare sqlalchemy session to interact with the database. |
no test coverage detected