-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6949 from onflow/leo/pebble-storage-db
[Storage Refactor] Init pebble DB in scaffold.
- Loading branch information
Showing
8 changed files
with
216 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package scaffold | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"github.com/cockroachdb/pebble" | ||
|
||
pebblestorage "github.com/onflow/flow-go/storage/pebble" | ||
) | ||
|
||
func InitPebbleDB(dir string) (*pebble.DB, io.Closer, error) { | ||
// if the pebble DB is not set, we skip initialization | ||
// the pebble DB must be provided to initialize | ||
// since we've set an default directory for the pebble DB, this check | ||
// is not necessary, but rather a sanity check | ||
if dir == "not set" { | ||
return nil, nil, fmt.Errorf("missing required flag '--pebble-dir'") | ||
} | ||
|
||
// Pre-create DB path | ||
err := os.MkdirAll(dir, 0700) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("could not create pebble db (path: %s): %w", dir, err) | ||
} | ||
|
||
db, err := pebblestorage.OpenDefaultPebbleDB(dir) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("could not open newly created pebble db (path: %s): %w", dir, err) | ||
} | ||
|
||
return db, db, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package scaffold_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/onflow/flow-go/cmd" | ||
"github.com/onflow/flow-go/cmd/scaffold" | ||
"github.com/onflow/flow-go/utils/unittest" | ||
) | ||
|
||
func TestInitPebbleDB(t *testing.T) { | ||
unittest.RunWithTempDir(t, func(dir string) { | ||
_, closer, err := scaffold.InitPebbleDB(dir) | ||
require.NoError(t, err) | ||
require.NoError(t, closer.Close()) | ||
}) | ||
} | ||
|
||
func TestInitPebbleDBDirNotSet(t *testing.T) { | ||
_, _, err := scaffold.InitPebbleDB(cmd.NotSet) | ||
require.Error(t, err) | ||
require.Contains(t, err.Error(), "missing required flag") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.