Skip to content

Update patch policy generation and tests - #45799

Merged
allenhouchins merged 4 commits into
mainfrom
allenhouchins-update-patch-policy-gen
May 19, 2026
Merged

Update patch policy generation and tests#45799
allenhouchins merged 4 commits into
mainfrom
allenhouchins-update-patch-policy-gen

Conversation

@allenhouchins

@allenhouchins allenhouchins commented May 19, 2026

Copy link
Copy Markdown
Member

This pull request refactors how patch policy SQL queries are generated and validated, with the main goal of simplifying and correcting the construction of NOT EXISTS queries for version checks. The changes ensure that the generated queries are more accurate, especially in cases involving SQL OR conditions and platform-specific version columns. The update also adapts related test cases to match the new query structure.

Patch policy query generation improvements:

  • Refactored the SQL generation logic in GenerateQueryForManifest to append the version_compare clause directly inside the original WHERE clause, rather than wrapping the entire query in extra parentheses. This results in simpler, more standard SQL queries.
  • Added logic to detect OR conditions in the WHERE clause and wrap them in parentheses to ensure correct SQL precedence when appending the AND version_compare(...) clause.
  • Improved selection of the version column (e.g., bundle_short_version, version, or file_version) based on platform and table name, ensuring correct queries for both macOS and Windows policies.

Test updates:

  • Updated all relevant test cases in patch_policy_test.go to expect the new, simplified query format, removing the extra parentheses and validating correct handling of SQL with OR and platform-specific columns. [1] [2] [3] [4]
  • Adjusted a Homebrew ingester test to match the new query formatting, ensuring consistency across the codebase.

Rework GenerateQueryForManifest to build the NOT EXISTS query more robustly: switch from string templates to helper functions (parenthesizeWhereClause, versionCompareSuffix, versionCompareColumn, tableFromExistsQuery) that trim/adjust the exists query, only parenthesize WHERE bodies containing OR, and choose the correct version column (use file_version for file table on Windows). Remove previous percent-escaping approach and simplify query assembly. Update tests and fixtures to match the new SQL formatting (remove extra inner parentheses) and to use file_version in the Codex CLI Windows manifest.
@allenhouchins allenhouchins changed the title Refactor patch policy SQL generation and tests Update patch policy generation and tests May 19, 2026
@codecov

codecov Bot commented May 19, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 57.50000% with 17 lines in your changes missing coverage. Please review.
✅ Project coverage is 66.77%. Comparing base (fdc101e) to head (f0589c2).
⚠️ Report is 6 commits behind head on main.

Files with missing lines Patch % Lines
pkg/patch_policy/patch_policy.go 57.50% 11 Missing and 6 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #45799      +/-   ##
==========================================
+ Coverage   66.76%   66.77%   +0.01%     
==========================================
  Files        2746     2747       +1     
  Lines      219601   219842     +241     
  Branches    11008    10996      -12     
==========================================
+ Hits       146609   146796     +187     
- Misses      59738    59774      +36     
- Partials    13254    13272      +18     
Flag Coverage Δ
backend 68.60% <57.50%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@allenhouchins
allenhouchins marked this pull request as ready for review May 19, 2026 15:47
@allenhouchins
allenhouchins requested a review from a team as a code owner May 19, 2026 15:47
Copilot AI review requested due to automatic review settings May 19, 2026 15:47

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Claude Code Review

This repository is configured for manual code reviews. Comment @claude review to trigger a review and subscribe this PR to future pushes, or @claude review once for a one-time review.

Tip: disable this comment in your organization's Code Review settings.

@coderabbitai

coderabbitai Bot commented May 19, 2026

Copy link
Copy Markdown
Contributor

Walkthrough

This PR refactors the patch-policy SQL query generation in GenerateQueryForManifest. The new implementation wraps the caller-provided ExistsQuery inside SELECT 1 WHERE NOT EXISTS (...) and appends a version_compare(...) predicate. It replaces the prior template-based approach with composable helpers: parenthesizeWhereClause to control precedence when OR operators are present, versionCompareSuffix and versionCompareColumn to generate platform-specific version comparisons, and tableFromExistsQuery to infer whether to use file_version or version columns. Test expectations are updated to reflect the new query structure, and configuration files are synchronized accordingly.

