Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: dbt show --inline with private models #7838

Merged
merged 3 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/unreleased/Fixes-20230609-191546.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Fixes
body: Allow dbt show --inline preview of private models
time: 2023-06-09T19:15:46.716379-04:00
custom:
Author: jtcohen6
Issue: "7837"
3 changes: 3 additions & 0 deletions core/dbt/context/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,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
9 changes: 7 additions & 2 deletions core/dbt/parser/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1683,8 +1683,13 @@ def _process_refs_for_node(manifest: Manifest, current_project: str, node: Manif
)
continue

# Handle references to models that are private
elif isinstance(target_model, ModelNode) and target_model.access == AccessType.Private:
# Handle references to models that are private, unless this is an 'ad hoc' query (SqlOperation, RPCCall)
elif (
isinstance(target_model, ModelNode)
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(
unique_id=node.unique_id,
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 @@ -137,3 +138,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') }}"])
Loading