fix(database): address Copilot review comments

- Remove _synchronous=NORMAL from DSN to preserve SQLite's default
  durability guarantees; WAL mode alone is sufficient for the fix
- Add error handling for db.Exec and db.DB() in openTestDB helper
- Use context.WithTimeout(5s) in with_fix sub-test to prevent hang
  on connection-never-released regression

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
カン 2026-04-28 10:57:53 +09:00 committed by 康厚超
parent de4d40a25c
commit 314f877d78
2 changed files with 11 additions and 5 deletions

View file

@ -89,9 +89,14 @@ func TestConcurrentWrites(t *testing.T) {
if err != nil {
t.Fatalf("gorm.Open: %v", err)
}
db.Exec("CREATE TABLE IF NOT EXISTS settings " +
"(id INTEGER PRIMARY KEY AUTOINCREMENT, key TEXT, value TEXT)")
sqlDB, _ := db.DB()
if err := db.Exec("CREATE TABLE IF NOT EXISTS settings " +
"(id INTEGER PRIMARY KEY AUTOINCREMENT, key TEXT, value TEXT)").Error; err != nil {
t.Fatalf("create settings table: %v", err)
}
sqlDB, err := db.DB()
if err != nil {
t.Fatalf("db.DB: %v", err)
}
sqlDB.SetMaxOpenConns(maxConns)
t.Cleanup(func() { sqlDB.Close() })
return sqlDB
@ -147,7 +152,8 @@ func TestConcurrentWrites(t *testing.T) {
// writes successfully, regardless of busy_timeout.
t.Run("with_fix_pool_serialises_writes", func(t *testing.T) {
sqlDB := openTestDB(t, 1)
ctx := context.Background()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
conn1, err := sqlDB.Conn(ctx)
if err != nil {

View file

@ -139,7 +139,7 @@ func InitDB(dbPath string) error {
c := &gorm.Config{
Logger: gormLogger,
}
db, err = gorm.Open(sqlite.Open(dbPath+"?_journal_mode=WAL&_synchronous=NORMAL"), c)
db, err = gorm.Open(sqlite.Open(dbPath+"?_journal_mode=WAL"), c)
if err != nil {
return err
}