Skip to content

[WIP] Add pgx helpers: PgxSession interface and CopyToTempTable generic#3759

Draft
mstaeble wants to merge 1 commit into
openshift:mainfrom
mstaeble:worktree-pgx-helpers
Draft

[WIP] Add pgx helpers: PgxSession interface and CopyToTempTable generic#3759
mstaeble wants to merge 1 commit into
openshift:mainfrom
mstaeble:worktree-pgx-helpers

Conversation

@mstaeble

@mstaeble mstaeble commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Introduces shared pgx utilities in pkg/db/pgx.go:
    • PgxSession: interface satisfied by both *pgx.Conn and pgx.Tx, enabling functions to work with either raw connections or transactions
    • TempColumn[T]: typed column definition binding a column name, SQL type, and value extractor so callers cannot have out-of-order columns
    • CopyToTempTable[T]: creates a temp table, COPYs rows into it via the COPY protocol, and returns a cleanup function. Cleans up automatically on COPY failure.
  • Converts testownershiploader to use CopyToTempTable, replacing manual CREATE TEMP TABLE + CopyFrom + DROP TABLE boilerplate

Test plan

  • make lint passes (0 issues)
  • make test passes
  • make e2e passes (108 tests, 0 failures)
  • Ran test ownership loader against staging DB: 64,045 mappings fetched, 38,970 upserted, 0 errors
  • Independent code review via isolated sub-agent

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Improvements
    • Streamlined how test ownership data is bulk-loaded into PostgreSQL temporary tables.
    • Added a reusable bulk-insert helper for temporary tables with typed column definitions and automatic cleanup.
    • Improved validation, error reporting, and failure handling during temporary table creation, data loading, and cleanup.
    • Preserved the existing upsert/delete behavior while simplifying the database loading workflow.

@openshift-merge-bot

Copy link
Copy Markdown
Contributor

Pipeline controller notification
This repo is configured to use the pipeline controller. Second-stage tests will be triggered either automatically or after lgtm label is added, depending on the repository configuration. The pipeline controller will automatically detect which contexts are required and will utilize /test Prow commands to trigger the second stage.

For optional jobs, comment /test ? to see a list of all defined jobs. To trigger manually all jobs from second stage use /pipeline required command.

This repository is configured in: automatic mode

@openshift-ci openshift-ci Bot added the ready-for-human-review Indicates a PR has been reviewed by automated tools and is ready for human review label Jul 10, 2026
@openshift-ci

openshift-ci Bot commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

@openshift-ci openshift-ci Bot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Jul 10, 2026
@openshift-ci

openshift-ci Bot commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: mstaeble
Once this PR has been reviewed and has the lgtm label, please assign dgoodwin for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@coderabbitai

coderabbitai Bot commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

@mstaeble, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 5 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Repository YAML (base), Central YAML (inherited)

Review profile: CHILL

Plan: Enterprise

Run ID: f586c764-2046-43ae-a645-6db5f72f7965

📥 Commits

Reviewing files that changed from the base of the PR and between 5d09d76 and 8527802.

📒 Files selected for processing (2)
  • pkg/dataloader/testownershiploader/testownershiploader.go
  • pkg/db/pgx.go

Walkthrough

A reusable pgx temporary-table COPY helper was added with typed column descriptors, validation, contextual errors, and cleanup. The test ownership loader now uses the helper instead of managing temporary-table creation and row copying directly.

Changes

Temporary-table bulk loading

Layer / File(s) Summary
Generic temporary-table COPY helper
pkg/db/pgx.go
Adds pgx session and typed column contracts, dynamic temporary-table creation, bulk COPY loading, identifier validation, contextual errors, and cleanup.
Test ownership loader integration
pkg/dataloader/testownershiploader/testownershiploader.go
Uses db.CopyToTempTable with typed TestOwnership extractors, removes local column ordering, and defers helper-provided cleanup.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Loader
  participant CopyToTempTable
  participant PostgreSQL
  Loader->>CopyToTempTable: Provide TestOwnership rows and column descriptors
  CopyToTempTable->>PostgreSQL: Create temporary table
  CopyToTempTable->>PostgreSQL: Bulk COPY rows
  CopyToTempTable-->>Loader: Return cleanup function
  Loader->>PostgreSQL: Drop temporary table
Loading

Possibly related PRs

  • openshift/sippy#3740: Refactors the same test ownership loader’s temporary-table bulk COPY flow.

Caution

Pre-merge checks failed

Please resolve all errors before merging. Addressing warnings is optional.

  • Ignore

❌ Failed checks (1 error, 2 warnings)

