parent
40bfa91018
commit
ebf1693a7c
@ -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
|
||||
}
|
||||
|
||||
|
@ -6,6 +6,8 @@ package sqlc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
const pragmaIntegrityCheck = `PRAGMA integrity_check`
|
||||
@ -100,3 +102,9 @@ func (q *Queries) PragmaForeignKeyCheck(ctx context.Context) ([]PragmaForeignKey
|
||||
}
|
||||
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
|
||||
}
|
||||
|
@ -2,12 +2,11 @@
|
||||
package persistence
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -40,9 +39,11 @@ func isDatabaseBusyError(err error) bool {
|
||||
|
||||
// setBusyTimeout sets the SQLite busy_timeout busy handler.
|
||||
// See https://sqlite.org/pragma.html#pragma_busy_timeout
|
||||
func setBusyTimeout(gormDB *gorm.DB, busyTimeout time.Duration) error {
|
||||
if tx := gormDB.Exec(fmt.Sprintf("PRAGMA busy_timeout = %d", busyTimeout.Milliseconds())); tx.Error != nil {
|
||||
return fmt.Errorf("setting busy_timeout: %w", tx.Error)
|
||||
func (db *DB) setBusyTimeout(ctx context.Context, busyTimeout time.Duration) error {
|
||||
queries := db.queries()
|
||||
err := queries.PragmaBusyTimeout(ctx, busyTimeout)
|
||||
if err != nil {
|
||||
return fmt.Errorf("setting busy_timeout: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user