Skip to content

Commit

Permalink
Fix: dbt show --inline with private models (#7838)
Browse files Browse the repository at this point in the history
* Add functional test

* Check resource_type before DbtReferenceError

* Changelog entry
  • Loading branch information
jtcohen6 committed Jun 14, 2023
1 parent e7c966d commit d1b1310
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
3 changes: 3 additions & 0 deletions core/dbt/context/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,9 @@ def resolve(
elif (
target_model.resource_type == NodeType.Model
and target_model.access == AccessType.Private
# don't raise this reference error for ad hoc 'preview' queries
and self.model.resource_type != NodeType.SqlOperation
and self.model.resource_type != NodeType.RPCCall # TODO: rm
):
if not self.model.group or self.model.group != target_model.group:
raise DbtReferenceError(
Expand Down
4 changes: 3 additions & 1 deletion core/dbt/parser/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1407,10 +1407,12 @@ def _process_refs_for_node(manifest: Manifest, current_project: str, node: Manif
)
continue

# Handle references to models that are private
# Handle references to models that are private, unless this is an 'ad hoc' query (SqlOperation, RPCCall)
elif (
target_model.resource_type == NodeType.Model
and target_model.access == AccessType.Private
and node.resource_type != NodeType.SqlOperation
and node.resource_type != NodeType.RPCCall # TODO: rm
):
if not node.group or node.group != target_model.group:
raise dbt.exceptions.DbtReferenceError(
Expand Down
12 changes: 12 additions & 0 deletions tests/functional/show/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@
select current_setting('timezone') as timezone
"""

private_model_yml = """
groups:
- name: my_cool_group
owner: {name: me}
models:
- name: private_model
access: private
config:
group: my_cool_group
"""


schema_yml = """
models:
Expand Down
18 changes: 18 additions & 0 deletions tests/functional/show/test_show.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
models__ephemeral_model,
schema_yml,
models__sql_header,
private_model_yml,
)


Expand Down Expand Up @@ -123,3 +124,20 @@ def test_none(self, project):
(results, log_output) = run_dbt_and_capture(["show", "--select", "sample_model.v2"])
assert "Previewing node 'sample_model.v1'" not in log_output
assert "Previewing node 'sample_model.v2'" in log_output


class TestShowPrivateModel:
@pytest.fixture(scope="class")
def models(self):
return {
"schema.yml": private_model_yml,
"private_model.sql": models__sample_model,
}

@pytest.fixture(scope="class")
def seeds(self):
return {"sample_seed.csv": seeds__sample_seed}

def test_version_unspecified(self, project):
run_dbt(["build"])
run_dbt(["show", "--inline", "select * from {{ ref('private_model') }}"])

0 comments on commit d1b1310

Please sign in to comment.