| 1 | package common |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "io" |
| 6 | "path/filepath" |
| 7 | ) |
| 8 | |
| 9 | // BaseMigration provides common functionality for migrations |
| 10 | type BaseMigration struct { |
| 11 | FromVersion string |
| 12 | ToVersion string |
| 13 | Description string |
| 14 | Convert func(in io.ReadSeeker, out io.Writer) error |
| 15 | } |
| 16 | |
| 17 | // Versions returns the version string for this migration |
| 18 | func (m *BaseMigration) Versions() string { |
| 19 | return fmt.Sprintf("%s-to-%s", m.FromVersion, m.ToVersion) |
| 20 | } |
| 21 | |
| 22 | // configBackupSuffix returns the backup suffix for the config file |
| 23 | // e.g. ".16-to-17.bak" results in "config.16-to-17.bak" |
| 24 | func (m *BaseMigration) configBackupSuffix() string { |
| 25 | return fmt.Sprintf(".%s-to-%s.bak", m.FromVersion, m.ToVersion) |
| 26 | } |
| 27 | |
| 28 | // Reversible returns true as we keep backups |
| 29 | func (m *BaseMigration) Reversible() bool { |
| 30 | return true |
| 31 | } |
| 32 | |
| 33 | // Apply performs the migration |
| 34 | func (m *BaseMigration) Apply(opts Options) error { |
| 35 | if opts.Verbose { |
| 36 | fmt.Printf("applying %s repo migration\n", m.Versions()) |
| 37 | if m.Description != "" { |
| 38 | fmt.Printf("> %s\n", m.Description) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | // Check version |
| 43 | if err := CheckVersion(opts.Path, m.FromVersion); err != nil { |
| 44 | return err |
| 45 | } |
| 46 | |
| 47 | configPath := filepath.Join(opts.Path, "config") |
| 48 | |
| 49 | // Perform migration with backup |
| 50 | if err := WithBackup(configPath, m.configBackupSuffix(), m.Convert); err != nil { |
| 51 | return err |
| 52 | } |
| 53 | |
| 54 | // Update version |
| 55 | if err := WriteVersion(opts.Path, m.ToVersion); err != nil { |
| 56 | if opts.Verbose { |
| 57 | fmt.Printf("failed to update version file to %s\n", m.ToVersion) |
| 58 | } |
| 59 | return err |
| 60 | } |
| 61 | |
| 62 | if opts.Verbose { |
| 63 | fmt.Println("updated version file") |
| 64 | fmt.Printf("Migration %s succeeded\n", m.Versions()) |
| 65 | } |
| 66 | |
| 67 | return nil |
| 68 | } |
| 69 | |
| 70 | // Revert reverts the migration |
| 71 | func (m *BaseMigration) Revert(opts Options) error { |
| 72 | if opts.Verbose { |
| 73 | fmt.Println("reverting migration") |
| 74 | } |
| 75 | |
| 76 | // Check we're at the expected version |
| 77 | if err := CheckVersion(opts.Path, m.ToVersion); err != nil { |
| 78 | return err |
| 79 | } |
| 80 | |
| 81 | // Restore backup |
| 82 | configPath := filepath.Join(opts.Path, "config") |
| 83 | if err := RevertBackup(configPath, m.configBackupSuffix()); err != nil { |
| 84 | return err |
| 85 | } |
| 86 | |
| 87 | // Revert version |
| 88 | if err := WriteVersion(opts.Path, m.FromVersion); err != nil { |
| 89 | return err |
| 90 | } |
| 91 | |
| 92 | if opts.Verbose { |
| 93 | fmt.Printf("lowered version number to %s\n", m.FromVersion) |
| 94 | } |
| 95 | |
| 96 | return nil |
| 97 | } |