Skip to content

Commit

Permalink
test: switch to WAL journal mode in unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Jian Zeng <[email protected]>
  • Loading branch information
knight42 committed Jan 7, 2024
1 parent 6a4f9e5 commit 04b5236
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 11 deletions.
4 changes: 1 addition & 3 deletions pkg/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,8 @@ func New(configPath string) (*Server, error) {
}

func NewWithConfig(cfg Config) (*Server, error) {
// TODO: enforce shared cache mode?
db, err := gorm.Open(sqlite.Open(cfg.DbURL), &gorm.Config{
QueryFields: true,
SkipDefaultTransaction: true,
QueryFields: true,
})
if err != nil {
return nil, fmt.Errorf("open db: %w", err)
Expand Down
11 changes: 3 additions & 8 deletions pkg/server/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,11 @@ func NewTestEnv(t *testing.T) *TestEnv {
_ = dbFile.Close()
_ = os.Remove(dbFile.Name())
})
db, err := gorm.Open(sqlite.Open(dbFile.Name()), &gorm.Config{
QueryFields: true,
SkipDefaultTransaction: true,
// Switch to WAL mode to avoid "database is locked" error.
db, err := gorm.Open(sqlite.Open(dbFile.Name()+"?_journal_mode=WAL"), &gorm.Config{
QueryFields: true,
})
require.NoError(t, err)
sqlDB, err := db.DB()
require.NoError(t, err)
// To resolve the "database is locked" error.
// See also https://github.com/mattn/go-sqlite3/issues/209
sqlDB.SetMaxOpenConns(1)
require.NoError(t, model.AutoMigrate(db))

s := &Server{
Expand Down

0 comments on commit 04b5236

Please sign in to comment.