Skip to content

Commit

Permalink
BaseContext: expose md5 function in context (dbt-labs#6247)
Browse files Browse the repository at this point in the history
* BaseContext: expose md5 function in context

* BaseContext: add return value type

* Add changie entry

* rename "md5" to "local_md5"

* fix test_context.py
  • Loading branch information
haritamar authored Nov 28, 2022
1 parent 7d70664 commit bce0e7c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .changes/unreleased/Features-20221114-185207.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
kind: Features
body: Added an md5 function to the base context
time: 2022-11-14T18:52:07.788593+02:00
custom:
Author: haritamar
Issue: "6246"
PR: "6247"
14 changes: 14 additions & 0 deletions core/dbt/context/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from dbt import flags
from dbt import tracking
from dbt import utils
from dbt.clients.jinja import get_rendered
from dbt.clients.yaml_helper import yaml, safe_load, SafeLoader, Loader, Dumper # noqa: F401
from dbt.constants import SECRET_ENV_PREFIX, DEFAULT_ENV_PLACEHOLDER
Expand Down Expand Up @@ -687,6 +688,19 @@ def diff_of_two_dicts(
dict_diff.update({k: dict_a[k]})
return dict_diff

@contextmember
@staticmethod
def local_md5(value: str) -> str:
"""Calculates an MD5 hash of the given string.
It's called "local_md5" to emphasize that it runs locally in dbt (in jinja context) and not an MD5 SQL command.
:param value: The value to hash
Usage:
{% set value_hash = local_md5("hello world") %}
"""
return utils.md5(value)


def generate_base_context(cli_vars: Dict[str, Any]) -> Dict[str, Any]:
ctx = BaseContext(cli_vars)
Expand Down
1 change: 1 addition & 0 deletions test/unit/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ def assert_has_keys(required_keys: Set[str], maybe_keys: Set[str], ctx: Dict[str
"flags",
"print",
"diff_of_two_dicts",
"local_md5"
}
)

Expand Down

0 comments on commit bce0e7c

Please sign in to comment.