Skip to content

Commit

Permalink
swap aliases ordering, also add node parameter to generate_schema_nam…
Browse files Browse the repository at this point in the history
…e, fix tests
  • Loading branch information
Jacob Beck committed May 29, 2019
1 parent a73074b commit 68782b7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
Expand Up @@ -3,17 +3,18 @@
Renders a schema name given a custom schema name. If the custom
schema name is none, then the resulting schema is just the "schema"
value in the specified target. If a schema override is specified, then
the resulting schema is the default schema concatenated with the
the resulting schema is the default schema concatenated with the
custom schema.

This macro can be overriden in projects to define different semantics
for rendering a schema name.

Arguments:
custom_schema_name: The custom schema name specified for a model, or none
node: The node the schema is being generated for

#}
{% macro generate_schema_name(custom_schema_name=none) -%}
{% macro generate_schema_name(custom_schema_name, node) -%}

{%- set default_schema = target.schema -%}
{%- if custom_schema_name is none -%}
Expand All @@ -36,9 +37,10 @@

Arguments:
custom_schema_name: The custom schema name specified for a model, or none
node: The node the schema is being generated for

#}
{% macro generate_schema_name_for_env(custom_schema_name=none) -%}
{% macro generate_schema_name_for_env(custom_schema_name, node) -%}

{%- set default_schema = target.schema -%}
{%- if target.name == 'prod' and custom_schema_name is not none -%}
Expand Down
9 changes: 6 additions & 3 deletions core/dbt/parser/base.py
Expand Up @@ -82,8 +82,9 @@ def get_schema_func(self):
'generate_schema_name',
GLOBAL_PROJECT_NAME
)
# this is only true in tests!
if get_schema_macro is None:
def get_schema(_):
def get_schema(custom_schema_name=None, node=None):
return self.default_schema
else:
root_context = dbt.context.parser.generate_macro(
Expand Down Expand Up @@ -117,8 +118,10 @@ def get_alias_func(self):
'generate_alias_name',
GLOBAL_PROJECT_NAME
)

# the generate_alias_name macro might not exist
if get_alias_macro is None:
def get_alias(custom_alias_name=None, node=None):
def get_alias(custom_alias_name, node):
if custom_alias_name is None:
return node.name
else:
Expand Down Expand Up @@ -206,7 +209,7 @@ def _update_parsed_node_info(self, parsed_node, config):
# definition, not the current package
schema_override = config.config.get('schema')
get_schema = self.get_schema_func()
parsed_node.schema = get_schema(schema_override).strip()
parsed_node.schema = get_schema(schema_override, parsed_node).strip()

alias_override = config.config.get('alias')
get_alias = self.get_alias_func()
Expand Down
2 changes: 1 addition & 1 deletion test/integration/043_custom_aliases_test/macros/macros.sql
@@ -1,5 +1,5 @@

{% macro generate_alias_name(node, custom_alias_name=none) -%}
{% macro generate_alias_name(custom_alias_name, node) -%}
{%- if custom_alias_name is none -%}
{{ node.name }}
{%- else -%}
Expand Down

0 comments on commit 68782b7

Please sign in to comment.