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:
+4
-4
@@ -19,19 +19,19 @@ func init() {
|
||||
rootCmd.AddCommand(absorbCmd)
|
||||
}
|
||||
|
||||
func runAbsorb(_ *cobra.Command, args []string) error {
|
||||
func runAbsorb(cmd *cobra.Command, args []string) error {
|
||||
store, err := openStore()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer store.Close()
|
||||
|
||||
targetID, err := store.Resolve(args[0])
|
||||
targetID, err := store.Resolve(cmd.Context(), args[0])
|
||||
if err != nil {
|
||||
return fmt.Errorf("not_found — no entity with id %s", args[0])
|
||||
}
|
||||
|
||||
sourceID, err := store.Resolve(args[1])
|
||||
sourceID, err := store.Resolve(cmd.Context(), args[1])
|
||||
if err != nil {
|
||||
return fmt.Errorf("not_found — no entity with id %s", args[1])
|
||||
}
|
||||
@@ -40,7 +40,7 @@ func runAbsorb(_ *cobra.Command, args []string) error {
|
||||
return fmt.Errorf("target and source must be different entities")
|
||||
}
|
||||
|
||||
if err := store.Absorb(targetID, sourceID); err != nil {
|
||||
if err := store.Absorb(cmd.Context(), targetID, sourceID); err != nil {
|
||||
if err == db.ErrTargetCrystallized {
|
||||
return fmt.Errorf("invalid_absorb — target %s is crystallized, demote first",
|
||||
display.FormatID(targetID))
|
||||
|
||||
+2
-2
@@ -17,7 +17,7 @@ var addCmd = &cobra.Command{
|
||||
RunE: runAdd,
|
||||
}
|
||||
|
||||
func runAdd(_ *cobra.Command, args []string) error {
|
||||
func runAdd(cmd *cobra.Command, args []string) error {
|
||||
input := strings.Join(args, " ")
|
||||
|
||||
parsed, err := parse.Parse(input)
|
||||
@@ -47,7 +47,7 @@ func runAdd(_ *cobra.Command, args []string) error {
|
||||
e.CardType = &ct
|
||||
}
|
||||
|
||||
if err := store.Create(e); err != nil {
|
||||
if err := store.Create(cmd.Context(), e); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -26,7 +26,7 @@ func init() {
|
||||
rootCmd.AddCommand(cardsCmd)
|
||||
}
|
||||
|
||||
func runCards(_ *cobra.Command, _ []string) error {
|
||||
func runCards(cmd *cobra.Command, _ []string) error {
|
||||
store, err := openStore()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -49,7 +49,7 @@ func runCards(_ *cobra.Command, _ []string) error {
|
||||
p.CardTypeFilter = &ct
|
||||
}
|
||||
|
||||
entities, err := store.List(p)
|
||||
entities, err := store.List(cmd.Context(), p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
+4
-4
@@ -19,19 +19,19 @@ func init() {
|
||||
rootCmd.AddCommand(copyCmd)
|
||||
}
|
||||
|
||||
func runCopy(_ *cobra.Command, args []string) error {
|
||||
func runCopy(cmd *cobra.Command, args []string) error {
|
||||
store, err := openStore()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer store.Close()
|
||||
|
||||
id, err := store.Resolve(args[0])
|
||||
id, err := store.Resolve(cmd.Context(), args[0])
|
||||
if err != nil {
|
||||
return fmt.Errorf("not_found — no entity with id %s", args[0])
|
||||
}
|
||||
|
||||
e, err := store.Get(id)
|
||||
e, err := store.Get(cmd.Context(), id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -40,7 +40,7 @@ func runCopy(_ *cobra.Command, args []string) error {
|
||||
return fmt.Errorf("clipboard: %w", err)
|
||||
}
|
||||
|
||||
if err := store.IncrementUse(id); err != nil {
|
||||
if err := store.IncrementUse(cmd.Context(), id); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
+3
-3
@@ -19,19 +19,19 @@ func init() {
|
||||
rootCmd.AddCommand(deleteCmd)
|
||||
}
|
||||
|
||||
func runDelete(_ *cobra.Command, args []string) error {
|
||||
func runDelete(cmd *cobra.Command, args []string) error {
|
||||
store, err := openStore()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer store.Close()
|
||||
|
||||
id, err := store.Resolve(args[0])
|
||||
id, err := store.Resolve(cmd.Context(), args[0])
|
||||
if err != nil {
|
||||
return fmt.Errorf("not_found — no entity with id %s", args[0])
|
||||
}
|
||||
|
||||
result, err := store.SoftDelete(id)
|
||||
result, err := store.SoftDelete(cmd.Context(), id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
+7
-6
@@ -1,6 +1,7 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
@@ -35,7 +36,7 @@ type demoEntity struct {
|
||||
Tags []string `json:"tags"`
|
||||
}
|
||||
|
||||
func runDemo(_ *cobra.Command, _ []string) error {
|
||||
func runDemo(cmd *cobra.Command, _ []string) error {
|
||||
tmpDir, err := os.MkdirTemp("", "nib-demo-*")
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -48,7 +49,7 @@ func runDemo(_ *cobra.Command, _ []string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := seedDemo(store); err != nil {
|
||||
if err := seedDemo(cmd.Context(), store); err != nil {
|
||||
store.Close()
|
||||
return fmt.Errorf("seed demo data: %w", err)
|
||||
}
|
||||
@@ -58,7 +59,7 @@ func runDemo(_ *cobra.Command, _ []string) error {
|
||||
return runServe(nil, nil)
|
||||
}
|
||||
|
||||
func seedDemo(store *db.Store) error {
|
||||
func seedDemo(ctx context.Context, store *db.Store) error {
|
||||
data, err := findDemoFile()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -94,19 +95,19 @@ func seedDemo(store *db.Store) error {
|
||||
e.CompletedAt = &t
|
||||
}
|
||||
|
||||
if err := store.Create(e); err != nil {
|
||||
if err := store.Create(ctx, e); err != nil {
|
||||
return fmt.Errorf("entity %d: %w", i, err)
|
||||
}
|
||||
|
||||
if entry.CardType != nil {
|
||||
ct := db.CardType(*entry.CardType)
|
||||
if err := store.Promote(e.ID, ct, entry.CardData); err != nil {
|
||||
if err := store.Promote(ctx, e.ID, ct, entry.CardData); err != nil {
|
||||
return fmt.Errorf("promote entity %d: %w", i, err)
|
||||
}
|
||||
}
|
||||
|
||||
if entry.Deleted {
|
||||
store.SoftDelete(e.ID)
|
||||
store.SoftDelete(ctx, e.ID)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+3
-3
@@ -19,19 +19,19 @@ func init() {
|
||||
rootCmd.AddCommand(demoteCmd)
|
||||
}
|
||||
|
||||
func runDemote(_ *cobra.Command, args []string) error {
|
||||
func runDemote(cmd *cobra.Command, args []string) error {
|
||||
store, err := openStore()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer store.Close()
|
||||
|
||||
id, err := store.Resolve(args[0])
|
||||
id, err := store.Resolve(cmd.Context(), args[0])
|
||||
if err != nil {
|
||||
return fmt.Errorf("not_found — no entity with id %s", args[0])
|
||||
}
|
||||
|
||||
if err := store.Demote(id); err != nil {
|
||||
if err := store.Demote(cmd.Context(), id); err != nil {
|
||||
if err == db.ErrAlreadyFluid {
|
||||
return fmt.Errorf("invalid_demote — entity %s is already fluid", display.FormatID(id))
|
||||
}
|
||||
|
||||
+9
-9
@@ -21,19 +21,19 @@ func init() {
|
||||
rootCmd.AddCommand(editCmd)
|
||||
}
|
||||
|
||||
func runEdit(_ *cobra.Command, args []string) error {
|
||||
func runEdit(cmd *cobra.Command, args []string) error {
|
||||
store, err := openStore()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer store.Close()
|
||||
|
||||
id, err := store.Resolve(args[0])
|
||||
id, err := store.Resolve(cmd.Context(), args[0])
|
||||
if err != nil {
|
||||
return fmt.Errorf("not_found — no entity with id %s", args[0])
|
||||
}
|
||||
|
||||
e, err := store.Get(id)
|
||||
e, err := store.Get(cmd.Context(), id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -55,11 +55,11 @@ func runEdit(_ *cobra.Command, args []string) error {
|
||||
editor = "vi"
|
||||
}
|
||||
|
||||
cmd := exec.Command(editor, tmpfile.Name())
|
||||
cmd.Stdin = os.Stdin
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
if err := cmd.Run(); err != nil {
|
||||
editorCmd := exec.Command(editor, tmpfile.Name())
|
||||
editorCmd.Stdin = os.Stdin
|
||||
editorCmd.Stdout = os.Stdout
|
||||
editorCmd.Stderr = os.Stderr
|
||||
if err := editorCmd.Run(); err != nil {
|
||||
return fmt.Errorf("editor: %w", err)
|
||||
}
|
||||
|
||||
@@ -74,7 +74,7 @@ func runEdit(_ *cobra.Command, args []string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := store.Update(id, &db.EntityUpdate{Body: &body}); err != nil {
|
||||
if err := store.Update(cmd.Context(), id, &db.EntityUpdate{Body: &body}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ func init() {
|
||||
lsCmd.Flags().BoolVar(&lsAll, "all", false, "include deleted entities")
|
||||
}
|
||||
|
||||
func runLs(_ *cobra.Command, _ []string) error {
|
||||
func runLs(cmd *cobra.Command, _ []string) error {
|
||||
store, err := openStore()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -88,7 +88,7 @@ func runLs(_ *cobra.Command, _ []string) error {
|
||||
p.Since = &since
|
||||
}
|
||||
|
||||
entities, err := store.List(p)
|
||||
entities, err := store.List(cmd.Context(), p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
+4
-4
@@ -20,14 +20,14 @@ func init() {
|
||||
rootCmd.AddCommand(promoteCmd)
|
||||
}
|
||||
|
||||
func runPromote(_ *cobra.Command, args []string) error {
|
||||
func runPromote(cmd *cobra.Command, args []string) error {
|
||||
store, err := openStore()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer store.Close()
|
||||
|
||||
id, err := store.Resolve(args[0])
|
||||
id, err := store.Resolve(cmd.Context(), args[0])
|
||||
if err != nil {
|
||||
return fmt.Errorf("not_found — no entity with id %s", args[0])
|
||||
}
|
||||
@@ -40,14 +40,14 @@ func runPromote(_ *cobra.Command, args []string) error {
|
||||
cardType = db.CardType(args[1])
|
||||
}
|
||||
|
||||
e, err := store.Get(id)
|
||||
e, err := store.Get(cmd.Context(), id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cd := carddata.GenerateCardData(cardType, e.Body)
|
||||
|
||||
if err := store.Promote(id, cardType, cd); err != nil {
|
||||
if err := store.Promote(cmd.Context(), id, cardType, cd); err != nil {
|
||||
if err == db.ErrAlreadyPromoted {
|
||||
return fmt.Errorf("invalid_promote — entity %s is already a %s",
|
||||
display.FormatID(id), *e.CardType)
|
||||
|
||||
Reference in New Issue
Block a user