Manager: convert busy-timeout query to sqlc

Ref: #104305
This commit is contained in:
Sybren A. Stüvel 2024-09-18 16:49:01 +02:00
parent 40bfa91018
commit ebf1693a7c
3 changed files with 15 additions and 6 deletions

View File

@ -54,7 +54,7 @@ func OpenDB(ctx context.Context, dsn string) (*DB, error) {
} }
}() }()
if err := setBusyTimeout(db.gormDB, 5*time.Second); err != nil { if err := db.setBusyTimeout(ctx, 5*time.Second); err != nil {
return nil, err return nil, err
} }

View File

@ -6,6 +6,8 @@ package sqlc
import ( import (
"context" "context"
"fmt"
"time"
) )
const pragmaIntegrityCheck = `PRAGMA integrity_check` const pragmaIntegrityCheck = `PRAGMA integrity_check`
@ -100,3 +102,9 @@ func (q *Queries) PragmaForeignKeyCheck(ctx context.Context) ([]PragmaForeignKey
} }
return items, nil return items, nil
} }
func (q *Queries) PragmaBusyTimeout(ctx context.Context, busyTimeout time.Duration) error {
sql := fmt.Sprintf("PRAGMA busy_timeout = %d", busyTimeout.Milliseconds())
_, err := q.db.ExecContext(ctx, sql)
return err
}

View File

@ -2,12 +2,11 @@
package persistence package persistence
import ( import (
"context"
"errors" "errors"
"fmt" "fmt"
"strings" "strings"
"time" "time"
"gorm.io/gorm"
) )
var ( var (
@ -40,9 +39,11 @@ func isDatabaseBusyError(err error) bool {
// setBusyTimeout sets the SQLite busy_timeout busy handler. // setBusyTimeout sets the SQLite busy_timeout busy handler.
// See https://sqlite.org/pragma.html#pragma_busy_timeout // See https://sqlite.org/pragma.html#pragma_busy_timeout
func setBusyTimeout(gormDB *gorm.DB, busyTimeout time.Duration) error { func (db *DB) setBusyTimeout(ctx context.Context, busyTimeout time.Duration) error {
if tx := gormDB.Exec(fmt.Sprintf("PRAGMA busy_timeout = %d", busyTimeout.Milliseconds())); tx.Error != nil { queries := db.queries()
return fmt.Errorf("setting busy_timeout: %w", tx.Error) err := queries.PragmaBusyTimeout(ctx, busyTimeout)
if err != nil {
return fmt.Errorf("setting busy_timeout: %w", err)
} }
return nil return nil
} }