Skip to content

Commit

Permalink
Add current package selection syntax (#9270)
Browse files Browse the repository at this point in the history
Co-authored-by: Jeremy Cohen <[email protected]>
Co-authored-by: Kshitij Aranke <[email protected]>
  • Loading branch information
3 people authored May 1, 2024
1 parent 2e3c6fe commit 01f5dc8
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changes/unreleased/Features-20231212-163409.yaml
Original file line number Diff line number Diff line change
@@ -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"
4 changes: 4 additions & 0 deletions core/dbt/graph/selector_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/test_graph_selector_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/utils/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,6 @@ def manifest(
disabled={},
selectors={},
groups={},
metadata=ManifestMetadata(adapter_type="postgres"),
metadata=ManifestMetadata(adapter_type="postgres", project_name="pkg"),
)
return manifest

0 comments on commit 01f5dc8

Please sign in to comment.