Skip to content

Commit

Permalink
fix bug preventing overriding a disabled package model in the current…
Browse files Browse the repository at this point in the history
… project (#391)
  • Loading branch information
cmcarthur authored Apr 21, 2017
1 parent d9f165d commit 29871fc
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)


Expand Down
3 changes: 2 additions & 1 deletion dbt/compilation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
47 changes: 44 additions & 3 deletions dbt/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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(
Expand Down
132 changes: 132 additions & 0 deletions test/unit/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit 29871fc

Please sign in to comment.