Configures the default connection with a preexisting :class:`cassandra.cluster.Session` Note: the mapper presently requires a Session :attr:`~.row_factory` set to ``dict_factory``. This may be relaxed in the future
(s)
| 272 | |
| 273 | |
| 274 | def set_session(s): |
| 275 | """ |
| 276 | Configures the default connection with a preexisting :class:`cassandra.cluster.Session` |
| 277 | |
| 278 | Note: the mapper presently requires a Session :attr:`~.row_factory` set to ``dict_factory``. |
| 279 | This may be relaxed in the future |
| 280 | """ |
| 281 | |
| 282 | try: |
| 283 | conn = get_connection() |
| 284 | except CQLEngineException: |
| 285 | # no default connection set; initialize one |
| 286 | register_connection('default', session=s, default=True) |
| 287 | conn = get_connection() |
| 288 | else: |
| 289 | if conn.session: |
| 290 | log.warning("configuring new default session for cqlengine when one was already set") |
| 291 | |
| 292 | if not any([ |
| 293 | s.cluster.profile_manager.default.row_factory is dict_factory and s.cluster._config_mode in [_ConfigMode.PROFILES, _ConfigMode.UNCOMMITTED], |
| 294 | s.row_factory is dict_factory and s.cluster._config_mode in [_ConfigMode.LEGACY, _ConfigMode.UNCOMMITTED], |
| 295 | ]): |
| 296 | raise CQLEngineException("Failed to initialize: row_factory must be 'dict_factory'") |
| 297 | |
| 298 | conn.session = s |
| 299 | conn.cluster = s.cluster |
| 300 | |
| 301 | # Set default keyspace from given session's keyspace |
| 302 | if conn.session.keyspace: |
| 303 | from cassandra.cqlengine import models |
| 304 | models.DEFAULT_KEYSPACE = conn.session.keyspace |
| 305 | |
| 306 | conn.setup_session() |
| 307 | |
| 308 | log.debug("cqlengine default connection initialized with %s", s) |
| 309 | |
| 310 | |
| 311 | # TODO next major: if a cloud config is specified in kwargs, hosts will be ignored. |