catbase/util/migrate/config.go

62 lines
1.2 KiB
Go

package main
import (
"github.com/jmoiron/sqlx"
bh "github.com/timshannon/bolthold"
"go.etcd.io/bbolt"
"log"
)
type Value struct {
// Key is the key field of the table
Key string `db:"key" json:"key"`
// Value represents the secret that must not be shared
Value string `db:"value" json:"value"`
}
// Secret is a separate type (for storage differentiation)
type Secret Value
func migrateConfig(db *sqlx.DB, store *bh.Store) error {
allValues := []Value{}
err := db.Select(&allValues, `select * from config`)
if err != nil {
return err
}
for _, v := range allValues {
err = store.Insert(v.Key, v)
if err != nil {
return err
}
}
log.Printf("Migrated %d values", len(allValues))
allSecrets := []Secret{}
err = db.Select(&allSecrets, `select * from secrets`)
if err != nil {
return err
}
for _, v := range allSecrets {
err = store.Insert(v.Key, v)
if err != nil {
return err
}
}
log.Printf("Migrated %d secrets", len(allSecrets))
return nil
}
func rmConfig(store *bh.Store) {
store.Bolt().Batch(func(tx *bbolt.Tx) error {
tx.DeleteBucket([]byte("variables"))
return nil
})
store.Bolt().Batch(func(tx *bbolt.Tx) error {
tx.DeleteBucket([]byte("secrets"))
return nil
})
}