@@ -369,7 +369,10 @@ def __init__(
369369 self ._environment_statements : t .List [EnvironmentStatements ] = []
370370 self ._excluded_requirements : t .Set [str ] = set ()
371371 self ._default_catalog : t .Optional [str ] = None
372+ self ._default_catalog_per_gateway : t .Optional [t .Dict [str , str ]] = None
372373 self ._engine_adapter : t .Optional [EngineAdapter ] = None
374+ self ._connection_config : t .Optional [ConnectionConfig ] = None
375+ self ._test_connection_config : t .Optional [ConnectionConfig ] = None
373376 self ._linters : t .Dict [str , Linter ] = {}
374377 self ._loaded : bool = False
375378
@@ -412,7 +415,7 @@ def __init__(
412415
413416 self ._concurrent_tasks = concurrent_tasks
414417 self ._state_connection_config = (
415- self .config .get_state_connection (self .gateway ) or self ._connection_config
418+ self .config .get_state_connection (self .gateway ) or self .connection_config
416419 )
417420
418421 self ._snapshot_evaluator : t .Optional [SnapshotEvaluator ] = None
@@ -440,7 +443,7 @@ def default_dialect(self) -> t.Optional[str]:
440443 def engine_adapter (self ) -> EngineAdapter :
441444 """Returns the default engine adapter."""
442445 if self ._engine_adapter is None :
443- self ._engine_adapter = self ._connection_config .create_engine_adapter ()
446+ self ._engine_adapter = self .connection_config .create_engine_adapter ()
444447 return self ._engine_adapter
445448
446449 @property
@@ -965,8 +968,8 @@ def requirements(self) -> t.Dict[str, str]:
965968
966969 @property
967970 def default_catalog (self ) -> t .Optional [str ]:
968- if self ._default_catalog is None :
969- self ._default_catalog = self ._scheduler . get_default_catalog ( self )
971+ if self ._default_catalog is None and self . default_catalog_per_gateway :
972+ self ._default_catalog = self .default_catalog_per_gateway [ self . selected_gateway ]
970973 return self ._default_catalog
971974
972975 @python_api_analytics
@@ -1978,7 +1981,7 @@ def create_test(
19781981
19791982 try :
19801983 model_to_test = self .get_model (model , raise_if_missing = True )
1981- test_adapter = self ._test_connection_config .create_engine_adapter (
1984+ test_adapter = self .test_connection_config .create_engine_adapter (
19821985 register_comments_override = False
19831986 )
19841987
@@ -2463,7 +2466,7 @@ def _run_plan_tests(
24632466 self .console .log_test_results (
24642467 result ,
24652468 test_output ,
2466- self ._test_connection_config ._engine_adapter .DIALECT ,
2469+ self .test_connection_config ._engine_adapter .DIALECT ,
24672470 )
24682471 if not result .wasSuccessful ():
24692472 raise PlanError (
@@ -2507,30 +2510,45 @@ def engine_adapters(self) -> t.Dict[str, EngineAdapter]:
25072510 @cached_property
25082511 def default_catalog_per_gateway (self ) -> t .Dict [str , str ]:
25092512 """Returns the default catalogs for each engine adapter."""
2510- if self .gateway_managed_virtual_layer :
2511- return self ._scheduler .get_default_catalog_per_gateway (self )
2512- return {}
2513+ if self ._default_catalog_per_gateway is None :
2514+ self ._default_catalog_per_gateway = self ._scheduler .get_default_catalog_per_gateway (
2515+ self
2516+ )
2517+ return self ._default_catalog_per_gateway
25132518
25142519 @cached_property
25152520 def concurrent_tasks (self ) -> int :
25162521 if self ._concurrent_tasks is None :
2517- self ._concurrent_tasks = self ._connection_config .concurrent_tasks
2522+ self ._concurrent_tasks = self .connection_config .concurrent_tasks
25182523 return self ._concurrent_tasks
25192524
25202525 @cached_property
2521- def _connection_config (self ) -> ConnectionConfig :
2522- return self .config .get_connection (self .gateway )
2526+ def connection_config (self ) -> ConnectionConfig :
2527+ if self ._connection_config is None :
2528+ self ._connection_config = self .config .get_connection (self .selected_gateway )
2529+ return self ._connection_config
25232530
25242531 @cached_property
2525- def _test_connection_config (self ) -> ConnectionConfig :
2526- return self .config .get_test_connection (
2527- self .gateway , self .default_catalog , default_catalog_dialect = self .engine_adapter .DIALECT
2528- )
2532+ def test_connection_config (self ) -> ConnectionConfig :
2533+ if self ._test_connection_config is None :
2534+ self ._test_connection_config = self .config .get_test_connection (
2535+ self .gateway ,
2536+ self .default_catalog ,
2537+ default_catalog_dialect = self .engine_adapter .DIALECT ,
2538+ )
2539+ return self ._test_connection_config
25292540
25302541 @cached_property
25312542 def environment_catalog_mapping (self ) -> RegexKeyDict :
2543+ engine_adapter = None
2544+ try :
2545+ engine_adapter = self .engine_adapter
2546+ except Exception :
2547+ pass
2548+
25322549 if (
25332550 self .config .environment_catalog_mapping
2551+ and engine_adapter
25342552 and not self .engine_adapter .catalog_support .is_multi_catalog_supported
25352553 ):
25362554 raise SQLMeshError (
0 commit comments