diff --git a/CHANGELOG.md b/CHANGELOG.md index 1dad3e929b7..d2173591527 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## dbt 0.8.1 (Unreleased) + +### Bugfixes + +- Fix bug preventing overriding a disabled package model in the current project ([#391](https://github.com/fishtown-analytics/dbt/pull/391)) + ## dbt 0.8.0 (April 17, 2017) diff --git a/dbt/compilation.py b/dbt/compilation.py index c2bba79dfda..8bc29a911f5 100644 --- a/dbt/compilation.py +++ b/dbt/compilation.py @@ -487,7 +487,8 @@ def compile(self): 'macros': all_macros } - flat_graph = dbt.parser.process_refs(flat_graph) + flat_graph = dbt.parser.process_refs(flat_graph, + root_project.get('name')) linked_graph = self.link_graph(linker, flat_graph) diff --git a/dbt/parser.py b/dbt/parser.py index 8f2d6eb97b0..28be067dbc5 100644 --- a/dbt/parser.py +++ b/dbt/parser.py @@ -64,8 +64,47 @@ def ref(*args): return ref -def process_refs(flat_graph): +def resolve_ref(flat_graph, target_model_name, target_model_package, + current_project, node_package): + + if target_model_package is not None: + return dbt.utils.find_model_by_name( + flat_graph, + target_model_name, + target_model_package) + + target_model = None + + # first pass: look for models in the current_project + target_model = dbt.utils.find_model_by_name( + flat_graph, + target_model_name, + current_project) + + if target_model is not None and dbt.utils.is_enabled(target_model): + return target_model + + # second pass: look for models in the node's package + target_model = dbt.utils.find_model_by_name( + flat_graph, + target_model_name, + node_package) + + if target_model is not None and dbt.utils.is_enabled(target_model): + return target_model + + # final pass: look for models in any package + # todo: exclude the packages we have already searched. overriding + # a package model in another package doesn't necessarily work atm + return dbt.utils.find_model_by_name( + flat_graph, + target_model_name, + None) + + +def process_refs(flat_graph, current_project): for _, node in flat_graph.get('nodes').items(): + target_model = None target_model_name = None target_model_package = None @@ -75,10 +114,12 @@ def process_refs(flat_graph): elif len(ref) == 2: target_model_package, target_model_name = ref - target_model = dbt.utils.find_model_by_name( + target_model = resolve_ref( flat_graph, target_model_name, - target_model_package) + target_model_package, + current_project, + node.get('package_name')) if target_model is None: dbt.exceptions.ref_target_not_found( diff --git a/test/unit/test_parser.py b/test/unit/test_parser.py index d93c5fe766e..4367f7efab4 100644 --- a/test/unit/test_parser.py +++ b/test/unit/test_parser.py @@ -44,6 +44,14 @@ def setUp(self): 'vars': {}, } + self.disabled_config = { + 'enabled': False, + 'materialized': 'view', + 'post-hook': [], + 'pre-hook': [], + 'vars': {}, + } + def test__single_model(self): models = [{ 'name': 'model_one', @@ -544,6 +552,130 @@ def test__multiple_dependencies__packages(self): } ) + def test__process_refs__packages(self): + graph = { + 'macros': {}, + 'nodes': { + 'model.snowplow.events': { + 'name': 'events', + 'resource_type': 'model', + 'unique_id': 'model.snowplow.events', + 'fqn': ['snowplow', 'events'], + 'empty': False, + 'package_name': 'snowplow', + 'refs': [], + 'depends_on': { + 'nodes': [], + 'macros': [] + }, + 'config': self.disabled_config, + 'tags': set(), + 'path': 'events.sql', + 'root_path': get_os_path('/usr/src/app'), + 'raw_sql': 'does not matter' + }, + 'model.root.events': { + 'name': 'events', + 'resource_type': 'model', + 'unique_id': 'model.root.events', + 'fqn': ['root', 'events'], + 'empty': False, + 'package_name': 'root', + 'refs': [], + 'depends_on': { + 'nodes': [], + 'macros': [] + }, + 'config': self.model_config, + 'tags': set(), + 'path': 'events.sql', + 'root_path': get_os_path('/usr/src/app'), + 'raw_sql': 'does not matter' + }, + 'model.root.dep': { + 'name': 'dep', + 'resource_type': 'model', + 'unique_id': 'model.root.dep', + 'fqn': ['root', 'dep'], + 'empty': False, + 'package_name': 'root', + 'refs': [('events',)], + 'depends_on': { + 'nodes': [], + 'macros': [] + }, + 'config': self.model_config, + 'tags': set(), + 'path': 'multi.sql', + 'root_path': get_os_path('/usr/src/app'), + 'raw_sql': 'does not matter' + } + } + } + + self.assertEquals( + dbt.parser.process_refs(graph, 'root'), + { + 'macros': {}, + 'nodes': { + 'model.snowplow.events': { + 'name': 'events', + 'resource_type': 'model', + 'unique_id': 'model.snowplow.events', + 'fqn': ['snowplow', 'events'], + 'empty': False, + 'package_name': 'snowplow', + 'refs': [], + 'depends_on': { + 'nodes': [], + 'macros': [] + }, + 'config': self.disabled_config, + 'tags': set(), + 'path': 'events.sql', + 'root_path': get_os_path('/usr/src/app'), + 'raw_sql': 'does not matter' + }, + 'model.root.events': { + 'name': 'events', + 'resource_type': 'model', + 'unique_id': 'model.root.events', + 'fqn': ['root', 'events'], + 'empty': False, + 'package_name': 'root', + 'refs': [], + 'depends_on': { + 'nodes': [], + 'macros': [] + }, + 'config': self.model_config, + 'tags': set(), + 'path': 'events.sql', + 'root_path': get_os_path('/usr/src/app'), + 'raw_sql': 'does not matter' + }, + 'model.root.dep': { + 'name': 'dep', + 'resource_type': 'model', + 'unique_id': 'model.root.dep', + 'fqn': ['root', 'dep'], + 'empty': False, + 'package_name': 'root', + 'refs': [('events',)], + 'depends_on': { + 'nodes': ['model.root.events'], + 'macros': [] + }, + 'config': self.model_config, + 'tags': set(), + 'path': 'multi.sql', + 'root_path': get_os_path('/usr/src/app'), + 'raw_sql': 'does not matter' + } + } + } + ) + def test__in_model_config(self): models = [{ 'name': 'model_one',