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: change target path for nodes defined in yml #11323

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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-20250219-224310.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Fixes
body: Models defined in .yml files have weird paths
time: 2025-02-19T22:43:10.5172941+10:30
custom:
Author: joshuanits
Issue: "11321"
5 changes: 5 additions & 0 deletions core/dbt/contracts/graph/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,11 @@ def get_target_write_path(
if os.path.basename(self.path) == os.path.basename(self.original_file_path):
# One-to-one relationship of nodes to files.
path = self.original_file_path
elif os.path.dirname(self.path) == os.path.basename(self.original_file_path):
parent_dirname = os.path.dirname(self.original_file_path)
dirname = os.path.dirname(self.path).replace(".", "_")
basename = os.path.basename(self.path)
path = os.path.join(parent_dirname, dirname, basename)
else:
# Many-to-one relationship of nodes to files.
path = os.path.join(self.original_file_path, self.path)
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/graph/test_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,22 @@ def parsed_node(self) -> ParsedNode:
database=None,
)

@pytest.fixture(scope="class")
def parsed_yml_node(self) -> ParsedNode:
return ParsedNode(
resource_type=NodeType.Model,
unique_id="model.test_package.test_name",
name="test_name",
package_name="test_package",
schema="test_schema",
alias="test_alias",
fqn=["models", "test_name"],
original_file_path="folder/test_original_file_path.yml",
checksum=FileHash.from_contents("checksum"),
path="test_original_file_path.yml/test_path.sql",
database=None,
)

def test_get_target_write_path(self, parsed_node):
write_path = parsed_node.get_target_write_path("target_path", "subdirectory")
assert (
Expand All @@ -423,3 +439,10 @@ def test_get_target_write_path_split(self, parsed_node):
write_path
== "target_path/subdirectory/test_package/test_original_file_path/test_path/test_path_split.sql"
)

def test_get_target_write_path_for_yml_node(self, parsed_yml_node):
write_path = parsed_yml_node.get_target_write_path("target_path", "subdirectory")
assert (
write_path
== "target_path/subdirectory/test_package/folder/test_original_file_path_yml/test_path.sql"
)
Loading