Skip to content
This repository has been archived by the owner on Mar 28, 2023. It is now read-only.

Create Migration021 #1532

Merged
merged 2 commits into from
Apr 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion repo/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/tyler-smith/go-bip39"
)

const RepoVersion = "21"
const RepoVersion = "22"

var log = logging.MustGetLogger("repo")
var ErrRepoExists = errors.New("IPFS configuration file exists. Reinitializing would overwrite your keys. Use -f to force overwrite.")
Expand Down
1 change: 1 addition & 0 deletions repo/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var (
migrations.Migration018{},
migrations.Migration019{},
migrations.Migration020{},
migrations.Migration021{},
}
)

Expand Down
97 changes: 97 additions & 0 deletions repo/migrations/Migration021.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package migrations

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
)

// Migration021 migrates the config file to set the Swarm: EnableAutoRelay option
// to true.
type Migration021 struct{}

func (Migration021) Up(repoPath, dbPassword string, testnet bool) error {
var (
configMap = map[string]interface{}{}
configBytes, err = ioutil.ReadFile(path.Join(repoPath, "config"))
)
if err != nil {
return fmt.Errorf("reading config: %s", err.Error())
}

if err = json.Unmarshal(configBytes, &configMap); err != nil {
return fmt.Errorf("unmarshal config: %s", err.Error())
}

iSwarm, ok := configMap["Swarm"]
if !ok {
return fmt.Errorf("unmarshal config: missing swarm key")
}

swarm, ok := iSwarm.(map[string]interface{})
if !ok {
return fmt.Errorf("unmarshal config: error type asserting swarm")
}

swarm["EnableAutoRelay"] = true
swarm["EnableAutoNATService"] = false
configMap["Swarm"] = swarm

newConfigBytes, err := json.MarshalIndent(configMap, "", " ")
if err != nil {
return fmt.Errorf("marshal migrated config: %s", err.Error())
}

if err := ioutil.WriteFile(path.Join(repoPath, "config"), newConfigBytes, os.ModePerm); err != nil {
return fmt.Errorf("writing migrated config: %s", err.Error())
}

if err := writeRepoVer(repoPath, 22); err != nil {
return fmt.Errorf("bumping repover to 18: %s", err.Error())
}
return nil
}

func (Migration021) Down(repoPath, dbPassword string, testnet bool) error {
var (
configMap = map[string]interface{}{}
configBytes, err = ioutil.ReadFile(path.Join(repoPath, "config"))
)
if err != nil {
return fmt.Errorf("reading config: %s", err.Error())
}

if err = json.Unmarshal(configBytes, &configMap); err != nil {
return fmt.Errorf("unmarshal config: %s", err.Error())
}

iSwarm, ok := configMap["Swarm"]
if !ok {
return fmt.Errorf("unmarshal config: missing swarm key")
}

swarm, ok := iSwarm.(map[string]interface{})
if !ok {
return fmt.Errorf("unmarshal config: error type asserting swarm")
}

delete(swarm, "EnableAutoRelay")
delete(swarm, "EnableAutoNATService")
configMap["Swarm"] = swarm

newConfigBytes, err := json.MarshalIndent(configMap, "", " ")
if err != nil {
return fmt.Errorf("marshal migrated config: %s", err.Error())
}

if err := ioutil.WriteFile(path.Join(repoPath, "config"), newConfigBytes, os.ModePerm); err != nil {
return fmt.Errorf("writing migrated config: %s", err.Error())
}

if err := writeRepoVer(repoPath, 21); err != nil {
return fmt.Errorf("dropping repover to 16: %s", err.Error())
}
return nil
}
107 changes: 107 additions & 0 deletions repo/migrations/Migration021_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package migrations_test

import (
"github.com/OpenBazaar/openbazaar-go/repo/migrations"
"github.com/OpenBazaar/openbazaar-go/schema"
"io/ioutil"
"os"
"regexp"
"testing"
)

const preMigration021Config = `{
"Swarm": {
"AddrFilters": null,
"ConnMgr": {
"GracePeriod": "",
"HighWater": 0,
"LowWater": 0,
"Type": ""
},
"DisableBandwidthMetrics": false,
"DisableNatPortMap": false,
"DisableRelay": false,
"EnableRelayHop": false
}
}`

const postMigration021Config = `{
"Swarm": {
"AddrFilters": null,
"ConnMgr": {
"GracePeriod": "",
"HighWater": 0,
"LowWater": 0,
"Type": ""
},
"DisableBandwidthMetrics": false,
"DisableNatPortMap": false,
"DisableRelay": false,
"EnableAutoNATService": false,
"EnableAutoRelay": true,
"EnableRelayHop": false
}
}`

func TestMigration021(t *testing.T) {
var testRepo, err = schema.NewCustomSchemaManager(schema.SchemaContext{
DataPath: schema.GenerateTempPath(),
TestModeEnabled: true,
})
if err != nil {
t.Fatal(err)
}

if err = testRepo.BuildSchemaDirectories(); err != nil {
t.Fatal(err)
}
defer testRepo.DestroySchemaDirectories()

var (
configPath = testRepo.DataPathJoin("config")
repoverPath = testRepo.DataPathJoin("repover")
)
if err = ioutil.WriteFile(configPath, []byte(preMigration021Config), os.ModePerm); err != nil {
t.Fatal(err)
}

if err = ioutil.WriteFile(repoverPath, []byte("15"), os.ModePerm); err != nil {
t.Fatal(err)
}

var m migrations.Migration021
err = m.Up(testRepo.DataPath(), "", true)
if err != nil {
t.Fatal(err)
}

configBytes, err := ioutil.ReadFile(configPath)
if err != nil {
t.Fatal(err)
}

var re = regexp.MustCompile(`\s`)
if re.ReplaceAllString(string(configBytes), "") != re.ReplaceAllString(string(postMigration021Config), "") {
t.Logf("actual: %s", re.ReplaceAllString(string(configBytes), ""))
t.Fatal("incorrect post-migration config")
}

assertCorrectRepoVer(t, repoverPath, "22")

err = m.Down(testRepo.DataPath(), "", true)
if err != nil {
t.Fatal(err)
}

configBytes, err = ioutil.ReadFile(configPath)
if err != nil {
t.Fatal(err)
}

if re.ReplaceAllString(string(configBytes), "") != re.ReplaceAllString(string(preMigration021Config), "") {
t.Logf("actual: %s", re.ReplaceAllString(string(configBytes), ""))
t.Fatal("incorrect post-migration config")
}

assertCorrectRepoVer(t, repoverPath, "21")
}