Skip to content

Commit

Permalink
fix comparision for new model/body (#4631) (#4676)
Browse files Browse the repository at this point in the history
* fix comparison for new model/body
  • Loading branch information
ChenyuLInx authored Feb 3, 2022
1 parent bec6bec commit 056db40
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 37 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
- Restore previous log level (DEBUG) when a test depends on a disabled resource. Still WARN if the resource is missing ([#4594](https:/dbt-labs/dbt-core/issues/4594), [#4647](https:/dbt-labs/dbt-core/pull/4647))
- User wasn't asked for permission to overwite a profile entry when running init inside an existing project ([#4375](https:/dbt-labs/dbt-core/issues/4375), [#4447](https:/dbt-labs/dbt-core/pull/4447))
- A change in secret environment variables won't trigger a full reparse [#4650](https:/dbt-labs/dbt-core/issues/4650) [4665](https:/dbt-labs/dbt-core/pull/4665)
- Add project name validation to `dbt init` ([#4490](https:/dbt-labs/dbt-core/issues/4490),[#4536](https:/dbt-labs/dbt-core/pull/4536))

Contributors:
- [@NiallRees](https:/NiallRees) ([#4447](https:/dbt-labs/dbt-core/pull/4447))
- [@amirkdv](https:/amirkdv) ([#4536](https:/dbt-labs/dbt-core/pull/4536))

## dbt-core 1.0.1 (January 03, 2022)

Expand Down
67 changes: 30 additions & 37 deletions core/dbt/graph/selector_methods.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import abc
from itertools import chain
from pathlib import Path
from typing import Set, List, Dict, Iterator, Tuple, Any, Union, Type, Optional
from typing import Set, List, Dict, Iterator, Tuple, Any, Union, Type, Optional, Callable

from dbt.dataclass_schema import StrEnum

Expand Down Expand Up @@ -478,42 +478,28 @@ def check_macros_modified(self, node):
previous_macros = []
return self.recursively_check_macros_modified(node, previous_macros)

def check_modified(self, old: Optional[SelectorTarget], new: SelectorTarget) -> bool:
# TODO check modifed_content and check_modified macro seems a bit redundent
def check_modified_content(self, old: Optional[SelectorTarget], new: SelectorTarget) -> bool:
different_contents = not new.same_contents(old) # type: ignore
upstream_macro_change = self.check_macros_modified(new)
return different_contents or upstream_macro_change

def check_modified_body(self, old: Optional[SelectorTarget], new: SelectorTarget) -> bool:
if hasattr(new, "same_body"):
return not new.same_body(old) # type: ignore
else:
return False

def check_modified_configs(self, old: Optional[SelectorTarget], new: SelectorTarget) -> bool:
if hasattr(new, "same_config"):
return not new.same_config(old) # type: ignore
else:
return False

def check_modified_persisted_descriptions(
self, old: Optional[SelectorTarget], new: SelectorTarget
) -> bool:
if hasattr(new, "same_persisted_description"):
return not new.same_persisted_description(old) # type: ignore
else:
return False

def check_modified_relation(
self, old: Optional[SelectorTarget], new: SelectorTarget
) -> bool:
if hasattr(new, "same_database_representation"):
return not new.same_database_representation(old) # type: ignore
else:
return False

def check_modified_macros(self, _, new: SelectorTarget) -> bool:
return self.check_macros_modified(new)

@staticmethod
def check_modified_factory(
compare_method: str
) -> Callable[[Optional[SelectorTarget], SelectorTarget], bool]:
# get a function that compares two selector target based on compare method provided
def check_modified_things(old: Optional[SelectorTarget], new: SelectorTarget) -> bool:
if hasattr(new, compare_method):
# when old body does not exist or old and new are not the same
return not old or not getattr(new, compare_method)(old) # type: ignore
else:
return False
return check_modified_things

def check_new(self, old: Optional[SelectorTarget], new: SelectorTarget) -> bool:
return old is None

Expand All @@ -527,14 +513,21 @@ def search(

state_checks = {
# it's new if there is no old version
'new': lambda old, _: old is None,
'new':
lambda old, _: old is None,
# use methods defined above to compare properties of old + new
'modified': self.check_modified,
'modified.body': self.check_modified_body,
'modified.configs': self.check_modified_configs,
'modified.persisted_descriptions': self.check_modified_persisted_descriptions,
'modified.relation': self.check_modified_relation,
'modified.macros': self.check_modified_macros,
'modified':
self.check_modified_content,
'modified.body':
self.check_modified_factory('same_body'),
'modified.configs':
self.check_modified_factory('same_config'),
'modified.persisted_descriptions':
self.check_modified_factory('same_persisted_description'),
'modified.relation':
self.check_modified_factory('same_database_representation'),
'modified.macros':
self.check_modified_macros,
}
if selector in state_checks:
checker = state_checks[selector]
Expand Down
2 changes: 2 additions & 0 deletions test/unit/test_graph_selector_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,8 @@ def test_select_state_added_model(manifest, previous_state):
manifest, method, 'modified') == {'another_model'}
assert search_manifest_using_method(
manifest, method, 'new') == {'another_model'}
assert search_manifest_using_method(
manifest, method, 'modified.body') == {'another_model'}


def test_select_state_changed_model_sql(manifest, previous_state, view_model):
Expand Down

0 comments on commit 056db40

Please sign in to comment.