-
Notifications
You must be signed in to change notification settings - Fork 358
Feat(dbt): Add support for on-run-start and on-run-end hooks #4044
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
0ff6643
e6b8cab
4a8c2a1
9a8b6a7
eba68b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| {% macro store_schemas(schemas) %} | ||
| create or replace table schema_table as select {{schemas}} as all_schemas; | ||
| {% endmacro %} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,8 @@ | |
| import logging | ||
| import sys | ||
| import typing as t | ||
| import sqlmesh.core.dialect as d | ||
| from sqlglot.optimizer.simplify import gen | ||
| from pathlib import Path | ||
| from sqlmesh.core import constants as c | ||
| from sqlmesh.core.config import ( | ||
|
|
@@ -11,9 +13,11 @@ | |
| GatewayConfig, | ||
| ModelDefaultsConfig, | ||
| ) | ||
| from sqlmesh.core.environment import EnvironmentStatements | ||
| from sqlmesh.core.loader import CacheBase, LoadedProject, Loader | ||
| from sqlmesh.core.macros import MacroRegistry, macro | ||
| from sqlmesh.core.model import Model, ModelCache | ||
| from sqlmesh.core.model.common import make_python_env | ||
| from sqlmesh.core.signal import signal | ||
| from sqlmesh.dbt.basemodel import BMC, BaseModelConfig | ||
| from sqlmesh.dbt.context import DbtContext | ||
|
|
@@ -23,7 +27,11 @@ | |
| from sqlmesh.dbt.target import TargetConfig | ||
| from sqlmesh.utils import UniqueKeyDict | ||
| from sqlmesh.utils.errors import ConfigError | ||
| from sqlmesh.utils.jinja import JinjaMacroRegistry | ||
| from sqlmesh.utils.jinja import ( | ||
| JinjaMacroRegistry, | ||
| MacroInfo, | ||
| extract_macro_references_and_variables, | ||
| ) | ||
|
|
||
| if sys.version_info >= (3, 12): | ||
| from importlib import metadata | ||
|
|
@@ -230,6 +238,60 @@ def _load_requirements(self) -> t.Tuple[t.Dict[str, str], t.Set[str]]: | |
|
|
||
| return requirements, excluded_requirements | ||
|
|
||
| def _load_environment_statements(self, macros: MacroRegistry) -> EnvironmentStatements | None: | ||
| """Loads dbt's on_run_start, on_run_end hooks into sqlmesh's before_all, after_all statements respectively.""" | ||
|
|
||
| on_run_start: t.List[str] = [] | ||
| on_run_end: t.List[str] = [] | ||
| jinja_root_macros: t.Dict[str, MacroInfo] = {} | ||
| variables: t.Dict[str, t.Any] = self._get_variables() | ||
| dialect = self.config.dialect | ||
| for project in self._load_projects(): | ||
| context = project.context.copy() | ||
| if manifest := context._manifest: | ||
| on_run_start.extend(manifest._on_run_start or []) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we access these outside the manifest instance, we shouldn't mark them as private. Or at the very least add a public property method to expose these. |
||
| on_run_end.extend(manifest._on_run_end or []) | ||
|
|
||
| if root_package := context.jinja_macros.root_package_name: | ||
| if root_macros := context.jinja_macros.packages.get(root_package): | ||
| jinja_root_macros |= root_macros | ||
| context.set_and_render_variables(context.variables, root_package) | ||
| variables |= context.variables | ||
|
|
||
| if statements := on_run_start + on_run_end: | ||
| jinja_macro_references, used_variables = extract_macro_references_and_variables( | ||
| *(gen(stmt) for stmt in statements) | ||
| ) | ||
| jinja_macros = context.jinja_macros | ||
| jinja_macros.root_macros = jinja_root_macros | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we do this gymnastics with root macros? |
||
| jinja_macros = ( | ||
| jinja_macros.trim(jinja_macro_references) | ||
| if not jinja_macros.trimmed | ||
| else jinja_macros | ||
| ) | ||
|
|
||
| python_env = make_python_env( | ||
| [s for stmt in statements for s in d.parse(stmt, default_dialect=dialect)], | ||
| jinja_macro_references=jinja_macro_references, | ||
| module_path=self.config_path, | ||
| macros=macros, | ||
| variables=variables, | ||
| used_variables=used_variables, | ||
| path=self.config_path, | ||
| ) | ||
|
|
||
| return EnvironmentStatements( | ||
| before_all=[ | ||
| d.jinja_statement(stmt).sql(dialect=dialect) for stmt in on_run_start or [] | ||
| ], | ||
| after_all=[ | ||
| d.jinja_statement(stmt).sql(dialect=dialect) for stmt in on_run_end or [] | ||
| ], | ||
| python_env=python_env, | ||
| jinja_macros=jinja_macros, | ||
| ) | ||
| return None | ||
|
|
||
| def _compute_yaml_max_mtime_per_subfolder(self, root: Path) -> t.Dict[Path, float]: | ||
| if not root.is_dir(): | ||
| return {} | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.