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" 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, 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') }}"])