|
4 | 4 |
|
5 | 5 | import datetime as dt |
6 | 6 | import os |
| 7 | +from abc import ABC, abstractmethod |
7 | 8 | from dataclasses import asdict, is_dataclass |
8 | 9 | from pathlib import Path |
9 | 10 | from typing import TYPE_CHECKING, Any, Dict, Optional, Union |
@@ -38,18 +39,84 @@ class DbtBaseOperator(BaseOperator): |
38 | 39 | command itself, subclasses should set it. |
39 | 40 |
|
40 | 41 | Attributes: |
41 | | - command: The dbt command to execute. |
42 | 42 | project_dir: Directory for dbt to look for dbt_profile.yml. Defaults to |
43 | 43 | current directory. |
44 | 44 | profiles_dir: Directory for dbt to look for profiles.yml. Defaults to |
45 | 45 | ~/.dbt. |
46 | 46 | profile: Which profile to load. Overrides dbt_profile.yml. |
47 | 47 | target: Which target to load for the given profile. |
| 48 | + state: Unless overridden, use this state directory for both state comparison |
| 49 | + and deferral. |
| 50 | + cache_selected_only: At start of run, populate relational cache |
| 51 | + only for schemas containing selected nodes, or for all schemas of interest. |
| 52 | + fail_fast: Stop execution on first failure. |
| 53 | + single_threaded: Execute dbt command in single threaded mode. For test only |
| 54 | + threads: Specify number of threads to use while executing models. |
| 55 | + Overrides settings in profiles.yml. |
| 56 | + use_experimental_parser: Enable experimental parsing features. |
48 | 57 | vars: Supply variables to the project. Should be a YAML string. Overrides |
49 | 58 | variables defined in dbt_profile.yml. |
| 59 | + warn_error: If dbt would normally warn, instead raise an exception. |
| 60 | + Examples include --select that selects nothing, deprecations, |
| 61 | + configurations with no associated models, invalid test configurations, |
| 62 | + and missing sources/refs in tests. |
| 63 | + debug: Display debug logging during dbt execution. |
| 64 | + Useful for debugging and making bug reports. |
| 65 | + log_path: Log path for dbt execution. |
| 66 | + log_level: Specify the minimum severity of events that are logged |
| 67 | + to the console and the log file. |
| 68 | + log_level_file: Specify the minimum severity of events that are logged |
| 69 | + to the log file by overriding the default value |
| 70 | + log_format: Specify the format of logging to the console and the log file. |
| 71 | + log_format_file: Specify the format of logging to the log file by overriding |
| 72 | + the default value |
50 | 73 | log_cache_events: Flag to enable logging of cache events. |
51 | | - s3_conn_id: An s3 Airflow connection ID to use when pulling dbt files from s3. |
| 74 | + quiet/no_quiet: Suppress all non-error logging to stdout. |
| 75 | + Does not affect {{ print() }} macro calls. |
| 76 | + print/no_print: Output all {{ print() }} macro calls. |
| 77 | + record_timing_info: When this option is passed, dbt will output low-level |
| 78 | + timing stats to the specified file. |
| 79 | + defer/no_defer: If set, resolve unselected nodes by deferring to the manifest |
| 80 | + within the --state directory. |
| 81 | + partial_parse/no_partial_parse: Allow for partial parsing by looking for |
| 82 | + and writing to a pickle file in the target directory. |
| 83 | + introspect/no_introspect: Whether to scaffold introspective queries |
| 84 | + as part of compilation |
| 85 | + use_colors/no_use_colors: Specify whether log output is colorized |
| 86 | + in the console and the log file. |
| 87 | + static_parser/no_static_parser: Use the static parser. |
| 88 | + version_check/no_version_check: If set, ensure the installed dbt version matches |
| 89 | + the require-dbt-version specified in the dbt_project.yml file (if any). |
| 90 | + Otherwise, allow them to differ. |
| 91 | + write_json/no_write_json: Whether or not to write the manifest.json |
| 92 | + and run_results.json files to the target directory |
| 93 | + send_anonymous_usage_stats/no_send_anonymous_usage_stats: Send anonymous usage |
| 94 | + stats to dbt Labs. |
| 95 | + partial_parse_file_diff/no_partial_parse_file_diff: Internal flag for whether |
| 96 | + to compute a file diff during partial parsing. |
| 97 | + inject_ephemeral_ctes/no_inject_ephemeral_ctes: Internal flag controlling |
| 98 | + injection of referenced ephemeral models' CTEs during `compile`. |
| 99 | + empty/no_empty: If specified, limit input refs and sources to zero rows. |
| 100 | + show_resource_report/no_show_resource_report: If set, dbt will output |
| 101 | + resource report into log. |
| 102 | + favor_state/no_favor_state: If set, defer to the argument provided to |
| 103 | + the state flag for resolving unselected nodes, even if the node(s) exist |
| 104 | + as a database object in the current environment. |
| 105 | + export_saved_queries/no_export_saved_queries: Export saved queries within |
| 106 | + the 'build' command, otherwise no-op |
| 107 | + dbt_conn_id: An Airflow connection ID to generate dbt profile from. |
| 108 | + profiles_conn_id: An Airflow connection ID to use for pulling dbt profiles |
| 109 | + from remote (e.g. git/s3/gcs). |
| 110 | + project_conn_id: An Airflow connection ID to use for pulling dbt project |
| 111 | + from remote (e.g. git/s3/gcs). |
52 | 112 | do_xcom_push_artifacts: A list of dbt artifacts to XCom push. |
| 113 | + upload_dbt_project: Flag to enable unloading the project dbt after the operator |
| 114 | + execution back to project_dir. |
| 115 | + delete_before_upload: Flag to enable cleaning up project_dir before uploading |
| 116 | + dbt project back to. |
| 117 | + replace_on_upload: Flag to allow replacing files when uploading dbt project |
| 118 | + back to project_dir. |
| 119 | + env_vars: Supply environment variables to the project |
53 | 120 | """ |
54 | 121 |
|
55 | 122 | template_fields = base_template_fields |
@@ -77,6 +144,7 @@ def __init__( |
77 | 144 | log_level: Optional[str] = None, |
78 | 145 | log_level_file: Optional[str] = None, |
79 | 146 | log_format: LogFormat = LogFormat.DEFAULT, |
| 147 | + log_format_file: LogFormat = LogFormat.DEBUG, |
80 | 148 | log_cache_events: Optional[bool] = False, |
81 | 149 | quiet: Optional[bool] = None, |
82 | 150 | no_quiet: Optional[bool] = None, |
@@ -149,6 +217,7 @@ def __init__( |
149 | 217 | self.log_level = log_level |
150 | 218 | self.log_level_file = log_level_file |
151 | 219 | self.log_format = log_format |
| 220 | + self.log_format_file = log_format_file |
152 | 221 | self.record_timing_info = record_timing_info |
153 | 222 |
|
154 | 223 | self.dbt_defer = defer |
@@ -243,6 +312,7 @@ def execute(self, context): |
243 | 312 | return serializable_result |
244 | 313 |
|
245 | 314 | @property |
| 315 | + @abstractmethod |
246 | 316 | def command(self) -> str: |
247 | 317 | """Return the current dbt command. |
248 | 318 |
|
@@ -296,6 +366,22 @@ def make_run_results_serializable( |
296 | 366 | return asdict(result, dict_factory=run_result_factory) |
297 | 367 |
|
298 | 368 |
|
| 369 | +class _GraphRunnableOperator(ABC, DbtBaseOperator): |
| 370 | + """The abstract base Airflow dbt operator for list/compile commands. |
| 371 | +
|
| 372 | + Attributes: |
| 373 | + compiled_target: |
| 374 | + """ |
| 375 | + |
| 376 | + def __init__( |
| 377 | + self, |
| 378 | + compiled_target: Optional[Union[os.PathLike, str, bytes]] = None, |
| 379 | + **kwargs, |
| 380 | + ): |
| 381 | + super().__init__(**kwargs) |
| 382 | + self.compiled_target = compiled_target |
| 383 | + |
| 384 | + |
299 | 385 | selection_template_fields = ["select", "exclude"] |
300 | 386 |
|
301 | 387 |
|
@@ -397,7 +483,7 @@ def command(self) -> str: |
397 | 483 | return "test" |
398 | 484 |
|
399 | 485 |
|
400 | | -class DbtCompileOperator(DbtBaseOperator): |
| 486 | +class DbtCompileOperator(_GraphRunnableOperator): |
401 | 487 | """Executes a dbt compile command. |
402 | 488 |
|
403 | 489 | The compile command generates SQL files. The |
@@ -542,7 +628,7 @@ def command(self) -> str: |
542 | 628 | return "snapshot" |
543 | 629 |
|
544 | 630 |
|
545 | | -class DbtLsOperator(DbtBaseOperator): |
| 631 | +class DbtLsOperator(_GraphRunnableOperator): |
546 | 632 | """Executes a dbt list (or ls) command. |
547 | 633 |
|
548 | 634 | The documentation for the dbt command can be found here: |
|
0 commit comments