feat(gonic): add log-db option to log queries

related #620
This commit is contained in:
sentriz
2025-10-24 17:05:46 +01:00
parent ea5e6252a1
commit 75a0918a7e
2 changed files with 7 additions and 3 deletions

No files matched your search

+2 -1
View File
@@ -88,6 +88,7 @@ func main() {
confPprof := flag.Bool("pprof", false, "enable the /debug/pprof endpoint (optional)") confPprof := flag.Bool("pprof", false, "enable the /debug/pprof endpoint (optional)")
confExpvar := flag.Bool("expvar", false, "enable the /debug/vars endpoint (optional)") confExpvar := flag.Bool("expvar", false, "enable the /debug/vars endpoint (optional)")
confLogDB := flag.Bool("log-db", false, "enable database query logging (optional)")
deprecatedConfGenreSplit := flag.String("genre-split", "", "(deprecated, see multi-value settings)") deprecatedConfGenreSplit := flag.String("genre-split", "", "(deprecated, see multi-value settings)")
@@ -137,7 +138,7 @@ func main() {
log.Fatalf("couldn't create covers cache path: %v\n", err) log.Fatalf("couldn't create covers cache path: %v\n", err)
} }
dbc, err := db.New(*confDBPath, db.DefaultOptions()) dbc, err := db.New(*confDBPath, db.DefaultOptions(), *confLogDB)
if err != nil { if err != nil {
log.Fatalf("error opening database: %v\n", err) log.Fatalf("error opening database: %v\n", err)
} }
+5 -2
View File
@@ -40,7 +40,7 @@ type DB struct {
*gorm.DB *gorm.DB
} }
func New(path string, options url.Values) (*DB, error) { func New(path string, options url.Values, logQueries bool) (*DB, error) {
url := url.URL{ url := url.URL{
Scheme: "file", Scheme: "file",
Opaque: path, Opaque: path,
@@ -51,12 +51,15 @@ func New(path string, options url.Values) (*DB, error) {
return nil, fmt.Errorf("with gorm: %w", err) return nil, fmt.Errorf("with gorm: %w", err)
} }
db.SetLogger(log.New(os.Stdout, "gorm ", 0)) db.SetLogger(log.New(os.Stdout, "gorm ", 0))
if logQueries {
db.LogMode(true)
}
db.DB().SetMaxOpenConns(1) db.DB().SetMaxOpenConns(1)
return &DB{DB: db}, nil return &DB{DB: db}, nil
} }
func NewMock() (*DB, error) { func NewMock() (*DB, error) {
return New(":memory:", mockOptions()) return New(":memory:", mockOptions(), false)
} }
func (db *DB) InsertBulkLeftMany(table string, head []string, left int, col []int) error { func (db *DB) InsertBulkLeftMany(table string, head []string, left int, col []int) error {