Add repo upgrade migration
This commit is contained in:
parent
3af6c180e4
commit
82ed5a3a15
1 changed files with 46 additions and 0 deletions
46
internal/migrations/upgrade_repo_v2.go
Normal file
46
internal/migrations/upgrade_repo_v2.go
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
package migrations
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/restic/restic/internal/restic"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
register(&UpgradeRepoV2{})
|
||||||
|
}
|
||||||
|
|
||||||
|
type UpgradeRepoV2 struct{}
|
||||||
|
|
||||||
|
func (*UpgradeRepoV2) Name() string {
|
||||||
|
return "upgrade_repo_v2"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*UpgradeRepoV2) Desc() string {
|
||||||
|
return "upgrade a repository to version 2"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*UpgradeRepoV2) Check(ctx context.Context, repo restic.Repository) (bool, error) {
|
||||||
|
isV1 := repo.Config().Version == 1
|
||||||
|
return isV1, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*UpgradeRepoV2) Apply(ctx context.Context, repo restic.Repository) error {
|
||||||
|
cfg := repo.Config()
|
||||||
|
cfg.Version = 2
|
||||||
|
|
||||||
|
h := restic.Handle{Type: restic.ConfigFile}
|
||||||
|
|
||||||
|
err := repo.Backend().Remove(ctx, h)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("remove old config file failed: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = repo.SaveJSONUnpacked(ctx, restic.ConfigFile, cfg)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("save new config file failed: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue