From 5a5290c174d0c85b768d1d93f0e38935dfac3981 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Tue, 10 Nov 2026 05:43:57 +0000 Subject: [PATCH] db: validate the sqlite schema on one connection squibble nests queries; a second connection's auto_vacuum pragma writes, and without WAL that write waits on the first one's read until busy_timeout. Updates #1926 --- hscontrol/db/db.go | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/hscontrol/db/db.go b/hscontrol/db/db.go index ee3e91ff9..8f81774b9 100644 --- a/hscontrol/db/db.go +++ b/hscontrol/db/db.go @@ -33,11 +33,7 @@ var errDatabaseNotSupported = errors.New("database type not supported") var errForeignKeyConstraintsViolated = errors.New("foreign key constraints violated") -const ( - maxIdleConns = 100 - maxOpenConns = 100 - contextTimeout = 10 * time.Second -) +const contextTimeout = 10 * time.Second type HSDatabase struct { DB *gorm.DB @@ -463,16 +459,18 @@ WHERE auth_key_id IN ( return nil, fmt.Errorf("getting DB from gorm: %w", err) } - // or else it blocks... - sqlConn.SetMaxIdleConns(maxIdleConns) - - sqlConn.SetMaxOpenConns(maxOpenConns) - defer sqlConn.SetMaxIdleConns(1) - defer sqlConn.SetMaxOpenConns(1) - ctx, cancel := context.WithTimeout(context.Background(), contextTimeout) defer cancel() + // squibble nests queries. Without WAL a second connection would + // hang: its opening pragmas write, and the write waits for the + // first connection's read. + conn, err := sqlConn.Conn(ctx) + if err != nil { + return nil, fmt.Errorf("getting connection for schema validation: %w", err) + } + defer conn.Close() + opts := squibble.DigestOptions{ IgnoreTables: []string{ // Litestream tables, these are inserted by @@ -483,7 +481,7 @@ WHERE auth_key_id IN ( }, } - if err := squibble.Validate(ctx, sqlConn, dbSchema, &opts); err != nil { //nolint:noinlineerr + if err := squibble.Validate(ctx, conn, dbSchema, &opts); err != nil { //nolint:noinlineerr return nil, fmt.Errorf("validating schema: %w", err) } }