Skip to content

Commit

Permalink
Fix backup storage and add logs
Browse files Browse the repository at this point in the history
See #12
  • Loading branch information
rafaeljusto committed May 10, 2017
1 parent 6f4ec24 commit 4b26a78
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion internal/storage/boltdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,21 @@ func NewBoltDB(logger log.Logger, filename string) *BoltDB {
// }
// }
func (b *BoltDB) Save(backup Backup) error {
b.logger.Debugf("storage: saving backup “%s” in boltdb storage", backup.Backup.ID)

db, err := bolt.Open(b.Filename, BoltDBFileMode, nil)
if err != nil {
return errors.WithStack(newError(ErrorCodeOpeningFile, err))
}
defer db.Close()

encoded, err := json.Marshal(b)
encoded, err := json.Marshal(backup)
if err != nil {
return errors.WithStack(newError(ErrorCodeEncodingBackup, err))
}

b.logger.Debugf("storage: saving backup json format: “%s”", string(encoded))

err = db.Update(func(tx *bolt.Tx) error {
var bucket *bolt.Bucket
if bucket, err = tx.CreateBucketIfNotExists(BoltDBBucket); err != nil {
Expand All @@ -78,6 +82,7 @@ func (b *BoltDB) Save(backup Backup) error {
return errors.WithStack(newError(ErrorCodeUpdatingDatabase, err))
}

b.logger.Infof("storage: backup “%s” saved successfully in boltdb storage", backup.Backup.ID)
return nil
}

Expand All @@ -98,6 +103,8 @@ func (b *BoltDB) Save(backup Backup) error {
// }
// }
func (b BoltDB) List() (Backups, error) {
b.logger.Debug("storage: listing backups from boltdb storage")

db, err := bolt.Open(b.Filename, BoltDBFileMode, nil)
if err != nil {
return nil, errors.WithStack(newError(ErrorCodeOpeningFile, err))
Expand Down Expand Up @@ -133,6 +140,7 @@ func (b BoltDB) List() (Backups, error) {
return nil, errors.WithStack(newError(ErrorCodeListingDatabase, err))
}

b.logger.Infof("storage: backups listed successfully from boltdb storage")
sort.Sort(backups)
return backups, nil
}
Expand All @@ -154,6 +162,8 @@ func (b BoltDB) List() (Backups, error) {
// }
// }
func (b BoltDB) Remove(id string) error {
b.logger.Debugf("storage: removing backup “%s” from boltdb storage", id)

db, err := bolt.Open(b.Filename, BoltDBFileMode, nil)
if err != nil {
return errors.WithStack(newError(ErrorCodeOpeningFile, err))
Expand All @@ -177,5 +187,6 @@ func (b BoltDB) Remove(id string) error {
return errors.WithStack(newError(ErrorCodeUpdatingDatabase, err))
}

b.logger.Infof("storage: backup “%s” removed successfully from boltdb storage", id)
return nil
}

0 comments on commit 4b26a78

Please sign in to comment.