-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1363 from roverdotcom/feature/alias-macro
Adding a generate_alias_name macro
- Loading branch information
Showing
7 changed files
with
134 additions
and
1 deletion.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
core/dbt/include/global_project/macros/etc/get_custom_alias.sql
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,26 @@ | ||
|
||
{# | ||
Renders a alias name given a custom alias name. If the custom | ||
alias name is none, then the resulting alias is just the filename of the | ||
model. If a alias override is specified, then that is used. | ||
|
||
This macro can be overriden in projects to define different semantics | ||
for rendering a alias name. | ||
|
||
Arguments: | ||
custom_alias_name: The custom alias name specified for a model, or none | ||
|
||
#} | ||
{% macro generate_alias_name(node, custom_alias_name=none) -%} | ||
|
||
{%- if custom_alias_name is none -%} | ||
|
||
{{ node.name }} | ||
|
||
{%- else -%} | ||
|
||
{{ custom_alias_name | trim }} | ||
|
||
{%- endif -%} | ||
|
||
{%- endmacro %} |
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
21 changes: 21 additions & 0 deletions
21
test/integration/043_custom_aliases_test/macros/macros.sql
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,21 @@ | ||
|
||
{% macro generate_alias_name(node, custom_alias_name=none) -%} | ||
{%- if custom_alias_name is none -%} | ||
{{ node.name }} | ||
{%- else -%} | ||
custom_{{ custom_alias_name | trim }} | ||
{%- endif -%} | ||
{%- endmacro %} | ||
|
||
|
||
{% macro string_literal(s) -%} | ||
{{ adapter_macro('test.string_literal', s) }} | ||
{%- endmacro %} | ||
|
||
{% macro default__string_literal(s) %} | ||
'{{ s }}'::text | ||
{% endmacro %} | ||
|
||
{% macro bigquery__string_literal(s) %} | ||
cast('{{ s }}' as string) | ||
{% endmacro %} |
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,3 @@ | ||
{{ config(materialized='table', alias='alias') }} | ||
|
||
select {{ string_literal(this.name) }} as model_name |
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,3 @@ | ||
{{ config(materialized='table') }} | ||
|
||
select {{ string_literal(this.name) }} as model_name |
15 changes: 15 additions & 0 deletions
15
test/integration/043_custom_aliases_test/models/schema.yml
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,15 @@ | ||
version: 2 | ||
|
||
models: | ||
- name: model1 | ||
columns: | ||
- name: model_name | ||
tests: | ||
- accepted_values: | ||
values: ['custom_alias'] | ||
- name: model2 | ||
columns: | ||
- name: model_name | ||
tests: | ||
- accepted_values: | ||
values: ['model2'] |
22 changes: 22 additions & 0 deletions
22
test/integration/043_custom_aliases_test/test_custom_aliases.py
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,22 @@ | ||
from test.integration.base import DBTIntegrationTest | ||
|
||
|
||
class TestAliases(DBTIntegrationTest): | ||
@property | ||
def schema(self): | ||
return "custom_aliases_043" | ||
|
||
@property | ||
def models(self): | ||
return "test/integration/043_custom_aliases_test/models" | ||
|
||
@property | ||
def project_config(self): | ||
return { | ||
"macro-paths": ['test/integration/043_custom_aliases_test/macros'], | ||
} | ||
|
||
def test__customer_alias_name(self): | ||
results = self.run_dbt(['run']) | ||
self.assertEqual(len(results), 1) | ||
self.run_dbt(['test']) |