on main help sign in
ci: fix linting
1fa4e8aAndrey Nering
cmd/root.go+5−5
@@ -77,16 +77,16 @@ to assist developers in writing, debugging, and understanding code directly from
77 return err
77 return err
78 }
78 }
79
79
80+ // Create main context for the application
81+ ctx, cancel := context.WithCancel(context.Background())
82+ defer cancel()
83+
80 // Connect DB, this will also run migrations
84 // Connect DB, this will also run migrations
81− conn, err := db.Connect()
85+ conn, err := db.Connect(ctx)
82 if err != nil {
86 if err != nil {
83 return err
87 return err
84 }
88 }
85
89
86− // Create main context for the application
87− ctx, cancel := context.WithCancel(context.Background())
88− defer cancel()
89−
90 app, err := app.New(ctx, conn)
90 app, err := app.New(ctx, conn)
91 if err != nil {
91 if err != nil {
92 logging.Error("Failed to create app: %v", err)
92 logging.Error("Failed to create app: %v", err)
internal/db/connect.go+4−3
@@ -1,6 +1,7 @@
1package db
1package db
2
2
3import (
3import (
4+ "context"
4 "database/sql"
5 "database/sql"
5 "fmt"
6 "fmt"
6 "os"
7 "os"
@@ -15,7 +16,7 @@ import (
15 "github.com/pressly/goose/v3"
16 "github.com/pressly/goose/v3"
16)
17)
17
18
18−func Connect() (*sql.DB, error) {
19+func Connect(ctx context.Context) (*sql.DB, error) {
19 dataDir := config.Get().Data.Directory
20 dataDir := config.Get().Data.Directory
20 if dataDir == "" {
21 if dataDir == "" {
21 return nil, fmt.Errorf("data.dir is not set")
22 return nil, fmt.Errorf("data.dir is not set")
@@ -31,7 +32,7 @@ func Connect() (*sql.DB, error) {
31 }
32 }
32
33
33 // Verify connection
34 // Verify connection
34− if err = db.Ping(); err != nil {
35+ if err = db.PingContext(ctx); err != nil {
35 db.Close()
36 db.Close()
36 return nil, fmt.Errorf("failed to connect to database: %w", err)
37 return nil, fmt.Errorf("failed to connect to database: %w", err)
37 }
38 }
@@ -46,7 +47,7 @@ func Connect() (*sql.DB, error) {
46 }
47 }
47
48
48 for _, pragma := range pragmas {
49 for _, pragma := range pragmas {
49− if _, err = db.Exec(pragma); err != nil {
50+ if _, err = db.ExecContext(ctx, pragma); err != nil {
50 logging.Error("Failed to set pragma", pragma, err)
51 logging.Error("Failed to set pragma", pragma, err)
51 } else {
52 } else {
52 logging.Debug("Set pragma", "pragma", pragma)
53 logging.Debug("Set pragma", "pragma", pragma)
internal/history/file.go+1−1
@@ -83,7 +83,7 @@ func (s *service) createWithVersion(ctx context.Context, sessionID, path, conten
83 // Retry loop for transaction conflicts
83 // Retry loop for transaction conflicts
84 for attempt := range maxRetries {
84 for attempt := range maxRetries {
85 // Start a transaction
85 // Start a transaction
86− tx, txErr := s.db.Begin()
86+ tx, txErr := s.db.BeginTx(ctx, nil)
87 if txErr != nil {
87 if txErr != nil {
88 return File{}, fmt.Errorf("failed to begin transaction: %w", txErr)
88 return File{}, fmt.Errorf("failed to begin transaction: %w", txErr)
89 }
89 }