Skip to content

fix(auditlog): cast audit thread key to text for legacy uuid schemas - #615

Merged
SantiagoDePolonia merged 2 commits into
mainfrom
fix/audit-logs-failed
Jul 29, 2026
Merged

fix(auditlog): cast audit thread key to text for legacy uuid schemas#615
SantiagoDePolonia merged 2 commits into
mainfrom
fix/audit-logs-failed

Conversation

@SantiagoDePolonia

@SantiagoDePolonia SantiagoDePolonia commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Summary

Since v0.1.63 the Audit Logs page comes up empty on PostgreSQL deployments upgraded from older releases, and the gateway logs:

failed to count audit sessions: ERROR: COALESCE types text and uuid cannot be matched (SQLSTATE 42804)

Root cause: databases created before the unified schema (#586) have audit_logs.id as uuid (the old standalone PostgreSQL store's DDL); CREATE TABLE IF NOT EXISTS never retypes it. The session thread key added in #602COALESCE(NULLIF(session_id, ''), id) — then mixes text with uuid, so every GET /admin/audit/sessions call fails, and since group-by-session is the dashboard's default view, the page shows no entries. Fresh databases and SQLite are unaffected, which is why the suite stayed green.

Fix: cast the id inside the thread key: COALESCE(NULLIF(session_id, ''), CAST(id AS TEXT)). CAST(… AS TEXT) is valid on both backends and a no-op on SQLite, where the column is already TEXT.

User-visible impact

The Audit Logs page (session threads view) works again on PostgreSQL databases upgraded from pre-unification releases. No behavior change for fresh databases or SQLite.

Testing

  • New TestSQLReader_GetSessionsOnLegacyUUIDSchema recreates the exact pre-unification PostgreSQL DDL (uuid id, uuid audit_log_id FK, no session_id column — the migration adds it) and runs GetSessions. Against the unfixed query it reproduces the reported SQLSTATE 42804 error verbatim; with the fix it passes.
  • Full internal/auditlog suite green on SQLite and PostgreSQL (GOMODEL_TEST_POSTGRES_URL set), build and vet clean.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved audit session retrieval compatibility with older PostgreSQL audit log schemas.
    • Ensured session thread grouping and computed session details (latest entry, session identifier aggregation, activity counts) work correctly when legacy UUID-based data is present.
  • Tests
    • Added a regression test that validates session retrieval behavior on legacy PostgreSQL setups, including correct thread count, grouping, and returned per-thread aggregates.

PostgreSQL databases created before the unified schema (#586) keep their
original uuid audit_logs.id column - CREATE TABLE IF NOT EXISTS never
retypes it. The session thread key added in #602 coalesces id with the
text session_id column, so every GET /admin/audit/sessions call on such
a database failed with SQLSTATE 42804 (COALESCE types text and uuid
cannot be matched), leaving the Audit Logs page empty in its default
group-by-session view.

Cast the id to text inside the thread key; the cast is valid on both
backends and a no-op on SQLite. Covered by a regression test that
recreates the exact pre-unification PostgreSQL DDL and runs GetSessions
against it.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings July 29, 2026 09:01

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@coderabbitai

coderabbitai Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 4aef2874-3335-442d-83bc-abc8f88c839c

📥 Commits

Reviewing files that changed from the base of the PR and between 1745a3e and c204e3f.

📒 Files selected for processing (1)
  • internal/auditlog/session_id_test.go

📝 Walkthrough

Walkthrough

The SQL reader casts legacy UUID audit-log IDs to text when deriving thread keys. A PostgreSQL regression test creates legacy tables, seeds entries, and verifies grouped sessions, latest IDs, session IDs, and counts.

Changes

Legacy audit session compatibility

Layer / File(s) Summary
Legacy session key and regression coverage
internal/auditlog/reader_sessions_sql.go, internal/auditlog/session_id_test.go
The thread-key SQL expression casts legacy UUID IDs to text, and PostgreSQL regression coverage verifies singleton and multi-entry session grouping results.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: copilot

Poem

A rabbit hopped through UUID snow,
And taught old schemas text to flow.
Threads now gather, counts align,
Latest logs in order shine.
“Hop hooray!” the burrow sings.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise and accurately describes the main fix for legacy PostgreSQL audit log schemas.
Description check ✅ Passed The description includes the change, root cause, user impact, and testing, though it uses 'Summary' instead of the template's 'Description' heading.
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.
✨ 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 fix/audit-logs-failed

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.

@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 `@internal/auditlog/session_id_test.go`:
- Around line 213-218: Update the session summary assertions in the test around
result.Sessions to use table-driven expected summaries, including each session’s
key, count, and Latest.ID. Ensure sess-a explicitly expects the newer UUID
ending in 0002, while preserving the existing expectation for the other session.
🪄 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: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 36089eae-fa29-4f80-b0e4-7e9f52ad9205

📥 Commits

Reviewing files that changed from the base of the PR and between 20aa6c5 and 1745a3e.

📒 Files selected for processing (2)
  • internal/auditlog/reader_sessions_sql.go
  • internal/auditlog/session_id_test.go

Comment thread internal/auditlog/session_id_test.go Outdated
Table-drive the session summaries and assert Latest.ID for the grouped
thread too, so a regression in the per-thread recency ranking on the
uuid id column cannot slip past the count check.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings July 29, 2026 09:05

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@greptile-apps

greptile-apps Bot commented Jul 29, 2026

Copy link
Copy Markdown

Confidence Score: 5/5

The change is safe to merge with no actionable defects identified.

The cast is valid for both supported SQL dialects, preserves existing grouping semantics, fixes the legacy PostgreSQL type mismatch, and is covered by fresh-schema and legacy-schema tests.

T-Rex T-Rex Logs

What T-Rex did

  • T-Rex attempted the PostgreSQL compatibility path; the run exited with code 0 and skipped legacy-schema subtests because SQLite is inapplicable and PostgreSQL has no configured URL.
  • T-Rex executed the changed-code fallback and saw an exit code of 0 with all applicable SQLite GetSessions cases passing.
  • T-Rex evaluated parent behavior without CAST(id AS TEXT) on SQLite and confirmed it passes, implying that a reachable PostgreSQL instance is required to distinguish SQLSTATE 42804 behavior.

View all artifacts

T-Rex Ran code and verified through T-Rex

Reviews (1): Last reviewed commit: "fix(auditlog): cast audit thread key to ..." | Re-trigger Greptile

@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@SantiagoDePolonia
SantiagoDePolonia merged commit cdad145 into main Jul 29, 2026
19 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.

3 participants