Skip to content

Commit

Permalink
dbt_runner: make sure we don't fail on unrelated logs
Browse files Browse the repository at this point in the history
  • Loading branch information
haritamar committed Jul 4, 2024
1 parent e1f2138 commit 52e669a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
17 changes: 11 additions & 6 deletions elementary/clients/dbt/command_line_dbt_runner.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import os
import re
from dataclasses import dataclass
from typing import Any, Dict, List, Optional

Expand All @@ -14,6 +15,10 @@

logger = get_logger(__name__)

MACRO_RESULT_PATTERN = re.compile(
"Elementary: --ELEMENTARY-MACRO-OUTPUT-START--(.*)--ELEMENTARY-MACRO-OUTPUT-END--"
)


@dataclass
class DbtCommandResult:
Expand All @@ -22,8 +27,6 @@ class DbtCommandResult:


class CommandLineDbtRunner(BaseDbtRunner):
ELEMENTARY_LOG_PREFIX = "Elementary: "

def __init__(
self,
project_dir: str,
Expand Down Expand Up @@ -185,10 +188,12 @@ def run_operation(
if log_errors and log.level == "error":
logger.error(log.msg)
continue
if log.msg and log.msg.startswith(self.ELEMENTARY_LOG_PREFIX):
run_operation_results.append(
log.msg[len(self.ELEMENTARY_LOG_PREFIX) :]
)

if log.msg:
match = MACRO_RESULT_PATTERN.match(log.msg)
if match:
run_operation_results.append(match.group(1))

return run_operation_results

def run(
Expand Down
5 changes: 5 additions & 0 deletions tests/tests_with_db/dbt_project/macros/dummy_macro.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% macro dummy_macro() %}
{# Validate that logs in the macro don't fail the dbt runner #}
{% do elementary.edr_log('Amazing macro, so happy to be here') %}
{% do return({'goodbye': 'toodleoo'}) %}
{% endmacro %}
1 change: 1 addition & 0 deletions tests/tests_with_db/pytest.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[pytest]
addopts = --ignore=dbt_project
markers =
skip_targets(targets): skip test for the given targets
only_on_targets(targets): skip test for non given targets
Expand Down
6 changes: 3 additions & 3 deletions tests/tests_with_db/test_dbt_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

class BaseDbtRunnerTest:
def test_run_operation(self, custom_dbt_runner: BaseDbtRunner):
result = self._run_query(
custom_dbt_runner, "select 1 as bla union all select 2 as bla"
result = json.loads(
custom_dbt_runner.run_operation("elementary_tests.dummy_macro")[0]
)
assert result == [{"bla": 1}, {"bla": 2}]
assert result == {"goodbye": "toodleoo"}

def test_ls(self, custom_dbt_runner: BaseDbtRunner):
result = custom_dbt_runner.ls()
Expand Down

0 comments on commit 52e669a

Please sign in to comment.