Return the connection object for the given alias, creating it if needed. If the connection's event loop has changed (e.g., in a test with a new event loop), the connection is transparently replaced with a fresh one and a :class:`TortoiseLoopSwitchWarning<tortoise.wa
(self, conn_alias: str)
| 153 | return connection |
| 154 | |
| 155 | def get(self, conn_alias: str) -> BaseDBAsyncClient: |
| 156 | """ |
| 157 | Return the connection object for the given alias, creating it if needed. |
| 158 | |
| 159 | If the connection's event loop has changed (e.g., in a test with a new event loop), |
| 160 | the connection is transparently replaced with a fresh one and a |
| 161 | :class:`TortoiseLoopSwitchWarning<tortoise.warnings.TortoiseLoopSwitchWarning>` |
| 162 | is emitted. |
| 163 | |
| 164 | Used for accessing the low-level connection object |
| 165 | (:class:`BaseDBAsyncClient<tortoise.backends.base.client.BaseDBAsyncClient>`) for the |
| 166 | given alias. |
| 167 | |
| 168 | :param conn_alias: The alias for which the connection has to be fetched |
| 169 | |
| 170 | :raises ConfigurationError: If the connection alias does not exist. |
| 171 | """ |
| 172 | storage = self._get_storage() |
| 173 | try: |
| 174 | conn = storage[conn_alias] |
| 175 | if not conn._check_loop(): |
| 176 | from tortoise.warnings import TortoiseLoopSwitchWarning |
| 177 | |
| 178 | warnings.warn( |
| 179 | f"Tortoise connection '{conn_alias}' was created on a different " |
| 180 | f"event loop and will be reconnected. If this is expected (e.g., " |
| 181 | f"in tests), use tortoise_test_context() or suppress with: " |
| 182 | f"warnings.filterwarnings('ignore', " |
| 183 | f"category=TortoiseLoopSwitchWarning)", |
| 184 | TortoiseLoopSwitchWarning, |
| 185 | stacklevel=2, |
| 186 | ) |
| 187 | conn = self._create_connection(conn_alias) |
| 188 | storage[conn_alias] = conn |
| 189 | return conn |
| 190 | except KeyError: |
| 191 | connection: BaseDBAsyncClient = self._create_connection(conn_alias) |
| 192 | storage[conn_alias] = connection |
| 193 | return connection |
| 194 | |
| 195 | def set(self, conn_alias: str, conn_obj: BaseDBAsyncClient) -> ConnectionToken: |
| 196 | """ |
no test coverage detected