Skip to content

Commit

Permalink
Check if ref'd resource is selected before favoring state (#10108)
Browse files Browse the repository at this point in the history
* Add test case

* Check if ref'd resource is selected before favoring state

* Add changie
  • Loading branch information
jtcohen6 committed May 8, 2024
1 parent 43d6c2f commit 84cc540
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .changes/unreleased/Fixes-20240508-151127.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
kind: Fixes
body: 'Restore previous behavior for --favor-state: only favor defer_relation if not
selected in current command"'
time: 2024-05-08T15:11:27.510912+02:00
custom:
Author: jtcohen6
Issue: "10107"
7 changes: 5 additions & 2 deletions core/dbt/context/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,8 +554,11 @@ def create_relation(self, target_model: ManifestNode) -> RelationProxy:
and target_model.defer_relation
and self.config.args.defer
and (
# User has explicitly opted to prefer defer_relation
self.config.args.favor_state
# User has explicitly opted to prefer defer_relation for unselected resources
(
self.config.args.favor_state
and target_model.unique_id not in selected_resources.SELECTED_RESOURCES
)
# Or, this node's relation does not exist in the expected target location (cache lookup)
or not get_adapter(self.config).get_relation(
target_model.database, target_model.schema, target_model.identifier
Expand Down
20 changes: 20 additions & 0 deletions tests/functional/defer_state/test_defer_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,26 @@ def test_run_defer_iff_not_exists(self, project, unique_schema, other_schema):
assert len(results) == 2
assert other_schema not in results[0].node.compiled_code

# again with --favor-state, but this time select both the seed and the view
# because the seed is also selected, the view should select from the seed in our schema ('other_schema')
results = run_dbt(
[
"build",
"--state",
"state",
"--select",
"seed view_model",
"--resource-type",
"seed model",
"--defer",
"--favor-state",
"--target",
"otherschema",
]
)
assert len(results) == 2
assert other_schema in results[1].node.compiled_code


class TestDeferStateDeletedUpstream(BaseDeferState):
def test_run_defer_deleted_upstream(self, project, unique_schema, other_schema):
Expand Down

0 comments on commit 84cc540

Please sign in to comment.