Skip to content

Commit

Permalink
Merge pull request #1483 from fishtown-analytics/fix/swap-alias-gener…
Browse files Browse the repository at this point in the history
…ator-args

flip around generate_alias_name args, add node to generate_schema_name args
  • Loading branch information
beckjake authored Jun 4, 2019
2 parents 6c9d5c7 + 28dc10e commit 60001ad
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 19 deletions.
12 changes: 12 additions & 0 deletions core/dbt/deprecations.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ class SeedDropExistingDeprecation(DBTDeprecation):
will be removed in a future version of dbt."""


class GenerateSchemaNameSingleArgDeprecated(DBTDeprecation):
name = 'generate-schema-name-single-arg'
description = '''As of dbt v0.14.0, the `generate_schema_name` macro
accepts a second "node" argument. The one-argument form of `generate_schema_name`
is deprecated, and will become unsupported in a future release.
For more information, see:
https://docs.getdbt.com/v0.14/docs/upgrading-to-014
''' # noqa


_adapter_renamed_description = """\
The adapter function `adapter.{old_name}` is deprecated and will be removed in
a future release of dbt. Please use `adapter.{new_name}` instead.
Expand Down Expand Up @@ -72,6 +83,7 @@ def warn(name, *args, **kwargs):
deprecations_list = [
DBTRepositoriesDeprecation(),
SeedDropExistingDeprecation(),
GenerateSchemaNameSingleArgDeprecated(),
]

deprecations = {d.name: d for d in deprecations_list}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
{#
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.
model. If an 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
node: The available node that an alias is being generated for, or none

#}
{% macro generate_alias_name(node, custom_alias_name=none) -%}
{% macro generate_alias_name(custom_alias_name=none, node=none) -%}

{%- if custom_alias_name is none -%}

Expand Down
Original file line number Diff line number Diff line change
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
23 changes: 19 additions & 4 deletions core/dbt/parser/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from dbt.logger import GLOBAL_LOGGER as logger
from dbt.contracts.graph.parsed import ParsedNode
from dbt.parser.source_config import SourceConfig
from dbt import deprecations


class BaseParser(object):
Expand Down Expand Up @@ -82,8 +83,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 +119,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(node, custom_alias_name=None):
def get_alias(custom_alias_name, node):
if custom_alias_name is None:
return node.name
else:
Expand Down Expand Up @@ -206,11 +210,22 @@ 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()
try:
schema = get_schema(schema_override, parsed_node)
except dbt.exceptions.CompilationException as exc:
too_many_args = (
"macro 'dbt_macro__generate_schema_name' takes not more than "
"1 argument(s)"
)
if too_many_args not in str(exc):
raise
deprecations.warn('generate-schema-name-single-arg')
schema = get_schema(schema_override)
parsed_node.schema = schema.strip()

alias_override = config.config.get('alias')
get_alias = self.get_alias_func()
parsed_node.alias = get_alias(parsed_node, alias_override).strip()
parsed_node.alias = get_alias(alias_override, parsed_node).strip()

parsed_node.database = config.config.get(
'database', self.default_database
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

{% macro generate_schema_name(schema_name) -%}
{% macro generate_schema_name(schema_name, node) -%}

{{ schema_name }}_{{ target.schema }}_macro
{{ schema_name }}_{{ node.schema }}_macro

{%- endmacro %}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
import dbt.exceptions


class TestSimpleDependency(DBTIntegrationTest):

class BaseDependencyTest(DBTIntegrationTest):
@property
def schema(self):
return "local_dependency_006"
Expand All @@ -17,6 +16,12 @@ def schema(self):
def models(self):
return "test/integration/006_simple_dependency_test/local_models"

def base_schema(self):
return self.unique_schema()

def configured_schema(self):
return self.unique_schema() + '_configured'

@property
def packages_config(self):
return {
Expand All @@ -27,6 +32,17 @@ def packages_config(self):
]
}


class TestSimpleDependency(BaseDependencyTest):

@property
def schema(self):
return "local_dependency_006"

@property
def models(self):
return "test/integration/006_simple_dependency_test/local_models"

def base_schema(self):
return self.unique_schema()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select 1 as id
7 changes: 7 additions & 0 deletions test/integration/012_deprecation_tests/macros/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% macro generate_schema_name(schema_name) -%}
{%- if schema_name is none -%}
{{ target.schema }}
{%- else -%}
{{ schema_name }}
{%- endif -%}
{%- endmacro %}
34 changes: 30 additions & 4 deletions test/integration/012_deprecation_tests/test_deprecations.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import dbt.exceptions


class TestDeprecations(DBTIntegrationTest):
class BaseTestDeprecations(DBTIntegrationTest):
def setUp(self):
super(TestDeprecations, self).setUp()
super(BaseTestDeprecations, self).setUp()
deprecations.reset_deprecations()

@property
Expand All @@ -21,6 +21,8 @@ def dir(path):
def models(self):
return self.dir("models")


class TestDeprecations(BaseTestDeprecations):
@use_profile('postgres')
def test_postgres_deprecations_fail(self):
self.run_dbt(strict=True, expect_pass=False)
Expand All @@ -29,5 +31,29 @@ def test_postgres_deprecations_fail(self):
def test_postgres_deprecations(self):
self.assertEqual(deprecations.active_deprecations, set())
self.run_dbt(strict=False)
self.assertEqual({'adapter:already_exists'},
deprecations.active_deprecations)
expected = {'adapter:already_exists'}
self.assertEqual(expected, deprecations.active_deprecations)


class TestMacroDeprecations(BaseTestDeprecations):
@property
def models(self):
return self.dir('boring-models')

@property
def project_config(self):
return {
'macro-paths': [self.dir('macros')],
}

@use_profile('postgres')
def test_postgres_deprecations_fail(self):
with self.assertRaises(dbt.exceptions.CompilationException):
self.run_dbt(strict=True)

@use_profile('postgres')
def test_postgres_deprecations(self):
self.assertEqual(deprecations.active_deprecations, set())
self.run_dbt(strict=False)
expected = {'generate-schema-name-single-arg'}
self.assertEqual(expected, deprecations.active_deprecations)
2 changes: 1 addition & 1 deletion test/integration/024_custom_schema_test/macros/schema.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

{% macro generate_schema_name(schema_name) %}
{% macro generate_schema_name(schema_name, node) %}

{{ schema_name }}_{{ target.schema }}_macro

Expand Down
2 changes: 1 addition & 1 deletion test/integration/043_custom_aliases_test/macros/macros.sql
Original file line number Diff line number Diff line change
@@ -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 60001ad

Please sign in to comment.