Skip to content

Commit

Permalink
feat(config): version-to-version migrations, migration for the upcomi… (
Browse files Browse the repository at this point in the history
#44)

* feat(config): version-to-version migrations, migration for the upcoming v0.3.0 release
  • Loading branch information
anfragment authored Jan 2, 2024
1 parent e74e748 commit 1357130
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
1 change: 1 addition & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func (a *App) shutdown(ctx context.Context) {
}

func (a *App) domReady(ctx context.Context) {
config.RunMigrations()
config.SelfUpdate(ctx)
}

Expand Down
4 changes: 4 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (
// Config is the singleton config instance.
var Config config

// firstLaunch is true if the application is being run for the first time.
var firstLaunch bool

type filterList struct {
Name string `json:"name"`
Type string `json:"type"`
Expand Down Expand Up @@ -199,6 +202,7 @@ func init() {
if err := os.WriteFile(configFile, configData, 0644); err != nil {
log.Fatalf("failed to write config file: %v", err)
}
firstLaunch = true
}

if err := json.Unmarshal(configData, &Config); err != nil {
Expand Down
79 changes: 79 additions & 0 deletions config/migrations.go
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)
}
}

0 comments on commit 1357130

Please sign in to comment.