Check name Status Explanation Resolution
Sql Injection Prevention ❌ Error CopyToTempTable still interpolates c.Name and c.Type into CREATE TEMP TABLE without validation, leaving identifier/type SQL injection risk. Validate each column name, and restrict column types via allow-list/validation before building DDL; avoid raw SQL concatenation for identifiers.
Go Error Handling ⚠️ Warning loadMappings still ignores GORM's Raw(...).Scan(...).Error, so a DB query failure can be silently lost. Check and handle the returned .Error from the Raw(...).Scan(...) call, propagating it with fmt.Errorf(...: %w, err) or logging it explicitly.
Test Coverage For New Features ⚠️ Warning New code in pkg/db/pgx.go and testownershiploader has no corresponding tests; repo-wide search found no *_test.go coverage or references to the new APIs. Add unit tests for CopyToTempTable and the loader’s mapping/cleanup behavior, including invalid input and COPY-failure cleanup paths.
✅ Passed checks (18 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
Excessive Css In React Should Use Styles ✅ Passed Not applicable: the PR only changes Go backend files, with no React components or inline CSS to review.
Single Responsibility And Clear Naming ✅ Passed PASS — the new pgx helper module is narrowly scoped to temp-table bulk loading, with specific names and only 3 top-level declarations; the caller stays focused on test ownership loading.
Feature Documentation ✅ Passed docs/features only contains an unrelated job-analysis-symptoms page; no feature doc covers the pgx/temp-table or testownership-loader changes, so no update was needed.
Stable And Deterministic Test Names ✅ Passed The diff only changes db helper and loader code; no Ginkgo test titles were added or modified.
Test Structure And Quality ✅ Passed PR only changes production code in pkg/db/pgx.go and pkg/dataloader/testownershiploader.go; no Ginkgo test blocks are modified, so the test-quality check is not applicable.
Microshift Test Compatibility ✅ Passed No new Ginkgo e2e tests were added; only pgx utilities and a loader refactor changed, with no MicroShift-relevant test code.
Single Node Openshift (Sno) Test Compatibility ✅ Passed No Ginkgo e2e tests were added; the PR only changes pkg/db/pgx.go and testownershiploader.go.
Topology-Aware Scheduling Compatibility ✅ Passed Only DB/temp-table helper and a data loader changed; no manifests, controllers, replicas, affinities, or node selectors were added.
Ote Binary Stdout Contract ✅ Passed Touched code adds no stdout writes in process-level entrypoints; only logrus stderr logging and db helpers were changed.
Ipv6 And Disconnected Network Test Compatibility ✅ Passed No new Ginkgo e2e tests were added; the PR only changes db helpers and a loader, with no IPv4-specific or external-network test code.
No-Weak-Crypto ✅ Passed The touched code is pgx/SQL-only; no MD5/SHA1/DES/RC4/3DES/Blowfish/ECB, custom crypto, or secret comparisons were added.
Container-Privileges ✅ Passed PR changes only two Go files; no container/K8s manifests were modified, and no new privilege flags were introduced.
No-Sensitive-Data-In-Logs ✅ Passed New logs only emit counts, elapsed time, and a fixed temp-table name; no passwords, tokens, PII, or customer data are logged.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: new pgx helpers and the generic CopyToTempTable utility.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@mstaeble

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 10, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@mstaeble

Copy link
Copy Markdown
Contributor Author

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Jul 10, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Full review finished.


Your plan includes PR reviews subject to rate limits. More reviews will be available in 26 minutes.

@mstaeble

Copy link
Copy Markdown
Contributor Author

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Jul 10, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Full review finished.


Your plan includes PR reviews subject to rate limits. More reviews will be available in 58 minutes.

@mstaeble

Copy link
Copy Markdown
Contributor Author

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Full review finished.

@mstaeble mstaeble force-pushed the worktree-pgx-helpers branch 2 times, most recently from aa7e493 to 5d09d76 Compare July 11, 2026 14:36

@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: 1

🤖 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/db/pgx.go`:
- Around line 49-54: Validate every column name with validIdentifier before
constructing colDefs or createSQL, and return an appropriate error for any
invalid identifier. Since DDL identifiers and type definitions cannot use
placeholders, also validate c.Type using the repository’s established PostgreSQL
type allow-list or permissive type-validation rule before interpolation; keep
tempTable validation and the existing CREATE TEMP TABLE flow unchanged.
🪄 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: Repository YAML (base), Central YAML (inherited)

Review profile: CHILL

Plan: Enterprise

Run ID: b3c0fa4d-00f3-42fe-b49d-4ad34b202b2b

📥 Commits

Reviewing files that changed from the base of the PR and between 185e48a and 5d09d76.

📒 Files selected for processing (2)
  • pkg/dataloader/testownershiploader/testownershiploader.go
  • pkg/db/pgx.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • pkg/dataloader/testownershiploader/testownershiploader.go

Comment thread pkg/db/pgx.go
Introduces shared pgx utilities in pkg/db/pgx.go:

- PgxQuerier: interface satisfied by both *pgx.Conn and pgx.Tx,
  enabling functions to work with either raw connections or
  transactions.
- TempColumn[T]: typed column definition binding a column name, SQL
  type, and value extractor together so callers cannot have
  out-of-order columns.
- CopyToTempTable[T]: creates a temp table, COPYs rows into it via
  the COPY protocol, and returns a cleanup function. Cleans up
  automatically on COPY failure.

Moves the existing CopyFrom helper from copyfrom.go into the same
file.

Converts testownershiploader to use CopyToTempTable, replacing
manual CREATE TEMP TABLE + CopyFrom + DROP TABLE boilerplate.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@mstaeble mstaeble force-pushed the worktree-pgx-helpers branch from 5d09d76 to 8527802 Compare July 11, 2026 19:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. ready-for-human-review Indicates a PR has been reviewed by automated tools and is ready for human review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant