Skip to content

chore: increment count column in config access logs#1912

Merged
moshloop merged 2 commits intomainfrom
cnt-ac-log
Feb 25, 2026
Merged

chore: increment count column in config access logs#1912
moshloop merged 2 commits intomainfrom
cnt-ac-log

Conversation

@yashmehrotra
Copy link
Member

@yashmehrotra yashmehrotra commented Feb 24, 2026

Summary by CodeRabbit

  • Bug Fixes
    • Resolved configuration access log conflict handling to ensure accurate field updates and proper access counter incrementation during data upserts.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 24, 2026

Warning

Rate limit exceeded

@yashmehrotra has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 7 minutes and 38 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 1f9469d and 9e11c3a.

📒 Files selected for processing (1)
  • db/update.go

Walkthrough

Modified the ON CONFLICT update behavior in SaveConfigAccessLog to use a Set-based update approach. The change replaces AssignmentColumns with explicit updates to created_at, mfa, and properties from excluded values, while incrementing the count field by 1, maintaining the same WHERE gate condition.

Changes

Cohort / File(s) Summary
SaveConfigAccessLog Upsert Logic
db/update.go
Changed ON CONFLICT update from AssignmentColumns-based approach to Set-based updates, adding incremental count logic while updating created_at, mfa, and properties from excluded values.

Possibly related PRs

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'chore: increment count column in config access logs' directly describes the main change in the pull request - modifying the upsert logic to increment the count column by 1.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch cnt-ac-log

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.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
db/update.go (1)

1402-1413: ⚠️ Potential issue | 🟠 Major

count will not increment when conflicting record has an equal or older created_at

PostgreSQL's ON CONFLICT DO UPDATE SET ... WHERE <cond> gates the entire SET block on the WHERE predicate. When excluded.created_at <= config_access_logs.created_at, no assignment fires—including the new count + 1. This causes count to silently skip incrementing for out-of-order or duplicate access events.

The test suite ("Config access logs upsert") does not validate the count field behavior, creating ambiguity about intent:

  • If count should track every conflict hit (total access events), the WHERE gate must be removed and the condition inlined as CASE expressions for conditional fields.
  • If count should only increment on "fresher" events (implicit deduplication), the current behavior is correct but should be explicitly documented.
🛠️ Fix for unconditional count + conditional field updates
 return ctx.DB().Clauses(clause.OnConflict{
 	Columns: []clause.Column{{Name: "config_id"}, {Name: "external_user_id"}, {Name: "scraper_id"}},
 	DoUpdates: clause.Set{
-		{Column: clause.Column{Name: "created_at"}, Value: gorm.Expr("excluded.created_at")},
-		{Column: clause.Column{Name: "mfa"}, Value: gorm.Expr("excluded.mfa")},
-		{Column: clause.Column{Name: "properties"}, Value: gorm.Expr("excluded.properties")},
 		{Column: clause.Column{Name: "count"}, Value: gorm.Expr("config_access_logs.count + 1")},
+		{Column: clause.Column{Name: "created_at"}, Value: gorm.Expr("CASE WHEN excluded.created_at > config_access_logs.created_at THEN excluded.created_at ELSE config_access_logs.created_at END")},
+		{Column: clause.Column{Name: "mfa"}, Value: gorm.Expr("CASE WHEN excluded.created_at > config_access_logs.created_at THEN excluded.mfa ELSE config_access_logs.mfa END")},
+		{Column: clause.Column{Name: "properties"}, Value: gorm.Expr("CASE WHEN excluded.created_at > config_access_logs.created_at THEN excluded.properties ELSE config_access_logs.properties END")},
 	},
-	Where: clause.Where{Exprs: []clause.Expression{
-		clause.Expr{SQL: "excluded.created_at > config_access_logs.created_at"},
-	}},
 }).Create(accessLog).Error

Clarify the intended semantics and add a test case that validates count behavior alongside the existing timestamp-based tests.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@db/update.go` around lines 1402 - 1413, The ON CONFLICT currently wraps the
whole DoUpdates in a WHERE so count won't increment for conflicts where
excluded.created_at <= config_access_logs.created_at; change the upsert so count
is always incremented and only the timestamp/mfa/properties are conditionally
updated based on created_at. Concretely, modify the clause.OnConflict in the
upsert (the Create(accessLog) call) to remove the WHERE and replace the
DoUpdates entries so count uses an unconditional increment (e.g.,
config_access_logs.count + 1) while created_at/mfa/properties are set with
conditional expressions (CASE/GREATEST or CASE WHEN excluded.created_at >
config_access_logs.created_at THEN excluded.<field> ELSE
config_access_logs.<field> END). Also add a test in the "Config access logs
upsert" suite that asserts count increments for conflicts with older/equal
created_at to validate the intended semantics.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@db/update.go`:
- Around line 1402-1413: The ON CONFLICT currently wraps the whole DoUpdates in
a WHERE so count won't increment for conflicts where excluded.created_at <=
config_access_logs.created_at; change the upsert so count is always incremented
and only the timestamp/mfa/properties are conditionally updated based on
created_at. Concretely, modify the clause.OnConflict in the upsert (the
Create(accessLog) call) to remove the WHERE and replace the DoUpdates entries so
count uses an unconditional increment (e.g., config_access_logs.count + 1) while
created_at/mfa/properties are set with conditional expressions (CASE/GREATEST or
CASE WHEN excluded.created_at > config_access_logs.created_at THEN
excluded.<field> ELSE config_access_logs.<field> END). Also add a test in the
"Config access logs upsert" suite that asserts count increments for conflicts
with older/equal created_at to validate the intended semantics.

ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f54265d and 1f9469d.

📒 Files selected for processing (1)
  • db/update.go

@moshloop moshloop merged commit 6dc15a8 into main Feb 25, 2026
12 of 14 checks passed
@moshloop moshloop deleted the cnt-ac-log branch February 25, 2026 07:03
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.

2 participants