|
23 | 23 | from sqlmesh.core.metric import Metric, MetricMeta, expand_metrics, load_metric_ddl |
24 | 24 | from sqlmesh.core.model import ( |
25 | 25 | Model, |
26 | | - ExternalModel, |
27 | 26 | ModelCache, |
28 | 27 | SeedModel, |
29 | 28 | create_external_model, |
@@ -208,32 +207,41 @@ def _load_external_models( |
208 | 207 | if external_models_path.exists() and external_models_path.is_dir(): |
209 | 208 | paths_to_load.extend(self._glob_paths(external_models_path, extension=".yaml")) |
210 | 209 |
|
| 210 | + cache = SqlMeshLoader._Cache(self, self.config_path) |
| 211 | + |
| 212 | + def _load() -> t.List[Model]: |
| 213 | + try: |
| 214 | + with open(path, "r", encoding="utf-8") as file: |
| 215 | + return [ |
| 216 | + create_external_model( |
| 217 | + defaults=self.config.model_defaults.dict(), |
| 218 | + path=path, |
| 219 | + project=self.config.project, |
| 220 | + audit_definitions=audits, |
| 221 | + **{ |
| 222 | + "dialect": self.config.model_defaults.dialect, |
| 223 | + "default_catalog": self.context.default_catalog, |
| 224 | + **row, |
| 225 | + }, |
| 226 | + ) |
| 227 | + for row in YAML().load(file.read()) |
| 228 | + ] |
| 229 | + except Exception as ex: |
| 230 | + raise ConfigError(f"Failed to load model definition at '{path}'.\n{ex}") |
| 231 | + |
211 | 232 | for path in paths_to_load: |
212 | 233 | self._track_file(path) |
213 | 234 |
|
214 | | - with open(path, "r", encoding="utf-8") as file: |
215 | | - external_models: t.List[ExternalModel] = [] |
216 | | - for row in YAML().load(file.read()): |
217 | | - model = create_external_model( |
218 | | - defaults=self.config.model_defaults.dict(), |
219 | | - path=path, |
220 | | - project=self.config.project, |
221 | | - audit_definitions=audits, |
222 | | - **{ |
223 | | - "dialect": self.config.model_defaults.dialect, |
224 | | - "default_catalog": self.context.default_catalog, |
225 | | - **row, |
226 | | - }, |
227 | | - ) |
228 | | - external_models.append(model) |
229 | | - |
230 | | - # external models with no explicit gateway defined form the base set |
231 | | - for model in (e for e in external_models if e.gateway is None): |
| 235 | + external_models = cache.get_or_load_models(path, _load) |
| 236 | + # external models with no explicit gateway defined form the base set |
| 237 | + for model in external_models: |
| 238 | + if model.gateway is None: |
232 | 239 | models[model.fqn] = model |
233 | 240 |
|
234 | | - # however, if there is a gateway defined, gateway-specific models take precedence |
235 | | - if gateway: |
236 | | - for model in (e for e in external_models if e.gateway == gateway): |
| 241 | + # however, if there is a gateway defined, gateway-specific models take precedence |
| 242 | + if gateway: |
| 243 | + for model in external_models: |
| 244 | + if model.gateway == gateway: |
237 | 245 | models.update({model.fqn: model}) |
238 | 246 |
|
239 | 247 | return models |
|
0 commit comments