Skip to content

Commit

Permalink
Add a special handler to support the dbt_utils._is_relation macro
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Beck committed Oct 23, 2019
1 parent 3103442 commit f4ca94f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
11 changes: 10 additions & 1 deletion core/dbt/adapters/base/relation.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ def __bool__(self):
return True

def __getitem__(self, key):
# deprecations.warn('not-a-dictionary', obj=self)
try:
return getattr(self, key)
except AttributeError:
Expand Down Expand Up @@ -156,6 +155,16 @@ def get_default_include_policy(cls: Type[Self]) -> Policy:
def get_relation_type_class(cls: Type[Self]) -> Type[RelationType]:
return cls._get_field_named('type')

def get(self, key, default=None):
"""Override `.get` to return a metadata object so we don't break
dbt_utils.
"""
if key == 'metadata':
return {
'type': self.__class__.__name__
}
return super().get(key, default)

def matches(
self,
database: Optional[str] = None,
Expand Down
8 changes: 6 additions & 2 deletions test/integration/001_simple_copy_test/models/materialized.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
materialized = "table"
)
}}

-- ensure that dbt_utils' relation check will work
{% set relation = ref('seed') %}
{%- if not (relation is mapping and relation.get('metadata', {}).get('type', '').endswith('Relation')) -%}
{%- do exceptions.raise_compiler_error("Macro " ~ macro ~ " expected a Relation but received the value: " ~ relation) -%}
{%- endif -%}
-- this is a unicode character: å
select * from {{ ref('seed') }}
select * from {{ relation }}

0 comments on commit f4ca94f

Please sign in to comment.