Skip to content

Commit

Permalink
Fix observer.OpenDB() in-mem sqlite creation (it was creating redunda…
Browse files Browse the repository at this point in the history
…nt directory)
  • Loading branch information
swift1337 committed Jun 27, 2024
1 parent 398ae1a commit 87b2f41
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 18 deletions.
42 changes: 27 additions & 15 deletions zetaclient/chains/base/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,27 +290,26 @@ func (ob *Observer) StopChannel() chan struct{} {

// OpenDB open sql database in the given path.
func (ob *Observer) OpenDB(dbPath string, dbName string) error {
// create db path if not exist
if _, err := os.Stat(dbPath); os.IsNotExist(err) {
err := os.MkdirAll(dbPath, os.ModePerm)
if err != nil {
return errors.Wrapf(err, "error creating db path: %s", dbPath)
var dial gorm.Dialector

// SQLite in-mem db
if strings.Contains(dbPath, ":memory:") {
dial = sqlite.Open(dbPath)
} else {
if err := ensureDirectory(dbPath); err != nil {
return errors.Wrapf(err, "unable to ensure dbPath %q", dbPath)
}
}

// use custom dbName or chain name if not provided
if dbName == "" {
dbName = ob.chain.ChainName.String()
}
path := fmt.Sprintf("%s/%s", dbPath, dbName)
// use custom dbName or chain name if not provided
if dbName == "" {
dbName = ob.chain.ChainName.String()
}

// use memory db if specified
if strings.Contains(dbPath, ":memory:") {
path = dbPath
dial = sqlite.Open(fmt.Sprintf("%s/%s", dbPath, dbName))
}

// open db
db, err := gorm.Open(sqlite.Open(path), &gorm.Config{Logger: logger.Default.LogMode(logger.Silent)})
db, err := gorm.Open(dial, &gorm.Config{Logger: logger.Default.LogMode(logger.Silent)})
if err != nil {
return errors.Wrap(err, "error opening db")
}
Expand All @@ -320,11 +319,24 @@ func (ob *Observer) OpenDB(dbPath string, dbName string) error {
if err != nil {
return errors.Wrap(err, "error migrating db")
}

ob.db = db

return nil
}

func ensureDirectory(path string) error {
_, err := os.Stat(path)
switch {
case os.IsNotExist(err):
return os.MkdirAll(path, os.ModePerm)
case err != nil:
return err
default:
return nil
}
}

// CloseDB close the database.
func (ob *Observer) CloseDB() error {
dbInst, err := ob.db.DB()
Expand Down
2 changes: 1 addition & 1 deletion zetaclient/chains/base/observer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ func TestOpenCloseDB(t *testing.T) {
})
t.Run("should return error on invalid db path", func(t *testing.T) {
err := ob.OpenDB("/invalid/123db", "")
require.ErrorContains(t, err, "error creating db path")
require.ErrorContains(t, err, "unable to ensure dbPath")
})
}

Expand Down
2 changes: 1 addition & 1 deletion zetaclient/chains/bitcoin/observer/observer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func Test_NewObserver(t *testing.T) {
logger: base.Logger{},
ts: nil,
fail: true,
message: "error creating db path",
message: "unable to ensure dbPath",
},
}

Expand Down
2 changes: 1 addition & 1 deletion zetaclient/chains/evm/observer/observer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func Test_NewObserver(t *testing.T) {
logger: base.Logger{},
ts: nil,
fail: true,
message: "error creating db path",
message: "unable to ensure dbPath",
},
{
name: "should fail if RPC call fails",
Expand Down

0 comments on commit 87b2f41

Please sign in to comment.