2017-03-12 16:24:15 +00:00
|
|
|
package bolt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/portainer/portainer"
|
|
|
|
|
|
|
|
"github.com/boltdb/bolt"
|
|
|
|
)
|
|
|
|
|
2017-05-23 19:01:19 +00:00
|
|
|
// VersionService represents a service to manage stored versions.
|
2017-03-12 16:24:15 +00:00
|
|
|
type VersionService struct {
|
|
|
|
store *Store
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2017-05-23 19:01:19 +00:00
|
|
|
dBVersionKey = "DB_VERSION"
|
2017-03-12 16:24:15 +00:00
|
|
|
)
|
|
|
|
|
2017-05-23 19:01:19 +00:00
|
|
|
// DBVersion retrieves the stored database version.
|
2017-03-12 16:24:15 +00:00
|
|
|
func (service *VersionService) DBVersion() (int, error) {
|
|
|
|
var data []byte
|
|
|
|
err := service.store.db.View(func(tx *bolt.Tx) error {
|
|
|
|
bucket := tx.Bucket([]byte(versionBucketName))
|
2017-05-23 19:01:19 +00:00
|
|
|
value := bucket.Get([]byte(dBVersionKey))
|
2017-03-12 16:24:15 +00:00
|
|
|
if value == nil {
|
|
|
|
return portainer.ErrDBVersionNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
data = make([]byte, len(value))
|
|
|
|
copy(data, value)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
dbVersion, err := strconv.Atoi(string(data))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return dbVersion, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// StoreDBVersion store the database version.
|
|
|
|
func (service *VersionService) StoreDBVersion(version int) error {
|
|
|
|
return service.store.db.Update(func(tx *bolt.Tx) error {
|
|
|
|
bucket := tx.Bucket([]byte(versionBucketName))
|
|
|
|
|
|
|
|
data := []byte(strconv.Itoa(version))
|
2017-05-23 19:01:19 +00:00
|
|
|
err := bucket.Put([]byte(dBVersionKey), data)
|
2017-03-12 16:24:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|