Skip to content

Commit

Permalink
Storing Storm version in the database
Browse files Browse the repository at this point in the history
  • Loading branch information
asdine committed Sep 23, 2016
1 parent 6c51b36 commit 9442b71
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
30 changes: 27 additions & 3 deletions storm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import (
"github.com/boltdb/bolt"
)

// Defaults to gob
const (
metadataBucket = "__storm_metadata"
)

// Defaults to json
var defaultCodec = json.Codec

// Open opens a database at the given path with optional Storm options.
Expand All @@ -35,15 +39,20 @@ func Open(path string, stormOptions ...func(*DB) error) (*DB, error) {
s.boltOptions = &bolt.Options{Timeout: 1 * time.Second}
}

s.root = &node{s: s, rootBucket: s.rootBucket, codec: s.codec}

// skip if UseDB option is used
if s.Bolt == nil {
s.Bolt, err = bolt.Open(path, s.boltMode, s.boltOptions)
if err != nil {
return nil, err
}
}

s.root = &node{s: s, rootBucket: s.rootBucket, codec: s.codec}
err = s.checkVersion()
if err != nil {
return nil, err
}
}

return s, nil
}
Expand Down Expand Up @@ -113,6 +122,21 @@ func (s *DB) WithCodec(codec codec.EncodeDecoder) Node {
return n
}

func (s *DB) checkVersion() error {
var v string
err := s.Get(metadataBucket, "version", &v)
if err != nil && err != ErrNotFound {
return err
}

// for now, we only set the current version if it doesn't exist
if v == "" {
return s.Set(metadataBucket, "version", Version)
}

return nil
}

// toBytes turns an interface into a slice of bytes
func toBytes(key interface{}, encoder codec.EncodeDecoder) ([]byte, error) {
if key == nil {
Expand Down
5 changes: 5 additions & 0 deletions storm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ func TestNewStorm(t *testing.T) {
assert.Equal(t, file, db.Path)
assert.NotNil(t, db.Bolt)
assert.Equal(t, defaultCodec, db.Codec())

var v string
err = db.Get(metadataBucket, "version", &v)
assert.NoError(t, err)
assert.Equal(t, Version, v)
}

func TestNewStormWithStormOptions(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package storm

// Version of Storm
const Version = "0.4.0"

0 comments on commit 9442b71

Please sign in to comment.