From 01f5dc8b850531db3319a956d9e397994e684794 Mon Sep 17 00:00:00 2001 From: barton996 <57592874+barton996@users.noreply.github.com> Date: Wed, 1 May 2024 19:12:34 +0100 Subject: [PATCH] Add current package selection syntax (#9270) Co-authored-by: Jeremy Cohen Co-authored-by: Kshitij Aranke --- .../unreleased/Features-20231212-163409.yaml | 6 +++++ core/dbt/graph/selector_methods.py | 4 +++ tests/unit/test_graph_selector_methods.py | 27 +++++++++++++++++++ tests/unit/utils/manifest.py | 2 +- 4 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 .changes/unreleased/Features-20231212-163409.yaml diff --git a/.changes/unreleased/Features-20231212-163409.yaml b/.changes/unreleased/Features-20231212-163409.yaml new file mode 100644 index 00000000000..f9fe36a1fda --- /dev/null +++ b/.changes/unreleased/Features-20231212-163409.yaml @@ -0,0 +1,6 @@ +kind: Features +body: Package selector syntax for the current package +time: 2023-12-12T16:34:09.328891Z +custom: + Author: barton996 + Issue: "6891" diff --git a/core/dbt/graph/selector_methods.py b/core/dbt/graph/selector_methods.py index 25d3af0d493..d5d16c5c5ca 100644 --- a/core/dbt/graph/selector_methods.py +++ b/core/dbt/graph/selector_methods.py @@ -455,6 +455,10 @@ def search(self, included_nodes: Set[UniqueId], selector: str) -> Iterator[Uniqu class PackageSelectorMethod(SelectorMethod): def search(self, included_nodes: Set[UniqueId], selector: str) -> Iterator[UniqueId]: """Yields nodes from included that have the specified package""" + # `this` is an alias for the current dbt project name + if selector == "this" and self.manifest.metadata.project_name is not None: + selector = self.manifest.metadata.project_name + for unique_id, node in self.all_nodes(included_nodes): if fnmatch(node.package_name, selector): yield unique_id diff --git a/tests/unit/test_graph_selector_methods.py b/tests/unit/test_graph_selector_methods.py index 1a3a16fdafc..136131806f4 100644 --- a/tests/unit/test_graph_selector_methods.py +++ b/tests/unit/test_graph_selector_methods.py @@ -343,6 +343,33 @@ def test_select_package(manifest): } +def test_select_package_this(manifest): + new_manifest = copy.deepcopy(manifest) + + # change the package name for all nodes except ones where the unique_id contains "table_model" + for id, node in new_manifest.nodes.items(): + if "table_model" not in id: + node.package_name = "foo" + + for source in new_manifest.sources.values(): + if "table_model" not in source.unique_id: + source.package_name = "foo" + + methods = MethodManager(new_manifest, None) + method = methods.get_method("package", []) + assert isinstance(method, PackageSelectorMethod) + assert method.arguments == [] + + assert search_manifest_using_method(new_manifest, method, "this") == { + "not_null_table_model_id", + "table_model", + "table_model_csv", + "table_model_py", + "unique_table_model_id", + "unit_test_table_model", + } + + def test_select_config_materialized(manifest): methods = MethodManager(manifest, None) method = methods.get_method("config", ["materialized"]) diff --git a/tests/unit/utils/manifest.py b/tests/unit/utils/manifest.py index 2f56570df41..aef27c1be19 100644 --- a/tests/unit/utils/manifest.py +++ b/tests/unit/utils/manifest.py @@ -1001,6 +1001,6 @@ def manifest( disabled={}, selectors={}, groups={}, - metadata=ManifestMetadata(adapter_type="postgres"), + metadata=ManifestMetadata(adapter_type="postgres", project_name="pkg"), ) return manifest