fix: rebase onto main, fix SQLite concurrency, lint clean
All checks were successful
check / check (push) Successful in 2m11s

- Add busy_timeout PRAGMA and MaxOpenConns(1) for SQLite stability
- Use per-test temp DB in handler tests to prevent state leaks
- Pre-allocate migrations slice (prealloc lint)
- Remove invalid linter names (wsl_v5, noinlineerr) from .golangci.yml
- Remove unused //nolint:gosec directives
- Replace context.Background() with t.Context() in tests
- Use goimports formatting for all files
- All make check passes with zero failures
This commit is contained in:
clawbot
2026-02-26 20:25:46 -08:00
parent 6043e9b879
commit 69e1042e6e
8 changed files with 88 additions and 48 deletions

View File

@@ -81,7 +81,7 @@ func (s *Database) GetDB() *sql.DB {
func (s *Database) connect(ctx context.Context) error {
dbURL := s.params.Config.DBURL
if dbURL == "" {
dbURL = "file:./data.db?_journal_mode=WAL"
dbURL = "file:./data.db?_journal_mode=WAL&_busy_timeout=5000"
}
s.log.Info("connecting to database", "url", dbURL)
@@ -104,6 +104,8 @@ func (s *Database) connect(ctx context.Context) error {
return err
}
d.SetMaxOpenConns(1)
s.db = d
s.log.Info("database connected")
@@ -114,6 +116,13 @@ func (s *Database) connect(ctx context.Context) error {
return fmt.Errorf("enable foreign keys: %w", err)
}
_, err = s.db.ExecContext(
ctx, "PRAGMA busy_timeout = 5000",
)
if err != nil {
return fmt.Errorf("set busy timeout: %w", err)
}
return s.runMigrations(ctx)
}
@@ -233,7 +242,7 @@ func (s *Database) loadMigrations() (
)
}
var migrations []migration
migrations := make([]migration, 0, len(entries))
for _, entry := range entries {
if entry.IsDir() ||

View File

@@ -1,7 +1,6 @@
package db_test
import (
"context"
"encoding/json"
"testing"
@@ -32,7 +31,7 @@ func TestCreateUser(t *testing.T) {
t.Parallel()
database := setupTestDB(t)
ctx := context.Background()
ctx := t.Context()
id, token, err := database.CreateUser(ctx, "alice")
if err != nil {
@@ -53,7 +52,7 @@ func TestGetUserByToken(t *testing.T) {
t.Parallel()
database := setupTestDB(t)
ctx := context.Background()
ctx := t.Context()
_, token, err := database.CreateUser(ctx, "bob")
if err != nil {
@@ -79,7 +78,7 @@ func TestGetUserByNick(t *testing.T) {
t.Parallel()
database := setupTestDB(t)
ctx := context.Background()
ctx := t.Context()
_, _, err := database.CreateUser(ctx, "charlie")
if err != nil {
@@ -101,7 +100,7 @@ func TestChannelOperations(t *testing.T) {
t.Parallel()
database := setupTestDB(t)
ctx := context.Background()
ctx := t.Context()
chID, err := database.GetOrCreateChannel(ctx, "#test")
if err != nil || chID == 0 {
@@ -128,7 +127,7 @@ func TestJoinAndPart(t *testing.T) {
t.Parallel()
database := setupTestDB(t)
ctx := context.Background()
ctx := t.Context()
uid, _, err := database.CreateUser(ctx, "user1")
if err != nil {
@@ -170,7 +169,7 @@ func TestDeleteChannelIfEmpty(t *testing.T) {
t.Parallel()
database := setupTestDB(t)
ctx := context.Background()
ctx := t.Context()
chID, err := database.GetOrCreateChannel(
ctx, "#empty",
@@ -212,7 +211,7 @@ func createUserWithChannels(
) (int64, int64, int64) {
t.Helper()
ctx := context.Background()
ctx := t.Context()
uid, _, err := database.CreateUser(ctx, nick)
if err != nil {
@@ -255,7 +254,7 @@ func TestListChannels(t *testing.T) {
)
channels, err := database.ListChannels(
context.Background(), uid,
t.Context(), uid,
)
if err != nil || len(channels) != 2 {
t.Fatalf(
@@ -269,7 +268,7 @@ func TestListAllChannels(t *testing.T) {
t.Parallel()
database := setupTestDB(t)
ctx := context.Background()
ctx := t.Context()
_, err := database.GetOrCreateChannel(ctx, "#x")
if err != nil {
@@ -294,7 +293,7 @@ func TestChangeNick(t *testing.T) {
t.Parallel()
database := setupTestDB(t)
ctx := context.Background()
ctx := t.Context()
uid, token, err := database.CreateUser(ctx, "old")
if err != nil {
@@ -320,7 +319,7 @@ func TestSetTopic(t *testing.T) {
t.Parallel()
database := setupTestDB(t)
ctx := context.Background()
ctx := t.Context()
_, err := database.GetOrCreateChannel(
ctx, "#topictest",
@@ -354,7 +353,7 @@ func TestInsertAndPollMessages(t *testing.T) {
t.Parallel()
database := setupTestDB(t)
ctx := context.Background()
ctx := t.Context()
uid, _, err := database.CreateUser(ctx, "poller")
if err != nil {
@@ -415,7 +414,7 @@ func TestGetHistory(t *testing.T) {
t.Parallel()
database := setupTestDB(t)
ctx := context.Background()
ctx := t.Context()
const msgCount = 10
@@ -452,7 +451,7 @@ func TestDeleteUser(t *testing.T) {
t.Parallel()
database := setupTestDB(t)
ctx := context.Background()
ctx := t.Context()
uid, _, err := database.CreateUser(ctx, "deleteme")
if err != nil {
@@ -491,7 +490,7 @@ func TestChannelMembers(t *testing.T) {
t.Parallel()
database := setupTestDB(t)
ctx := context.Background()
ctx := t.Context()
uid1, _, err := database.CreateUser(ctx, "m1")
if err != nil {
@@ -539,7 +538,7 @@ func TestGetAllChannelMembershipsForUser(t *testing.T) {
channels, err :=
database.GetAllChannelMembershipsForUser(
context.Background(), uid,
t.Context(), uid,
)
if err != nil || len(channels) != 2 {
t.Fatalf(