Possibly related PRs

  • fleetdm/fleet#45244: Adjusts the Homebrew ingester's generated "patched" SQL structure (NOT EXISTS parentheses and version_compare placement) and updates test expectations for Docker Desktop and non-docker-desktop cases.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 28.57% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Update patch policy generation and tests' clearly and concisely summarizes the main changes across the pull request, which refactor the patch policy query generation and update corresponding tests.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description check ✅ Passed The PR description is comprehensive, clearly explaining the refactoring goals, specific improvements made, and test updates with proper context.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch allenhouchins-update-patch-policy-gen

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copilot AI 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.

Pull request overview

This PR updates patch policy query generation for maintained app manifests by rebuilding the NOT EXISTS patch query in a more SQL-idiomatic way, and adjusting tests/fixtures to match the new formatting and Windows file version semantics.

Changes:

  • Reworked GenerateQueryForManifest to append version_compare(...) inside the exists query’s WHERE clause, and only parenthesize the WHERE body when it contains OR.
  • Added helper functions to derive the correct version column (including file_version for Windows file table queries) and to extract the table name from the exists query.
  • Updated Go tests and the Codex CLI Windows manifest fixture to reflect the new SQL formatting and column selection.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
pkg/patch_policy/patch_policy.go Refactors patch policy query generation into helper functions; fixes OR-precedence handling and selects file_version for Windows file queries.
pkg/patch_policy/patch_policy_test.go Updates expected SQL strings and adds coverage for OR-precedence + Windows file_version behavior.
ee/maintained-apps/outputs/codex-cli/windows.json Updates patched query to use file_version for the Windows file table.
ee/maintained-apps/ingesters/homebrew/ingester_test.go Updates expected patched query formatting to match the new generator output.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@pkg/patch_policy/patch_policy.go`:
- Around line 44-50: Ensure the generated NOT EXISTS SQL only appends the
version_compare predicate when the original EXISTS query contains a WHERE
clause: after computing before (from p.ExistsQuery and parenthesizeWhereClause),
validate that before contains a WHERE clause (use a case-insensitive check or
regex for word boundary "WHERE") and if not, return an explicit error instead of
returning notExistsStart + before + suffix; update the function that builds this
SQL (referencing p.ExistsQuery, before, parenthesizeWhereClause, notExistsStart,
and suffix) to perform this guard and emit a clear error message when WHERE is
missing.
- Around line 71-77: The function versionCompareSuffix builds an SQL literal by
interpolating the version argument directly, which allows a single quote in
version to break the SQL; modify versionCompareSuffix to escape single quotes in
version (replace ' with '' per SQL standard) before formatting the string so the
produced clause is safe, keeping use of versionCompareColumn(platform,
existsQuery) unchanged and returning the formatted AND version_compare(...)
clause with the escaped version.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 2c5574bb-4768-450b-b98c-3a10439af816

📥 Commits

Reviewing files that changed from the base of the PR and between 3e33b4c and cad5c58.

📒 Files selected for processing (4)
  • ee/maintained-apps/ingesters/homebrew/ingester_test.go
  • ee/maintained-apps/outputs/codex-cli/windows.json
  • pkg/patch_policy/patch_policy.go
  • pkg/patch_policy/patch_policy_test.go

Comment on lines 44 to +50
before, _ := strings.CutSuffix(p.ExistsQuery, ";")
// Escape any literal '%' in the exists query (e.g. SQL LIKE patterns)
// so fmt.Sprintf doesn't interpret them as format verbs.
before = strings.ReplaceAll(before, "%", "%%")
before = strings.TrimSpace(before)
if strings.Contains(before, " OR ") {
before = parenthesizeWhereClause(before)
}

return notExistsStart + before + suffix, nil

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.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Validate ExistsQuery has a WHERE clause before appending AND version_compare(...).

Line 50 can emit invalid SQL for inputs like SELECT 1 FROM apps; (... FROM apps AND version_compare(...)) and also permits unscoped matching. Guard this early and return an explicit error when WHERE is missing.

Suggested fix
 var (
 	ErrWrongPlatform = errors.New("platform should be darwin or windows")
 	ErrNoExistsQuery = errors.New("exists query was not provided")
+	ErrInvalidExistsQuery = errors.New("exists query must include a WHERE clause")
 )
@@
 	before, _ := strings.CutSuffix(p.ExistsQuery, ";")
 	before = strings.TrimSpace(before)
+	if !strings.Contains(strings.ToUpper(before), " WHERE ") {
+		return "", ErrInvalidExistsQuery
+	}
 	if strings.Contains(before, " OR ") {
 		before = parenthesizeWhereClause(before)
 	}

As per coding guidelines: “ensure that appropriate filtering criteria are applied… Check for missing WHERE clauses…”

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@pkg/patch_policy/patch_policy.go` around lines 44 - 50, Ensure the generated
NOT EXISTS SQL only appends the version_compare predicate when the original
EXISTS query contains a WHERE clause: after computing before (from p.ExistsQuery
and parenthesizeWhereClause), validate that before contains a WHERE clause (use
a case-insensitive check or regex for word boundary "WHERE") and if not, return
an explicit error instead of returning notExistsStart + before + suffix; update
the function that builds this SQL (referencing p.ExistsQuery, before,
parenthesizeWhereClause, notExistsStart, and suffix) to perform this guard and
emit a clear error message when WHERE is missing.

