From 21e08d88c1d53b4d222d611e159568fbddd007dd Mon Sep 17 00:00:00 2001 From: Jeremy Cohen Date: Fri, 9 Jun 2023 19:14:08 -0400 Subject: [PATCH 1/3] Add functional test --- tests/functional/show/fixtures.py | 12 ++++++++++++ tests/functional/show/test_show.py | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/tests/functional/show/fixtures.py b/tests/functional/show/fixtures.py index d3d8c57e96c..85bfcd26c29 100644 --- a/tests/functional/show/fixtures.py +++ b/tests/functional/show/fixtures.py @@ -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: diff --git a/tests/functional/show/test_show.py b/tests/functional/show/test_show.py index 69686ebb09b..838b65e4254 100644 --- a/tests/functional/show/test_show.py +++ b/tests/functional/show/test_show.py @@ -10,6 +10,7 @@ models__ephemeral_model, schema_yml, models__sql_header, + private_model_yml, ) @@ -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') }}"]) From 108a0a7517ee200e715e2303030d7ae644f71372 Mon Sep 17 00:00:00 2001 From: Jeremy Cohen Date: Fri, 9 Jun 2023 19:14:36 -0400 Subject: [PATCH 2/3] Check resource_type before DbtReferenceError --- core/dbt/context/providers.py | 3 +++ core/dbt/parser/manifest.py | 9 +++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/core/dbt/context/providers.py b/core/dbt/context/providers.py index fe279a7fd3f..c4df007e806 100644 --- a/core/dbt/context/providers.py +++ b/core/dbt/context/providers.py @@ -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( diff --git a/core/dbt/parser/manifest.py b/core/dbt/parser/manifest.py index 1dbf39e01ad..e11acfddaa3 100644 --- a/core/dbt/parser/manifest.py +++ b/core/dbt/parser/manifest.py @@ -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, From dccddb6875894ca78e0fad7cd522f67fd7c6a1f8 Mon Sep 17 00:00:00 2001 From: Jeremy Cohen Date: Fri, 9 Jun 2023 19:15:52 -0400 Subject: [PATCH 3/3] Changelog entry --- .changes/unreleased/Fixes-20230609-191546.yaml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changes/unreleased/Fixes-20230609-191546.yaml diff --git a/.changes/unreleased/Fixes-20230609-191546.yaml b/.changes/unreleased/Fixes-20230609-191546.yaml new file mode 100644 index 00000000000..147d52b2c58 --- /dev/null +++ b/.changes/unreleased/Fixes-20230609-191546.yaml @@ -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"