Skip to content

[Fix] Load event history with its events from one snapshot - #125

Merged
Aloento merged 3 commits into
mainfrom
fix/issue-120-event-snapshot
Dec 1, 2026
Merged

Aloento merged 3 commits into
mainfrom
fix/issue-120-event-snapshot

Conversation

@Aloento

@Aloento Aloento commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

The event list and its incident_status history are read in two statements, so a concurrent update could pair stale rows with newer history. Both reads now run in one read-only REPEATABLE READ transaction. Eager loading is not an option: the schema has no Incident→IncidentStatus edge because production carries no foreign key.

Verification: go build ./..., go vet ./..., go test ./internal/.... The new TestInfoEventsIncludeOwnStatuses runs in the Postgres-backed acceptance suite and passes there.

Closes #120

@Aloento Aloento self-assigned this Dec 1, 2026
ecosquad-autoreview[bot]
ecosquad-autoreview Bot previously approved these changes Dec 1, 2026

@ecosquad-autoreview ecosquad-autoreview 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.

Summary

The fix is sound: getEventsByType now runs both the incident query and the incident_status query on a single read-only REPEATABLE READ transaction (internal/db/event_types.go:15-42, internal/db/tx.go:67-74). Because begin() binds the Ent client to the exact *sql.Tx connection (internal/db/tx.go:50-66), both statements share one connection and one Postgres snapshot, which is exactly what is needed to keep history rows paired with their incidents. The statusesByIncident → loadIncidentStatuses refactor (internal/db/mappers.go:119-135) is behavior-preserving, and WithTx/execWithTx still pass nil opts, so write transactions are unaffected.

Verdict: approve — no correctness, security, or build issues found in the diff. Findings below are suggestions.

Findings

Suggestion — same two-statement pattern remains on other read paths

The race fixed here (events and their incident_status history read in separate statements under READ COMMITTED) still exists on the paths that keep calling db.statusesByIncident against the pool client:

  • internal/db/db.go:242-246 (list path)
  • internal/db/db.go:279-283 (GetIncident)
  • internal/db/db.go:599-603 (bulk/RSS path)
  • internal/db/raw.go:83-87

A concurrent status insert between the two statements can still produce a stale or missing history there. If the invariant from #120 matters for those endpoints too, reuse beginRead + loadIncidentStatuses (or route them through getEventsByType) and add a test per path. At minimum, note in the issue that #120 only covers info/scheduled event listings.

Suggestion — new test is unverified against the schema

TestInfoEventsIncludeOwnStatuses (tests/events_snapshot_test.go) was not run locally (no Docker), as the description states. Two things worth confirming when CI's go-test-acc runs:

  1. The raw seed INSERT INTO incident (text, start_date, impact, system, type, status) (tests/events_snapshot_test.go:55-57) omits created_at/modified_at and description; if those columns are NOT NULL without defaults in the migrations, the insert fails.
  2. require.Len(t, events, 2) assumes GetInfoEvents returns every info event unfiltered — this holds today (internal/db/info.go:12 calls getEventsByType with no start_date/status filter), but the test would silently start failing if a visibility filter is ever added.

Both are acceptable to leave for CI, just flagging since the author explicitly did not run this suite.

Suggestion — per-call transaction cost

Every GetInfoEvents/GetScheduledEvents call now pins a pool connection for the full duration of the call (including mapping and chunked status queries). The transaction is short and Postgres doesn't block VACUUM on read-only REPEATABLE READ snapshots, so this is fine at current scale — just note it's a deliberate trade-off if these endpoints ever see high QPS.

Suggestion — rolled-back error discarded

defer func() { _ = tx.rollback() }() (internal/db/event_types.go:21) discards rollback errors. For a read-only transaction this is benign and matches the existing execWithTx pattern, no change required.

@ecosquad-autoreview ecosquad-autoreview 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.

Summary

The change makes getEventsByType read the event rows and their incident_status history in a single read-only REPEATABLE READ transaction, so both statements observe the same snapshot and a concurrent update can no longer pair stale rows with newer history. The refactor into loadIncidentStatuses(ctx, client, ids) is clean, the begin(ctx, opts) signature change is applied at both existing call sites (WithTx, execWithTx), and a regression test covering the exact scenario (two incidents, mismatched histories) is added. CI (go-test-acc, build, lint, CodeQL) is green.

