Add database migrations
Signed-off-by: Derek McGowan <derek@mcgstyle.net>
This commit is contained in:
75
metadata/migrations.go
Normal file
75
metadata/migrations.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package metadata
|
||||
|
||||
import "github.com/boltdb/bolt"
|
||||
|
||||
type migration struct {
|
||||
schema string
|
||||
version int
|
||||
migrate func(*bolt.Tx) error
|
||||
}
|
||||
|
||||
var migrations = []migration{
|
||||
{
|
||||
schema: "v1",
|
||||
version: 1,
|
||||
migrate: addChildLinks,
|
||||
},
|
||||
}
|
||||
|
||||
// addChildLinks Adds children key to the snapshotters to enforce snapshot
|
||||
// entries cannot be removed which have children
|
||||
func addChildLinks(tx *bolt.Tx) error {
|
||||
v1bkt := tx.Bucket(bucketKeyVersion)
|
||||
if v1bkt == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// iterate through each namespace
|
||||
v1c := v1bkt.Cursor()
|
||||
|
||||
for k, v := v1c.First(); k != nil; k, v = v1c.Next() {
|
||||
if v != nil {
|
||||
continue
|
||||
}
|
||||
nbkt := v1bkt.Bucket(k)
|
||||
|
||||
sbkt := nbkt.Bucket(bucketKeyObjectSnapshots)
|
||||
if sbkt != nil {
|
||||
// Iterate through each snapshotter
|
||||
if err := sbkt.ForEach(func(sk, sv []byte) error {
|
||||
if sv != nil {
|
||||
return nil
|
||||
}
|
||||
snbkt := sbkt.Bucket(sk)
|
||||
|
||||
// Iterate through each snapshot
|
||||
return snbkt.ForEach(func(k, v []byte) error {
|
||||
if v != nil {
|
||||
return nil
|
||||
}
|
||||
parent := snbkt.Bucket(k).Get(bucketKeyParent)
|
||||
if len(parent) > 0 {
|
||||
pbkt := snbkt.Bucket(parent)
|
||||
if pbkt == nil {
|
||||
// Not enforcing consistency during migration, skip
|
||||
return nil
|
||||
}
|
||||
cbkt, err := pbkt.CreateBucketIfNotExists(bucketKeyChildren)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := cbkt.Put(k, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user