-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(config): version-to-version migrations, migration for the upcomi… (
#44) * feat(config): version-to-version migrations, migration for the upcoming v0.3.0 release
- Loading branch information
1 parent
e74e748
commit 1357130
Showing
3 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package config | ||
|
||
import ( | ||
"errors" | ||
"log" | ||
"os" | ||
"path" | ||
|
||
"github.com/blang/semver" | ||
) | ||
|
||
// migrations is a map of version to migration function. | ||
// Warning: RunMigration() runs the migrations in arbitrary order. | ||
var migrations = map[string]func() error{ | ||
"v0.3.0": func() error { | ||
errStr := Config.AddFilterList(filterList{ | ||
Name: "DandelionSprout's URL Shortener", | ||
Type: "privacy", | ||
Url: "https://raw.githubusercontent.com/DandelionSprout/adfilt/master/LegitimateURLShortener.txt", | ||
Enabled: true, | ||
}) | ||
if errStr != "" { | ||
err := errors.New(errStr) | ||
return err | ||
} | ||
return nil | ||
}, | ||
} | ||
|
||
// RunMigrations runs the version-to-version migrations. | ||
func RunMigrations() { | ||
if Version == "development" { | ||
log.Println("skipping migrations in development mode") | ||
return | ||
} | ||
|
||
var lastMigration string | ||
lastMigrationFile := path.Join(Config.ConfigDir, "last_migration") | ||
if firstLaunch { | ||
lastMigration = Version | ||
} else { | ||
if _, err := os.Stat(lastMigrationFile); !os.IsNotExist(err) { | ||
lastMigrationData, err := os.ReadFile(lastMigrationFile) | ||
if err != nil { | ||
log.Fatalf("failed to read last migration file: %v", err) | ||
} | ||
lastMigration = string(lastMigrationData) | ||
} else { | ||
// Should trigger when updating from pre v0.3.0 | ||
lastMigration = "v0.0.0" | ||
} | ||
} | ||
|
||
lastMigrationV, err := semver.ParseTolerant(lastMigration) | ||
if err != nil { | ||
log.Printf("error parsing last migration(%s): %v\n", lastMigration, err) | ||
return | ||
} | ||
|
||
for version, migration := range migrations { | ||
versionV, err := semver.ParseTolerant(version) | ||
if err != nil { | ||
log.Printf("error parsing migration version(%s): %v\n", version, err) | ||
continue | ||
} | ||
|
||
if lastMigrationV.LT(versionV) { | ||
if err := migration(); err != nil { | ||
log.Printf("error running migration(%s): %v\n", version, err) | ||
} else { | ||
log.Printf("ran migration %s\n", version) | ||
} | ||
} | ||
} | ||
|
||
if err := os.WriteFile(lastMigrationFile, []byte(Version), 0644); err != nil { | ||
log.Printf("error writing last migration file: %v\n", err) | ||
} | ||
} |