4646
4747
4848if t .TYPE_CHECKING :
49- from sqlmesh .core .config import Config
5049 from sqlmesh .core .context import GenericContext
5150
5251
@@ -104,21 +103,21 @@ def get(self, path: Path) -> t.List[Model]:
104103
105104_defaults : t .Optional [t .Dict [str , t .Any ]] = None
106105_cache : t .Optional [CacheBase ] = None
107- _config : t .Optional [Config ] = None
106+ _config_essentials : t .Optional [t . Dict [ str , t . Any ] ] = None
108107_selected_gateway : t .Optional [str ] = None
109108
110109
111110def _init_model_defaults (
112- config : Config ,
111+ config_essentials : t . Dict [ str , t . Any ] ,
113112 selected_gateway : t .Optional [str ],
114113 model_loading_defaults : t .Optional [t .Dict [str , t .Any ]] = None ,
115114 cache : t .Optional [CacheBase ] = None ,
116115 console : t .Optional [Console ] = None ,
117116) -> None :
118- global _defaults , _cache , _config , _selected_gateway
117+ global _defaults , _cache , _config_essentials , _selected_gateway
119118 _defaults = model_loading_defaults
120119 _cache = cache
121- _config = config
120+ _config_essentials = config_essentials
122121 _selected_gateway = selected_gateway
123122
124123 # Set the console passed from the parent process
@@ -140,22 +139,22 @@ def load_sql_models(path: Path) -> t.List[Model]:
140139
141140
142141def get_variables (gateway_name : t .Optional [str ] = None ) -> t .Dict [str , t .Any ]:
143- assert _config
142+ assert _config_essentials
144143
145144 gateway_name = gateway_name or _selected_gateway
146145
147146 try :
148- gateway = _config . get_gateway (gateway_name )
147+ gateway = _config_essentials [ "gateways" ]. get (gateway_name )
149148 except ConfigError :
150149 from sqlmesh .core .console import get_console
151150
152151 get_console ().log_warning (
153- f"Gateway '{ gateway_name } ' not found in project '{ _config . project } '."
152+ f"Gateway '{ gateway_name } ' not found in project '{ _config_essentials [ ' project' ] } '."
154153 )
155154 gateway = None
156155
157156 return {
158- ** _config . variables ,
157+ ** _config_essentials [ " variables" ] ,
159158 ** (gateway .variables if gateway else {}),
160159 c .GATEWAY : gateway_name ,
161160 }
@@ -174,7 +173,12 @@ def __init__(self, context: GenericContext, path: Path) -> None:
174173 self ._variables_by_gateway : t .Dict [str , t .Dict [str , t .Any ]] = {}
175174 self ._console = get_console ()
176175
177- _init_model_defaults (self .config , self .context .selected_gateway )
176+ self .config_essentials = {
177+ "project" : self .config .project ,
178+ "variables" : self .config .variables ,
179+ "gateways" : self .config .gateways ,
180+ }
181+ _init_model_defaults (self .config_essentials , self .context .selected_gateway )
178182
179183 def load (self ) -> LoadedProject :
180184 """
@@ -539,6 +543,7 @@ def _load_sql_models(
539543 """Loads the sql models into a Dict"""
540544 models : UniqueKeyDict [str , Model ] = UniqueKeyDict ("models" )
541545 paths : t .Set [Path ] = set ()
546+ cached_paths : UniqueKeyDict [Path , t .List [Model ]] = UniqueKeyDict ("cached_paths" )
542547
543548 for path in self ._glob_paths (
544549 self .config_path / c .MODELS ,
@@ -550,14 +555,14 @@ def _load_sql_models(
550555
551556 self ._track_file (path )
552557 paths .add (path )
558+ if cached_models := cache .get (path ):
559+ cached_paths [path ] = cached_models
553560
554- for path in paths .copy ():
555- cached_models = cache .get (path )
556- if cached_models :
557- paths .remove (path )
558- for model in cached_models :
559- if model .enabled :
560- models [model .fqn ] = model
561+ for path , cached_models in cached_paths .items ():
562+ paths .remove (path )
563+ for model in cached_models :
564+ if model .enabled :
565+ models [model .fqn ] = model
561566
562567 if paths :
563568 model_loading_defaults = dict (
@@ -578,10 +583,18 @@ def _load_sql_models(
578583 default_catalog_per_gateway = self .context .default_catalog_per_gateway ,
579584 )
580585
581- errors : t .List [str ] = []
586+ # if not c.MAX_FORK_WORKERS:
587+ # breakpoint()
588+
582589 with create_process_pool_executor (
583590 initializer = _init_model_defaults ,
584- initargs = (self .config , gateway , model_loading_defaults , cache , self ._console ),
591+ initargs = (
592+ self .config_essentials ,
593+ gateway ,
594+ model_loading_defaults ,
595+ cache ,
596+ self ._console ,
597+ ),
585598 max_workers = c .MAX_FORK_WORKERS ,
586599 ) as pool :
587600 futures_to_paths = {pool .submit (load_sql_models , path ): path for path in paths }
@@ -591,7 +604,7 @@ def _load_sql_models(
591604 loaded = future .result ()
592605 for model in loaded or cache .get (path ):
593606 if model .fqn in models :
594- errors . append (
607+ raise ConfigError (
595608 self ._failed_to_load_model_error (
596609 path , f"Duplicate SQL model name: '{ model .name } '."
597610 )
@@ -600,11 +613,7 @@ def _load_sql_models(
600613 model ._path = path
601614 models [model .fqn ] = model
602615 except Exception as ex :
603- errors .append (self ._failed_to_load_model_error (path , str (ex )))
604-
605- if errors :
606- error_string = "\n " .join (errors )
607- raise ConfigError (error_string )
616+ raise ConfigError (self ._failed_to_load_model_error (path , ex ))
608617
609618 return models
610619
0 commit comments