Findings

No critical issues.

Suggestion

  • Same two-statement race remains elsewhere — the pattern fixed here (incident rows + statusesByIncident in separate, non-transactional statements) also exists in GetEventsWithCount (internal/db/db.go:242), GetIncident (internal/db/db.go:279), a third caller at db.go:599, and raw.go:83. If issue #120's concern is general (events paired with inconsistent status history), those paths are still vulnerable. Worth a follow-up PR or expanding the scope of this one; no blocker for merging the targeted fix.
  • Per-call Ent client — each getEventsByType call constructs a new *ent.Client (via beginRead/begin). Overhead is negligible for list endpoints, but if this becomes a hot path, the client could be created once per transaction and the pattern is fine as-is. No action needed.

Notes (non-issues, verified)

  • beginRead with ReadOnly: true + REPEATABLE READ is safe on PostgreSQL (write skew only applies to writing transactions), and the deferred rollback() after a successful read is correct since the transaction is never committed; ignoring the rollback error is acceptable for a read-only txn.
  • The Tx driver (sharedTxDriver with NopTx) ensures both queries run on the same connection/snapshot, which is exactly what the fix requires.
  • The test's helpers (truncateIncidents, openRawDB, insertIncidentStatus, databaseURL) all exist in the tests package, and the acceptance suite passes in CI.

Approve.

…napshot

# Conflicts:
#	internal/db/event_types.go
#	internal/db/mappers.go

@ecosquad-autoreview ecosquad-autoreview 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.

Summary

Fixes a snapshot inconsistency in getEventsByType (used by GetInfoEvents and GetMaintenances): the incident rows and their incident_status history are read in two separate statements, and under the default READ COMMITTED each statement can see a different point in time. Both reads now run inside a single read-only REPEATABLE READ transaction on one connection, via a new beginRead helper in internal/db/tx.go. A Postgres-backed acceptance test (tests/events_snapshot_test.go) verifies statuses attach to the right incidents.

The change is correct and well-scoped:

  • internal/db/tx.go: begin now takes *sql.TxOptions (callers pass nil, i.e. unchanged behavior); beginRead correctly maps to BEGIN ... ISOLATION LEVEL REPEATABLE READ READ ONLY via pgx stdlib. Read-only transactions cannot trigger Postgres serialization aborts, so REPEATABLE READ here is safe.
  • internal/db/event_types.go: both the incident query and statusesByIncident run through tx.client (bound to the same transactional connection), and the deferred rollback releases the pooled connection on every path. Snapshot staleness is a non-issue since the transaction is short-lived within one request.
  • tests/events_snapshot_test.go: helpers (truncateIncidents, openRawDB, insertIncidentStatus, databaseURL) all exist in the tests package; assertions are sound (grouping by ID, per-incident counts and ordering).

Findings

Suggestion — same two-statement pattern remains in other read paths (internal/db/db.go)

getEventsWithCount (used by GetEvents/GetEventsInternal/GetEventsInternalTx, i.e. the paginated /events list) and GetIncident still read incidents and incident_status in two statements with no shared snapshot, so they can exhibit the same skew this PR fixes. If #120 only covers the info/maintenance event lists (which the title and test suggest), that's fine — but consider a follow-up or a note in the PR description clarifying that the main list endpoint is intentionally left as-is (e.g. because REPEATABLE READ holds a pool connection longer for large pages).

Note — CI not yet green at review time

go-test, go-test-acc (the new acceptance test) and build were still in_progress when this review was written, so the claim that TestInfoEventsIncludeOwnStatuses passes against Postgres is unverified here. Please confirm the acceptance suite is green before merging.

Verdict

Approve — no correctness, security or build issues found in the diff.

@Aloento Aloento closed this Dec 1, 2026
@Aloento Aloento reopened this Dec 1, 2026
@Aloento
Aloento merged commit 35e1265 into main Dec 1, 2026
23 checks passed
@Aloento
Aloento deleted the fix/issue-120-event-snapshot branch December 1, 2026 11:12
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.

[checker] Load incidents and statuses from one snapshot

1 participant