refactor(db): thread context.Context through all Store methods

Enables request-scoped cancellation, timeouts, and graceful shutdown
for all database operations across API handlers, CLI commands, and TUI.
This commit is contained in:
2026-05-20 20:51:51 -04:00
parent 50b80f4407
commit d715b053e7
18 changed files with 267 additions and 228 deletions
+15 -15
View File
@@ -109,13 +109,13 @@ func listEntities(store *db.Store) http.HandlerFunc {
p.Limit = 50
}
total, err := store.Count(p)
total, err := store.Count(r.Context(), p)
if err != nil {
writeInternalError(w, err)
return
}
entities, err := store.List(p)
entities, err := store.List(r.Context(), p)
if err != nil {
writeInternalError(w, err)
return
@@ -177,7 +177,7 @@ func createEntity(store *db.Store) http.HandlerFunc {
e.CardData = req.CardData
}
if err := store.Create(e); err != nil {
if err := store.Create(r.Context(), e); err != nil {
if err == db.ErrInvalidCardData {
writeError(w, http.StatusBadRequest, "invalid_card_data", "card_data must be valid JSON")
return
@@ -193,7 +193,7 @@ func createEntity(store *db.Store) http.HandlerFunc {
func getEntity(store *db.Store) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
e, err := store.Get(id)
e, err := store.Get(r.Context(), id)
if err != nil {
if err == db.ErrNotFound {
writeError(w, http.StatusNotFound, "not_found", "no entity with id "+id)
@@ -243,7 +243,7 @@ func updateEntity(store *db.Store) http.HandlerFunc {
u.CardType = &ct
}
if err := store.Update(id, u); err != nil {
if err := store.Update(r.Context(), id, u); err != nil {
if err == db.ErrNotFound {
writeError(w, http.StatusNotFound, "not_found", "no entity with id "+id)
return
@@ -256,7 +256,7 @@ func updateEntity(store *db.Store) http.HandlerFunc {
return
}
e, err := store.Get(id)
e, err := store.Get(r.Context(), id)
if err != nil {
writeInternalError(w, err)
return
@@ -272,7 +272,7 @@ type DeleteResponse struct {
func deleteEntity(store *db.Store) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
result, err := store.SoftDelete(id)
result, err := store.SoftDelete(r.Context(), id)
if err != nil {
if err == db.ErrNotFound {
writeError(w, http.StatusNotFound, "not_found", "no entity with id "+id)
@@ -307,7 +307,7 @@ func promoteEntity(store *db.Store) http.HandlerFunc {
return
}
if err := store.Promote(id, db.CardType(req.CardType), req.CardData); err != nil {
if err := store.Promote(r.Context(), id, db.CardType(req.CardType), req.CardData); err != nil {
if err == db.ErrNotFound {
writeError(w, http.StatusNotFound, "not_found", "no entity with id "+id)
return
@@ -324,7 +324,7 @@ func promoteEntity(store *db.Store) http.HandlerFunc {
return
}
e, err := store.Get(id)
e, err := store.Get(r.Context(), id)
if err != nil {
writeInternalError(w, err)
return
@@ -337,7 +337,7 @@ func demoteEntity(store *db.Store) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
if err := store.Demote(id); err != nil {
if err := store.Demote(r.Context(), id); err != nil {
if err == db.ErrNotFound {
writeError(w, http.StatusNotFound, "not_found", "no entity with id "+id)
return
@@ -350,7 +350,7 @@ func demoteEntity(store *db.Store) http.HandlerFunc {
return
}
e, err := store.Get(id)
e, err := store.Get(r.Context(), id)
if err != nil {
writeInternalError(w, err)
return
@@ -381,7 +381,7 @@ func absorbEntity(store *db.Store) http.HandlerFunc {
return
}
if err := store.Absorb(id, req.SourceID); err != nil {
if err := store.Absorb(r.Context(), id, req.SourceID); err != nil {
if err == db.ErrNotFound {
writeError(w, http.StatusNotFound, "not_found", "target or source entity not found")
return
@@ -394,7 +394,7 @@ func absorbEntity(store *db.Store) http.HandlerFunc {
return
}
e, err := store.Get(id)
e, err := store.Get(r.Context(), id)
if err != nil {
writeInternalError(w, err)
return
@@ -407,7 +407,7 @@ func useEntity(store *db.Store) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
if err := store.IncrementUse(id); err != nil {
if err := store.IncrementUse(r.Context(), id); err != nil {
if err == db.ErrNotFound {
writeError(w, http.StatusNotFound, "not_found", "no entity with id "+id)
return
@@ -416,7 +416,7 @@ func useEntity(store *db.Store) http.HandlerFunc {
return
}
e, err := store.Get(id)
e, err := store.Get(r.Context(), id)
if err != nil {
writeInternalError(w, err)
return
+1 -1
View File
@@ -14,7 +14,7 @@ type TagResponse struct {
func listTags(store *db.Store) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
cardsOnly := r.URL.Query().Get("cards_only") == "true"
tags, err := store.ListTags(cardsOnly)
tags, err := store.ListTags(r.Context(), cardsOnly)
if err != nil {
writeInternalError(w, err)
return