Skip to content

Centralise resource_name extraction from dbt unique_id#2687

Merged
pankajkoti merged 5 commits into
mainfrom
refactor-unique-id-parsing
Jun 5, 2026
Merged

Centralise resource_name extraction from dbt unique_id#2687
pankajkoti merged 5 commits into
mainfrom
refactor-unique-id-parsing

Conversation

@pankajkoti
Copy link
Copy Markdown
Contributor

@pankajkoti pankajkoti commented May 15, 2026

Summary

  • Extracts the dbt unique_id -> resource_name parse into a single
    static method DbtNode.get_resource_name_from_unique_id in
    cosmos/dbt/graph.py, with a docstring documenting the manifest spec
    and the versioned-model variant, and validation that rejects malformed
    ids (fewer than two dots or any empty segment).
  • Routes DbtNode.resource_name through the helper and replaces the
    remaining open-coded split(".", 2)[2] sites in
    cosmos/operators/watcher.py and cosmos/operators/_watcher/base.py.

🤖 Generated with Claude Code

Base automatically changed from fix-watcher-fallback-versioned-selector to main May 15, 2026 20:06
@pankajkoti pankajkoti changed the title Extract get_resource_name_from_unique_id helper Centralise resource_name extraction from dbt unique_id May 15, 2026
@pankajkoti pankajkoti force-pushed the refactor-unique-id-parsing branch from 7c2e490 to 2c909e2 Compare May 19, 2026 10:11
@pankajkoti pankajkoti force-pushed the refactor-unique-id-parsing branch from 2c909e2 to 96546a7 Compare May 26, 2026 13:48
@codecov
Copy link
Copy Markdown

codecov Bot commented May 26, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.36%. Comparing base (8efa180) to head (12f2fed).
⚠️ Report is 3 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@pankajkoti pankajkoti force-pushed the refactor-unique-id-parsing branch from 1951500 to c344aaa Compare May 26, 2026 16:24
@pankajkoti pankajkoti marked this pull request as ready for review May 26, 2026 16:54
@pankajkoti pankajkoti requested a review from jbandoro as a code owner May 26, 2026 16:54
Copilot AI review requested due to automatic review settings May 26, 2026 16:54
@pankajkoti pankajkoti requested review from a team, corsettigyg and dwreeves as code owners May 26, 2026 16:54
@pankajkoti pankajkoti requested review from pankajastro and tatiana May 26, 2026 16:54
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 dbt unique_id formats.
  • Updated DbtNode.resource_name and 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.

Comment thread cosmos/operators/watcher.py Outdated
Copy link
Copy Markdown
Contributor

@pankajastro pankajastro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment thread cosmos/operators/watcher.py Outdated
Comment thread cosmos/dbt/resource.py Outdated
pankajkoti and others added 3 commits June 5, 2026 12:49
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.
Copilot AI review requested due to automatic review settings June 5, 2026 07:19
@pankajkoti pankajkoti force-pushed the refactor-unique-id-parsing branch from b20738c to c3e3599 Compare June 5, 2026 07:19
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Comment thread cosmos/dbt/graph.py Outdated
Comment thread cosmos/dbt/graph.py
Comment thread tests/dbt/test_graph.py Outdated
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.
@pankajkoti pankajkoti merged commit c3bf94e into main Jun 5, 2026
126 checks passed
@pankajkoti pankajkoti deleted the refactor-unique-id-parsing branch June 5, 2026 08:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants