Skip to content

Commit

Permalink
Merge pull request #1701 from fishtown-analytics/feature/expose-confi…
Browse files Browse the repository at this point in the history
…g-in-generation-macros

add configs to ParsedNodes before generating a schema/alias
  • Loading branch information
drewbanin authored Aug 27, 2019
2 parents 1b03db6 + 7fafa2a commit c002858
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ This is primarily a bugfix release which contains a few minor improvements too.
- Specify the `application` field in Snowflake connections ([#1622](https://github.com/fishtown-analytics/dbt/issues/1622), [#1623](https://github.com/fishtown-analytics/dbt/pull/1623))
- Add support for clustering on Snowflake ([#634](https://github.com/fishtown-analytics/dbt/issues/634), [#1591](https://github.com/fishtown-analytics/dbt/pull/1591), [#1689](https://github.com/fishtown-analytics/dbt/pull/1689))
- Add support for job priority on BigQuery ([#1456](https://github.com/fishtown-analytics/dbt/issues/1456), [#1673](https://github.com/fishtown-analytics/dbt/pull/1673))
- Add `node.config` and `node.tags` to the `generate_schema_name` and `generate_alias_name` macro context ([#1700](https://github.com/fishtown-analytics/dbt/issues/1700), [#1701](https://github.com/fishtown-analytics/dbt/pull/1701))

### Fixes:
- Fix for reused `check_cols` values in snapshots ([#1614](https://github.com/fishtown-analytics/dbt/pull/1614))
Expand Down
18 changes: 9 additions & 9 deletions core/dbt/parser/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,15 @@ def _update_parsed_node_info(self, parsed_node, config):
generate and set the true values to use, overriding the temporary parse
values set in _build_intermediate_parsed_node.
"""
# Set tags on node provided in config blocks
model_tags = config.config.get('tags', [])
parsed_node.tags.extend(model_tags)

# Overwrite node config
config_dict = parsed_node.get('config', {})
config_dict.update(config.config)
parsed_node.config = config_dict

# Special macro defined in the global project. Use the root project's
# definition, not the current package
schema_override = config.config.get('schema')
Expand All @@ -231,15 +240,6 @@ def _update_parsed_node_info(self, parsed_node, config):
'database', self.default_database
).strip()

# Set tags on node provided in config blocks
model_tags = config.config.get('tags', [])
parsed_node.tags.extend(model_tags)

# Overwrite node config
config_dict = parsed_node.get('config', {})
config_dict.update(config.config)
parsed_node.config = config_dict

for hook_type in dbt.hooks.ModelHookType.Both:
parsed_node.config[hook_type] = dbt.hooks.get_hooks(parsed_node,
hook_type)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

{% macro generate_schema_name(schema_name, node) %}

{{ node.config['schema'] }}_{{ target.schema }}_macro

{% endmacro %}
28 changes: 28 additions & 0 deletions test/integration/024_custom_schema_test/test_custom_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,31 @@ def test__postgres__custom_schema_from_macro(self):
self.assertTablesEqual("seed", "view_1", schema, v1_schema)
self.assertTablesEqual("seed", "view_2", schema, v2_schema)
self.assertTablesEqual("agg", "view_3", schema, xf_schema)


class TestCustomSchemaWithCustomMacroConfigs(TestCustomSchemaWithCustomMacro):

@property
def project_config(self):
return {
'macro-paths': ['custom-macros-configs'],
'models': {
'schema': 'dbt_test'
}
}

@use_profile('postgres')
def test__postgres__custom_schema_from_macro(self):
self.run_sql_file("seed.sql")

results = self.run_dbt()
self.assertEqual(len(results), 3)

schema = self.unique_schema()
v1_schema = "dbt_test_{}_macro".format(schema)
v2_schema = "custom_{}_macro".format(schema)
xf_schema = "test_{}_macro".format(schema)

self.assertTablesEqual("seed", "view_1", schema, v1_schema)
self.assertTablesEqual("seed", "view_2", schema, v2_schema)
self.assertTablesEqual("agg", "view_3", schema, xf_schema)
21 changes: 21 additions & 0 deletions test/integration/043_custom_aliases_test/macros-configs/macros.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

{#-- Verify that the config['alias'] key is present #}
{% macro generate_alias_name(custom_alias_name, node) -%}
{%- if custom_alias_name is none -%}
{{ node.name }}
{%- else -%}
custom_{{ node.config['alias'] | 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 %}
14 changes: 14 additions & 0 deletions test/integration/043_custom_aliases_test/test_custom_aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,17 @@ def test_postgres_customer_alias_name(self):
results = self.run_dbt(['run'])
self.assertEqual(len(results), 2)
self.run_dbt(['test'])


class TestAliasesWithConfig(TestAliases):
@property
def project_config(self):
return {
"macro-paths": ['macros-configs'],
}

@use_profile('postgres')
def test_postgres_customer_alias_name(self):
results = self.run_dbt(['run'])
self.assertEqual(len(results), 2)
self.run_dbt(['test'])

0 comments on commit c002858

Please sign in to comment.