|
3 | 3 | import logging
|
4 | 4 | import sys
|
5 | 5 | import typing as t
|
| 6 | +import sqlmesh.core.dialect as d |
| 7 | +from sqlglot.optimizer.simplify import gen |
6 | 8 | from pathlib import Path
|
7 | 9 | from sqlmesh.core import constants as c
|
8 | 10 | from sqlmesh.core.config import (
|
|
11 | 13 | GatewayConfig,
|
12 | 14 | ModelDefaultsConfig,
|
13 | 15 | )
|
| 16 | +from sqlmesh.core.environment import EnvironmentStatements |
14 | 17 | from sqlmesh.core.loader import CacheBase, LoadedProject, Loader
|
15 | 18 | from sqlmesh.core.macros import MacroRegistry, macro
|
16 | 19 | from sqlmesh.core.model import Model, ModelCache
|
| 20 | +from sqlmesh.core.model.common import make_python_env |
17 | 21 | from sqlmesh.core.signal import signal
|
18 | 22 | from sqlmesh.dbt.basemodel import BMC, BaseModelConfig
|
19 | 23 | from sqlmesh.dbt.context import DbtContext
|
|
23 | 27 | from sqlmesh.dbt.target import TargetConfig
|
24 | 28 | from sqlmesh.utils import UniqueKeyDict
|
25 | 29 | from sqlmesh.utils.errors import ConfigError
|
26 |
| -from sqlmesh.utils.jinja import JinjaMacroRegistry |
| 30 | +from sqlmesh.utils.jinja import ( |
| 31 | + JinjaMacroRegistry, |
| 32 | + MacroInfo, |
| 33 | + extract_macro_references_and_variables, |
| 34 | +) |
27 | 35 |
|
28 | 36 | if sys.version_info >= (3, 12):
|
29 | 37 | from importlib import metadata
|
@@ -230,6 +238,60 @@ def _load_requirements(self) -> t.Tuple[t.Dict[str, str], t.Set[str]]:
|
230 | 238 |
|
231 | 239 | return requirements, excluded_requirements
|
232 | 240 |
|
| 241 | + def _load_environment_statements(self, macros: MacroRegistry) -> EnvironmentStatements | None: |
| 242 | + """Loads dbt's on_run_start, on_run_end hooks into sqlmesh's before_all, after_all statements respectively.""" |
| 243 | + |
| 244 | + on_run_start: t.List[str] = [] |
| 245 | + on_run_end: t.List[str] = [] |
| 246 | + jinja_root_macros: t.Dict[str, MacroInfo] = {} |
| 247 | + variables: t.Dict[str, t.Any] = self._get_variables() |
| 248 | + dialect = self.config.dialect |
| 249 | + for project in self._load_projects(): |
| 250 | + context = project.context.copy() |
| 251 | + if manifest := context._manifest: |
| 252 | + on_run_start.extend(manifest._on_run_start or []) |
| 253 | + on_run_end.extend(manifest._on_run_end or []) |
| 254 | + |
| 255 | + if root_package := context.jinja_macros.root_package_name: |
| 256 | + if root_macros := context.jinja_macros.packages.get(root_package): |
| 257 | + jinja_root_macros |= root_macros |
| 258 | + context.set_and_render_variables(context.variables, root_package) |
| 259 | + variables |= context.variables |
| 260 | + |
| 261 | + if statements := on_run_start + on_run_end: |
| 262 | + jinja_macro_references, used_variables = extract_macro_references_and_variables( |
| 263 | + *(gen(stmt) for stmt in statements) |
| 264 | + ) |
| 265 | + jinja_macros = context.jinja_macros |
| 266 | + jinja_macros.root_macros = jinja_root_macros |
| 267 | + jinja_macros = ( |
| 268 | + jinja_macros.trim(jinja_macro_references) |
| 269 | + if not jinja_macros.trimmed |
| 270 | + else jinja_macros |
| 271 | + ) |
| 272 | + |
| 273 | + python_env = make_python_env( |
| 274 | + [s for stmt in statements for s in d.parse(stmt, default_dialect=dialect)], |
| 275 | + jinja_macro_references=jinja_macro_references, |
| 276 | + module_path=self.config_path, |
| 277 | + macros=macros, |
| 278 | + variables=variables, |
| 279 | + used_variables=used_variables, |
| 280 | + path=self.config_path, |
| 281 | + ) |
| 282 | + |
| 283 | + return EnvironmentStatements( |
| 284 | + before_all=[ |
| 285 | + d.jinja_statement(stmt).sql(dialect=dialect) for stmt in on_run_start or [] |
| 286 | + ], |
| 287 | + after_all=[ |
| 288 | + d.jinja_statement(stmt).sql(dialect=dialect) for stmt in on_run_end or [] |
| 289 | + ], |
| 290 | + python_env=python_env, |
| 291 | + jinja_macros=jinja_macros, |
| 292 | + ) |
| 293 | + return None |
| 294 | + |
233 | 295 | def _compute_yaml_max_mtime_per_subfolder(self, root: Path) -> t.Dict[Path, float]:
|
234 | 296 | if not root.is_dir():
|
235 | 297 | return {}
|
|
0 commit comments