Files
diun/internal/db/migrate.go
CrazyMax 1115234010 Add CLI to interact with Diun through gRPC (#382)
Add simple CLI to interact with Diun through gRPC
Create image and notif proto services
Compile and validate protos through a dedicated Dockerfile and bake target
Implement proto definitions
Move server as `serve` command
New commands `image` and `notif`
Refactor command line usage doc
Better CLI error handling
Tools build constraint to manage tools deps through go modules
Add upgrade notes

Co-authored-by: CrazyMax <crazy-max@users.noreply.github.com>
2021-05-26 18:18:10 +02:00

80 lines
1.7 KiB
Go

package db
import (
"encoding/json"
"fmt"
"time"
"github.com/opencontainers/go-digest"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"go.etcd.io/bbolt"
)
// Migrate runs database migration
func (c *Client) Migrate() error {
if c.metadata.Version == dbVersion {
return nil
}
migrations := map[int]func(*Client) error{
2: (*Client).migration2,
}
for version := c.metadata.Version + 1; version <= dbVersion; version++ {
migration, found := migrations[version]
if !found {
return fmt.Errorf("database migration v%d not found", version)
}
log.Info().Msgf("Database migration v%d...", version)
if err := migration(c); err != nil {
return errors.Wrapf(err, "Database migration v%d failed", version)
}
}
return c.WriteMetadata(Metadata{
Version: dbVersion,
})
}
func (c *Client) migration2() error {
type oldManifest struct {
Name string
Tag string
MIMEType string
Digest digest.Digest
Created *time.Time
DockerVersion string
Labels map[string]string
Architecture string `json:"-"`
Os string `json:"-"`
Layers []string
}
tx, err := c.Begin(true)
if err != nil {
return err
}
defer func() {
if err := tx.Rollback(); err != nil && err != bbolt.ErrTxClosed {
log.Error().Err(err).Msg("Cannot rollback")
}
}()
bucket := tx.Bucket([]byte(bucketManifest))
curs := bucket.Cursor()
for k, v := curs.First(); k != nil; k, v = curs.Next() {
var oldManifest oldManifest
if err := json.Unmarshal(v, &oldManifest); err != nil {
return err
}
entryBytes, _ := json.Marshal(oldManifest)
if err := bucket.Put(k, entryBytes); err != nil {
return err
}
}
return tx.Commit()
}