Skip to content

fix: group SearchDependencies CTEs by ecosystem - #250

Merged
andrew merged 2 commits into
git-pkgs:mainfrom
abhinavgautam01:fix-issue-241
Jun 28, 2026
Merged

andrew merged 2 commits into
git-pkgs:mainfrom
abhinavgautam01:fix-issue-241

Conversation

@abhinavgautam01

Copy link
Copy Markdown
Contributor

Description

This PR fixes a bug in git pkgs search where metadata (first_seen, last_changed and added_in) could cross ecosystems for packages that share the same name (e.g., requests in PyPI vs npm).

Previously, the first_added and last_changed Common Table Expressions (CTEs) within the SearchDependencies method were only grouping and joining by the dependency name. This update ensures they group by both name and ecosystem to keep metadata strictly isolated to its respective ecosystem.

Fixes #241

Changes Made

  • Modified the first_added CTE in internal/database/queries.go to include dc.ecosystem in the SELECT and GROUP BY clauses.
  • Modified the last_changed CTE to also select and group by dc.ecosystem.
  • Updated the LEFT JOIN conditions to match on both name and ecosystem.

@andrew andrew left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Fix is correct — verified against the #241 repro, pypi requests now reports first_seen: 2021-01-01 instead of inheriting the npm date.

Could you add a regression test before merge? internal/database/database_test.go already has the setup helpers (see TestMultipleVersionsSamePackage), and the issue has a precise repro: two ecosystems, same package name, different commit dates, then assert SearchDependencies returns the correct FirstSeen/LastChanged per ecosystem. Without one this can regress silently next time someone touches the CTE.

(Unrelated to this PR but adjacent: MIN(c.sha) as added_in in the same CTE returns the lexicographically smallest SHA rather than the SHA of the earliest commit. Pre-existing, not blocking here — happy to track separately.)

@abhinavgautam01

Copy link
Copy Markdown
Contributor Author

Done! Added two regression tests and also fixed the added_in SHA issue you flagged:

  • TestSearchDependenciesCrossEcosystem, exact repro from git pkgs search metadata can cross ecosystems for same-named packages #241: same package name ( requests ) across npm and pip with different commit dates, asserts FirstSeen / LastChanged are scoped per ecosystem.

  • TestSearchDependenciesAddedInSHA, covers the MIN(c.sha) pre-existing bug you mentioned: uses a lex-large SHA ( zzz999 ) on an early commit and a lex-small SHA ( aaa000 ) on a later commit, asserts added_in returns zzz999 (the earliest commit, not the lex-smallest). Fixed by replacing MIN(c.sha) with a correlated subquery ordered by committed_at ASC LIMIT 1 .

@andrew andrew left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the tests and the bonus added_in fix.

Verified both TestSearchDependenciesCrossEcosystem and TestSearchDependenciesAddedInSHA fail on main and pass here, so they're real regression guards. Arg ordering with the extra ? is correct, and the correlated subquery hits idx_dependency_changes_name/ecosystem so it stays bounded per group.

One note for a possible follow-up (not blocking): SQLite guarantees that bare columns alongside a single MIN()/MAX() come from the row that produced the aggregate, so first_added could be SELECT dc.name, dc.ecosystem, MIN(c.committed_at) AS first_seen, c.sha AS added_in ... GROUP BY dc.name, dc.ecosystem without the subquery. We're SQLite-only via modernc so it's safe to rely on, but the explicit subquery is also fine and arguably clearer to read.

@andrew
andrew merged commit ff56085 into git-pkgs:main Jun 28, 2026
4 checks passed
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.

git pkgs search metadata can cross ecosystems for same-named packages

2 participants