diff --git a/v2/snapshot.go b/v2/snapshot.go index 303a030f7..b03d461d5 100644 --- a/v2/snapshot.go +++ b/v2/snapshot.go @@ -363,6 +363,11 @@ func (sql *SqliteDb) ImportSnapshotFromTable(version int64, traverseOrder Traver } func (sql *SqliteDb) ImportMostRecentSnapshot(targetVersion int64, traverseOrder TraverseOrderType, loadLeaves bool) (*Node, int64, error) { + // First check if we need to find the database paths + if sql.opts.Path == "" { + return nil, 0, fmt.Errorf("database path is empty") + } + read, err := sql.getReadConn() if err != nil { return nil, 0, err @@ -414,6 +419,8 @@ func (sql *SqliteDb) ImportMostRecentSnapshot(targetVersion int64, traverseOrder return root, version, err } +// FindDbsInPath finds all database directories in the given path that contain a changelog.sqlite file. +// This function is used by various commands to locate IAVL databases. func FindDbsInPath(path string) ([]string, error) { var paths []string err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error { @@ -431,9 +438,6 @@ func FindDbsInPath(path string) ([]string, error) { return paths, nil } -// TODO -// merge these two functions - func (snap *sqliteSnapshot) writeStep(node *Node) error { snap.ordinal++ // Pre-order, NLR traversal diff --git a/v2/sqlite_test.go b/v2/sqlite_test.go index 9b31d9199..a0730342c 100644 --- a/v2/sqlite_test.go +++ b/v2/sqlite_test.go @@ -322,3 +322,18 @@ func Test_ConcurrentIndexRead(t *testing.T) { err = conn.Exec("CREATE INDEX foo_idx ON foo (id)") require.NoError(t, err) } + +func TestImportMostRecentSnapshotEmptyPath(t *testing.T) { + // Create a db with empty path + sql, err := NewSqliteDb(NewNodePool(), SqliteDbOptions{Path: ""}) + require.NoError(t, err) + + // Attempt to import snapshot from an empty path + root, version, err := sql.ImportMostRecentSnapshot(1, PreOrder, false) + + // Verify that we get the expected error about no prior snapshot found + require.Error(t, err) + require.Nil(t, root) + require.Equal(t, int64(0), version) + require.Contains(t, err.Error(), "no prior snapshot found") +}