- Fix .golangci.yml for v2 format (linters-settings -> linters.settings) - All production code now passes golangci-lint with zero issues - Line length 88, funlen 80/50, cyclop 15, dupl 100 - Extract shared helpers in db (scanChannels, scanInt64s, scanMessages) - Split runMigrations into applyMigration/execMigration - Fix fanOut return signature (remove unused int64) - Add fanOutSilent helper to avoid dogsled - Rewrite CLI code for lint compliance (nlreturn, wsl_v5, noctx, etc) - Rename CLI api package to chatapi to avoid revive var-naming - Fix all noinlineerr, mnd, perfsprint, funcorder issues - Fix db tests: extract helpers, add t.Parallel, proper error checks - Broker tests already clean - Handler integration tests still have lint issues (next commit)
48 lines
826 B
Go
48 lines
826 B
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"log/slog"
|
|
"sync/atomic"
|
|
)
|
|
|
|
//nolint:gochecknoglobals // test counter
|
|
var testDBCounter atomic.Int64
|
|
|
|
// NewTestDatabase creates an in-memory database for testing.
|
|
func NewTestDatabase() (*Database, error) {
|
|
n := testDBCounter.Add(1)
|
|
|
|
dsn := fmt.Sprintf(
|
|
"file:testdb%d?mode=memory"+
|
|
"&cache=shared&_pragma=foreign_keys(1)",
|
|
n,
|
|
)
|
|
|
|
d, err := sql.Open("sqlite", dsn)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
database := &Database{db: d, log: slog.Default()}
|
|
|
|
err = database.runMigrations(context.Background())
|
|
if err != nil {
|
|
closeErr := d.Close()
|
|
if closeErr != nil {
|
|
return nil, closeErr
|
|
}
|
|
|
|
return nil, err
|
|
}
|
|
|
|
return database, nil
|
|
}
|
|
|
|
// Close closes the underlying database connection.
|
|
func (s *Database) Close() error {
|
|
return s.db.Close()
|
|
}
|