From 82ed5a3a157e869b04caea0933834de7d9af9d56 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Thu, 31 Mar 2022 21:12:13 +0200 Subject: [PATCH] Add repo upgrade migration --- internal/migrations/upgrade_repo_v2.go | 46 ++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 internal/migrations/upgrade_repo_v2.go diff --git a/internal/migrations/upgrade_repo_v2.go b/internal/migrations/upgrade_repo_v2.go new file mode 100644 index 000000000..7f8f8111d --- /dev/null +++ b/internal/migrations/upgrade_repo_v2.go @@ -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 +}