DUP-GO-7: queryWithDir and queryNoDir are near-identical SQL queries #41

Closed
opened 2026-05-27 16:56:55 +02:00 by dries · 0 comments
Owner

Summary

In internal/db/sessions.go (lines 442–520), queryWithDir and queryNoDir are two 38-line SQL strings that differ only in one WHERE clause condition and the argument list.

Duplicated code

The entire SELECT body — the agent coalesce, the model three-way CASE, the JOIN message, the WHERE json_extract, the ORDER BY, and LIMIT 1 — is copy-pasted verbatim between the two queries (76 lines total).

Note

The author commented that two queries were chosen for "query plan reviewability" — a conscious trade-off.

Fix

Unify into a single dynamically-built query using the same directoryWhere pattern used elsewhere in stats.go.

Effort

Medium — deliberate choice, needs discussion before changing.

## Summary In `internal/db/sessions.go` (lines 442–520), `queryWithDir` and `queryNoDir` are two 38-line SQL strings that differ only in one `WHERE` clause condition and the argument list. ## Duplicated code The entire `SELECT` body — the `agent` coalesce, the `model` three-way CASE, the `JOIN message`, the `WHERE json_extract`, the `ORDER BY`, and `LIMIT 1` — is copy-pasted verbatim between the two queries (76 lines total). ## Note The author commented that two queries were chosen for "query plan reviewability" — a conscious trade-off. ## Fix Unify into a single dynamically-built query using the same `directoryWhere` pattern used elsewhere in `stats.go`. ## Effort Medium — deliberate choice, needs discussion before changing.
dries closed this issue 2026-07-11 11:23:17 +02:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
dries/ocman#41
No description provided.