Get the database engine. Args: url: the DB url to use. Returns: The database engine. Raises: ValueError: If the database url is None.
(url: str | None = None)
| 87 | return kwargs |
| 88 | |
| 89 | def get_engine(url: str | None = None) -> sqlalchemy.engine.Engine: |
| 90 | """Get the database engine. |
| 91 | |
| 92 | Args: |
| 93 | url: the DB url to use. |
| 94 | |
| 95 | Returns: |
| 96 | The database engine. |
| 97 | |
| 98 | Raises: |
| 99 | ValueError: If the database url is None. |
| 100 | """ |
| 101 | conf = get_config() |
| 102 | url = url or conf.db_url |
| 103 | if url is None: |
| 104 | msg = "No database url configured" |
| 105 | raise ValueError(msg) |
| 106 | |
| 107 | global _ENGINE |
| 108 | if url in _ENGINE: |
| 109 | return _ENGINE[url] |
| 110 | |
| 111 | if not environment.ALEMBIC_CONFIG.get().exists(): |
| 112 | console.warn( |
| 113 | "Database is not initialized, run [bold]reflex db init[/bold] first.", |
| 114 | dedupe=True, |
| 115 | ) |
| 116 | _ENGINE[url] = sqlalchemy.engine.create_engine( |
| 117 | url, |
| 118 | **get_engine_args(url), |
| 119 | ) |
| 120 | return _ENGINE[url] |
| 121 | |
| 122 | def create_all(): |
| 123 | """Create all the tables.""" |