Skip to content

Commit

Permalink
fix: use shared cache for sqlite in-memory mode (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
agaffney committed Sep 17, 2024
1 parent d8ecccf commit 76101a3
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
2 changes: 1 addition & 1 deletion database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ type InMemoryDatabase struct {
func NewInMemory(logger *slog.Logger) (*InMemoryDatabase, error) {
// Open sqlite DB
metadataDb, err := gorm.Open(
sqlite.Open(":memory:"),
sqlite.Open("file::memory:?cache=shared"),
&gorm.Config{
Logger: gormlogger.Discard,
},
Expand Down
62 changes: 62 additions & 0 deletions database/database_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2024 Blink Labs Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package database_test

import (
"testing"
"time"

"github.com/blinklabs-io/node/database"
"gorm.io/gorm"
)

type TestTable struct {
gorm.Model
}

// TestInMemorySqliteMultipleTransaction tests that our sqlite connection allows multiple
// concurrent transactions when using in-memory mode. This requires special URI flags, and
// this is mostly making sure that we don't lose them
func TestInMemorySqliteMultipleTransaction(t *testing.T) {
var db database.Database
doQuery := func(sleep time.Duration) error {
txn := db.Metadata().Begin()
if result := txn.First(&TestTable{}); result.Error != nil {
return result.Error
}
time.Sleep(sleep)
if result := txn.Commit(); result.Error != nil {
return result.Error
}
return nil
}
db, err := database.NewInMemory(nil)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if err := db.Metadata().AutoMigrate(&TestTable{}); err != nil {
t.Fatalf("unexpected error: %s", err)
}
if result := db.Metadata().Create(&TestTable{}); result.Error != nil {
t.Fatalf("unexpected error: %s", result.Error)
}
// The linter calls us on the lack of error checking, but it's a goroutine...
//nolint:errcheck
go doQuery(5 * time.Second)
time.Sleep(1 * time.Second)
if err := doQuery(0); err != nil {
t.Fatalf("unexpected error: %s", err)
}
}

0 comments on commit 76101a3

Please sign in to comment.