Centralise resource_name extraction from dbt unique_id#2687
Conversation
resource_name extraction from dbt unique_id
7c2e490 to
2c909e2
Compare
2c909e2 to
96546a7
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2687 +/- ##
==========================================
+ Coverage 98.28% 98.36% +0.07%
==========================================
Files 107 107
Lines 7929 7942 +13
==========================================
+ Hits 7793 7812 +19
+ Misses 136 130 -6 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
1951500 to
c344aaa
Compare
There was a problem hiding this comment.
Pull request overview
This PR centralizes the logic for extracting a dbt node’s resource_name from its unique_id into a single helper (get_resource_name_from_unique_id) and updates watcher/operator codepaths (and tests) to use it, including support for versioned models and multi-segment resource names (e.g. sources).
Changes:
- Added
cosmos.dbt.resource.get_resource_name_from_unique_id()with documented dbtunique_idformats. - Updated
DbtNode.resource_nameand watcher fallback command generation to use the helper instead of open-coded splitting. - Added/updated tests to cover the helper and watcher fallback behavior (including sources).
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
cosmos/dbt/resource.py |
Adds the centralized helper for parsing resource_name from unique_id. |
cosmos/dbt/graph.py |
Routes DbtNode.resource_name through the new helper and simplifies the docstring. |
cosmos/operators/watcher.py |
Replaces open-coded unique_id parsing in watcher logic and retry fallbacks with the helper. |
cosmos/operators/_watcher/base.py |
Uses the helper for model selector extraction in fallback retry command generation. |
tests/dbt/test_resource.py |
Adds unit tests for the new helper (plain/versioned/malformed IDs). |
tests/operators/test_watcher.py |
Updates assertions to use the helper and adds a source-freshness fallback test. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
The dbt unique_id-to-resource_name parse (split(".", 2)[2]) was
open-coded in several places. PR #2659 added one more such site to fix
a versioned-model bug. Consolidating the parse into a single helper
with a docstring documents the unique_id format in one place and
removes the drift risk between call sites.
Add get_resource_name_from_unique_id in cosmos/dbt/resource.py with a
docstring citing the dbt manifest spec and the versioned-model variant.
Route DbtNode.resource_name through it, then replace the open-coded
splits in cosmos/operators/watcher.py and cosmos/operators/_watcher/
base.py.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Three additional inline split(".", 2)[2] sites came in from a main
merge: one in DbtTestWatcherOperator's fallback in
cosmos/operators/watcher.py and two assertions in
tests/operators/test_watcher.py. Route them through
get_resource_name_from_unique_id for consistency with the helper
introduced earlier in this PR.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
DbtSourceWatcherOperator._fallback_to_non_watcher_run had no test coverage, leaving the helper call there flagged by codecov/patch. Cover it directly, asserting cmd_flags is ["--select", "source:<resource_name>"]. Use a multi-segment resource name (raw.orders) to also pin the maxsplit=2 behaviour that preserves dots after the package segment.
Replace the standalone cosmos.dbt.resource module with a DbtNode.get_resource_name_from_unique_id static method and route all call sites through it. Collapse the redundant double split in the source-freshness exclude path into a single helper call, logging a warning on malformed unique_ids instead of silently dropping them.
b20738c to
c3e3599
Compare
Validate that the unique_id splits into exactly three non-empty segments before returning the resource name, raising ValueError on shapes like 'model..name', '..' or 'model.pkg.' instead of silently mis-parsing them.
Summary
unique_id->resource_nameparse into a singlestatic method
DbtNode.get_resource_name_from_unique_idincosmos/dbt/graph.py, with a docstring documenting the manifest specand the versioned-model variant, and validation that rejects malformed
ids (fewer than two dots or any empty segment).
DbtNode.resource_namethrough the helper and replaces theremaining open-coded
split(".", 2)[2]sites incosmos/operators/watcher.pyandcosmos/operators/_watcher/base.py.🤖 Generated with Claude Code