Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.13.0 fixes around database quoting and rendering #1332

Merged
merged 4 commits into from
Mar 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion core/dbt/adapters/base/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from dbt.logger import GLOBAL_LOGGER as logger
from dbt.schema import Column
from dbt.utils import filter_null_values, translate_aliases
from dbt.node_types import NodeType

from dbt.adapters.base.meta import AdapterMeta, available, available_raw, \
available_deprecated
Expand Down Expand Up @@ -229,7 +230,14 @@ def _relations_cache_for_schemas(self, manifest):
if not dbt.flags.USE_CACHE:
return

schemas = manifest.get_used_schemas()
# We only really need to cache relations for resources that
# dbt will try to build. Even the executable() list is probably
# more expansive than necessary. Really, we just want to avoid
# caching Sources here, as there could be _many_ different schemas
# in the list, and dbt largely doesn't need to know if those sources
# exist or not.
resource_types = NodeType.executable()
schemas = manifest.get_used_schemas(resource_types)

relations = []
# add all relations
Expand Down
5 changes: 3 additions & 2 deletions core/dbt/contracts/graph/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
PARSED_MACRO_CONTRACT, PARSED_DOCUMENTATION_CONTRACT, \
PARSED_SOURCE_DEFINITION_CONTRACT
from dbt.contracts.graph.compiled import COMPILED_NODE_CONTRACT, CompiledNode
from dbt.exceptions import ValidationException
from dbt.exceptions import ValidationException, raise_duplicate_resource_name
from dbt.node_types import NodeType
from dbt.logger import GLOBAL_LOGGER as logger
from dbt import tracking
Expand Down Expand Up @@ -401,10 +401,11 @@ def __getattr__(self, name):
type(self).__name__, name)
)

def get_used_schemas(self):
def get_used_schemas(self, resource_types=None):
return frozenset({
(node.database, node.schema)
for node in self.nodes.values()
if not resource_types or node.resource_type in resource_types
})

def get_used_databases(self):
Expand Down
16 changes: 11 additions & 5 deletions core/dbt/node_runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,17 @@ def compile(self, manifest):


class ModelRunner(CompileRunner):
def get_node_representation(self):
if self.config.credentials.database == self.node.database:
template = "{0.schema}.{0.alias}"
else:
template = "{0.database}.{0.schema}.{0.alias}"

return template.format(self.node)

def describe_node(self):
materialization = dbt.utils.get_materialization(self.node)
return "{0} model {1.database}.{1.schema}.{1.alias}".format(
materialization, self.node
)
return "{} model {}".format(self.node.get_materialization(),
self.get_node_representation())

def print_start_line(self):
description = self.describe_node()
Expand Down Expand Up @@ -477,7 +483,7 @@ def print_result_line(self, result):

class SeedRunner(ModelRunner):
def describe_node(self):
return "seed file {0.database}.{0.schema}.{0.alias}".format(self.node)
return "seed file {}".format(self.get_node_representation())

def before_execute(self):
description = self.describe_node()
Expand Down
2 changes: 1 addition & 1 deletion plugins/snowflake/dbt/adapters/snowflake/relation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class SnowflakeRelation(BaseRelation):
},
'quote_character': '"',
'quote_policy': {
'database': True,
'database': False,
'schema': False,
'identifier': False,
},
Expand Down
8 changes: 4 additions & 4 deletions test/unit/test_snowflake_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def test_quoting_on_drop_schema(self):
)

self.mock_execute.assert_has_calls([
mock.call('drop schema if exists "test_database"."test_schema" cascade', None)
mock.call('drop schema if exists test_database."test_schema" cascade', None)
])

def test_quoting_on_drop(self):
Expand All @@ -84,7 +84,7 @@ def test_quoting_on_drop(self):

self.mock_execute.assert_has_calls([
mock.call(
'drop table if exists "test_database"."test_schema".test_table cascade',
'drop table if exists test_database."test_schema".test_table cascade',
None
)
])
Expand All @@ -100,7 +100,7 @@ def test_quoting_on_truncate(self):
self.adapter.truncate_relation(relation)

self.mock_execute.assert_has_calls([
mock.call('truncate table "test_database"."test_schema".test_table', None)
mock.call('truncate table test_database."test_schema".test_table', None)
])

def test_quoting_on_rename(self):
Expand All @@ -125,7 +125,7 @@ def test_quoting_on_rename(self):
)
self.mock_execute.assert_has_calls([
mock.call(
'alter table "test_database"."test_schema".table_a rename to "test_database"."test_schema".table_b',
'alter table test_database."test_schema".table_a rename to test_database."test_schema".table_b',
None
)
])
Expand Down