-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add basic dbt runner tests * dbt runner - refactor in preparation for api runner * add dbt api runner! * api_dbt_runner: bugfix * get_dbt_runner -> create_dbt_runner * api_dbt_runner: support env vars * python 3.8 compat * more compat changes * add decorator requirement * unittests fix * remove unnecessary decorator dependency * Delete slim dbt runner
- Loading branch information
Showing
51 changed files
with
1,029 additions
and
438 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,3 +97,4 @@ venv/ | |
|
||
# elementary outputs | ||
edr_target/ | ||
tests/tests_with_db/dbt_project/dbt_packages/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,4 @@ types-PyYAML | |
types-setuptools | ||
pandas-stubs | ||
types-retry | ||
types-decorator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import json | ||
from dataclasses import dataclass | ||
from typing import List, Optional, cast | ||
|
||
from dbt.cli.main import dbtRunner, dbtRunnerResult | ||
from google.protobuf.json_format import MessageToDict | ||
|
||
from elementary.clients.dbt.command_line_dbt_runner import ( | ||
CommandLineDbtRunner, | ||
DbtCommandResult, | ||
) | ||
from elementary.exceptions.exceptions import DbtCommandError | ||
from elementary.utils.env_vars_context import env_vars_context | ||
from elementary.utils.log import get_logger | ||
|
||
logger = get_logger(__name__) | ||
|
||
|
||
@dataclass | ||
class APIDbtCommandResult(DbtCommandResult): | ||
result_obj: dbtRunnerResult | ||
|
||
|
||
class APIDbtRunner(CommandLineDbtRunner): | ||
def _inner_run_command( | ||
self, | ||
dbt_command_args: List[str], | ||
capture_output: bool, | ||
quiet: bool, | ||
log_output: bool, | ||
log_format: str, | ||
) -> DbtCommandResult: | ||
# The dbt python API always prints the output and we collect the logs using a programmatic callback so no | ||
# need to capture the output anymore here. | ||
dbt_command_args = list(dbt_command_args) | ||
if "-q" not in dbt_command_args and "--quiet" not in dbt_command_args: | ||
dbt_command_args.extend(["--quiet"]) | ||
|
||
dbt_logs = [] | ||
|
||
def collect_dbt_command_logs(event): | ||
event_dump = json.dumps(MessageToDict(event)) # type: ignore[arg-type] | ||
logger.debug(f"dbt event msg: {event_dump}") | ||
if event.info.name == "JinjaLogInfo": | ||
dbt_logs.append(event_dump) | ||
|
||
with env_vars_context(self.env_vars): | ||
dbt = dbtRunner(callbacks=[collect_dbt_command_logs]) | ||
res: dbtRunnerResult = dbt.invoke(dbt_command_args) | ||
output = "\n".join(dbt_logs) or None | ||
if self.raise_on_failure and not res.success: | ||
raise DbtCommandError(base_command_args=dbt_command_args, err_msg=output) | ||
|
||
return APIDbtCommandResult(success=res.success, output=output, result_obj=res) | ||
|
||
def _parse_ls_command_result( | ||
self, select: Optional[str], result: DbtCommandResult | ||
) -> List[str]: | ||
ls_result = cast(APIDbtCommandResult, result).result_obj.result | ||
return cast(List[str], ls_result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.