Skip to content

[Fix] Guard ReOpenIncident with a version predicate - #134

Merged
Aloento merged 2 commits into
mainfrom
fix/issue-118-reopen-version
Dec 1, 2026
Merged

Aloento merged 2 commits into
mainfrom
fix/issue-118-reopen-version

Conversation

@Aloento

@Aloento Aloento commented Dec 1, 2026

Copy link
Copy Markdown
Member

ReOpenIncident issued ClearEndDate() with no version predicate, so a stale copy could reopen an incident another writer had already changed.

The update now runs Where(incident.IDEQ(id), incident.VersionEQ(*inc.Version)), sets version+1 in the same statement and returns ErrVersionConflict when zero rows match. reopenIncident maps that conflict to the existing 409 response instead of 500. A call for a missing incident now also reports ErrVersionConflict instead of succeeding silently, consistent with the zero-row rule.

Verification: go build ./..., go vet ./..., go test ./internal/.... TestReOpenIncidentVersionPredicate in tests/reopen_version_test.go checks that a stale version leaves end_date and the version untouched and that a current version clears end_date and bumps the version; the tests/ suite runs in CI against Postgres.

Closes #118

ReOpenIncident cleared end_date without checking the caller's version, so a
stale copy could reopen an incident modified after it was read. The update now
matches on the loaded version, bumps it, and reports ErrVersionConflict when
the row no longer matches. The patch handler maps that conflict to 409 instead
of 500.
@Aloento Aloento self-assigned this 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

Good fix for #118. ReOpenIncident now runs a single atomic UPDATE ... WHERE id = ? AND version = ? that clears end_date and bumps the version in the same statement, returning db.ErrVersionConflict on zero rows — this correctly closes the stale-write race that UpdateOneID + unconditional ClearEndDate() had. The handler maps the conflict to the existing 409 path, and the new Postgres-backed test in tests/reopen_version_test.go covers both the conflict and the success case. All CI checks (build, go-test, go-test-acc, lint, CodeQL) are green.

Verdict: approve — no blocking issues, but two behavior concerns worth addressing.

Findings

Warning — nil-version rejection surfaces as a 500

internal/db/db.go (ReOpenIncident, new guard): when inc.Version == nil the function returns an ad-hoc errors.New("version is required for incident modification"). This is not db.ErrVersionConflict (and no other sentinel), so in internal/api/v2/v2.go:1216-1226 it falls into the logger.Error(... database error) branch and is reported as a 500. A missing client-supplied version is an input error, not a server error.

Note this is reachable in practice: PatchIncidentHandler only requires a version for maintenance (v2.go:1149) and only copies incData.Version onto storedIncident when non-nil (v2.go:1175-1176). For a non-maintenance incident, the version used is whatever getEventFromContext populated; if that path can ever yield a db.Incident with a nil Version, a previously working reopen now returns 500 instead of the old behavior (unconditional success).

Fix: define a sentinel (e.g. db.ErrVersionMissing) in internal/db/errors.go, return it from the guard, and map it to a 400 in reopenIncident before the conflict branch. Also double-check that getEventFromContext → GetIncident → incidentFromEnt always populates Version.

Suggestion — missing incident maps to 409 instead of 404

Previously ReOpenIncident swallowed ent.IsNotFound and returned nil (silent success — the bug this PR fixes). Now a zero-row update for a deleted incident returns ErrVersionConflict → 409 Conflict. The PR description calls this intentional, but semantically a 404 is the correct status for a deleted resource, and 409 will make it harder for clients to distinguish "someone else changed it" from "it no longer exists". Consider a Count/GetIncident check up front (or an ent.IsNotFound-style probe) to return ErrDBIncidentDSNotExist → 404, reserving ErrVersionConflict for genuine version mismatches. If 409 is accepted as-is, document it in the API contract since it's a user-visible behavior change.

Suggestion — test nit

tests/reopen_version_test.go:42: the stale-version check works, but note version - 1 will be 0 when the incident starts at version 0/1 depending on the schema default; using an obviously different stale value (or asserting version first) makes the test robust to the starting version. Non-blocking.

Things checked

  • Atomicity: single UPDATE with version predicate + version bump — no read-then-write window. Correct.
  • Update().Save(ctx) returns (int, error) in Ent; affected == 0 handling is correct.
  • Conflict mapping in reopenIncident reuses the same apiErrors.RaiseConflictErr/409 pattern already established at v2.go:1096-1103.
  • Version increment is appropriate: clearing end_date is a real content change, so bumping the optimistic-lock counter is correct.
  • CI: build, unit tests, Postgres acceptance tests (including the new test), lint, and CodeQL all pass.

@Aloento
Aloento merged commit e567187 into main Dec 1, 2026
13 checks passed
@Aloento
Aloento deleted the fix/issue-118-reopen-version branch December 1, 2026 10:58
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.

[db] Add a version predicate to ReOpenIncident

1 participant