Comment on lines +71 to +77
func versionCompareSuffix(platform, existsQuery, version string) (string, error) {
column, err := versionCompareColumn(platform, existsQuery)
if err != nil {
return "", err
}
return fmt.Sprintf(" AND version_compare(%s, '%s') < 0);", column, version), nil
}

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.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Escape single quotes in version before SQL interpolation.

Line 76 interpolates version directly into a quoted SQL literal. A version containing ' can break SQL or alter predicate semantics. Escape with SQL-standard doubled quotes.

Suggested fix
 func versionCompareSuffix(platform, existsQuery, version string) (string, error) {
 	column, err := versionCompareColumn(platform, existsQuery)
 	if err != nil {
 		return "", err
 	}
-	return fmt.Sprintf(" AND version_compare(%s, '%s') < 0);", column, version), nil
+	safeVersion := strings.ReplaceAll(version, "'", "''")
+	return fmt.Sprintf(" AND version_compare(%s, '%s') < 0);", column, safeVersion), nil
 }

As per coding guidelines: “Review all SQL queries for possible SQL injection.”

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func versionCompareSuffix(platform, existsQuery, version string) (string, error) {
column, err := versionCompareColumn(platform, existsQuery)
if err != nil {
return "", err
}
return fmt.Sprintf(" AND version_compare(%s, '%s') < 0);", column, version), nil
}
func versionCompareSuffix(platform, existsQuery, version string) (string, error) {
column, err := versionCompareColumn(platform, existsQuery)
if err != nil {
return "", err
}
safeVersion := strings.ReplaceAll(version, "'", "''")
return fmt.Sprintf(" AND version_compare(%s, '%s') < 0);", column, safeVersion), nil
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@pkg/patch_policy/patch_policy.go` around lines 71 - 77, The function
versionCompareSuffix builds an SQL literal by interpolating the version argument
directly, which allows a single quote in version to break the SQL; modify
versionCompareSuffix to escape single quotes in version (replace ' with '' per
SQL standard) before formatting the string so the produced clause is safe,
keeping use of versionCompareColumn(platform, existsQuery) unchanged and
returning the formatted AND version_compare(...) clause with the escaped
version.

Resolve modify/delete conflict for codex-cli/windows.json by accepting
main's removal of Codex CLI (#45802).
@allenhouchins
allenhouchins marked this pull request as draft May 19, 2026 16:19
@allenhouchins
allenhouchins marked this pull request as ready for review May 19, 2026 16:21
@allenhouchins
allenhouchins removed request for a team, eashaw, ireedy and mike-j-thomas May 19, 2026 17:13
@allenhouchins
allenhouchins merged commit 48b8eda into main May 19, 2026
45 checks passed
@allenhouchins
allenhouchins deleted the allenhouchins-update-patch-policy-gen branch May 19, 2026 22:17
@coderabbitai coderabbitai Bot mentioned this pull request Jun 11, 2026
3 tasks
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.

3 participants