forked from OpenBazaar/openbazaar-go
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Migration011.go
88 lines (73 loc) · 2.12 KB
/
Migration011.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package migrations
import (
"encoding/json"
"errors"
"io/ioutil"
"os"
"path"
)
// migration011ModeratorsKey is the key we want to manipulate in the JSON
const migration011ModeratorsKey = "moderators"
type Migration011 struct{}
type Migration011_listing struct {
Listing Migration011_listing_listing `json:"listing"`
}
type Migration011_listing_listing struct {
ModeratorIDs []string `json:"moderators"`
}
func (Migration011) Up(repoPath string, dbPassword string, testnet bool) error {
var (
err error
listingJSON []byte
listingsJSON []byte
listingRecords []map[string]interface{}
listingsFilePath = path.Join(repoPath, "root", "listings.json")
)
// Don't do anything if no listing index exists
if _, err := os.Stat(listingsFilePath); os.IsNotExist(err) {
return writeRepoVer(repoPath, 12)
}
// Load index
listingsJSON, err = ioutil.ReadFile(listingsFilePath)
if err != nil {
return err
}
if err = json.Unmarshal(listingsJSON, &listingRecords); err != nil {
return err
}
// Iterate over listing. If a listing is missing moderators then load the full
// listing file and populate the listing index abstract
for _, listing := range listingRecords {
if listing[migration011ModeratorsKey] != nil {
continue
}
iSlug := listing["slug"]
slug, ok := iSlug.(string)
if !ok {
return errors.New("'slug' is not a string")
}
listingFilePath := path.Join(repoPath, "root", "listings", slug+".json")
listingJSON, err = ioutil.ReadFile(listingFilePath)
if err != nil {
return err
}
listingRecord := &Migration011_listing{}
if err = json.Unmarshal(listingJSON, &listingRecord); err != nil {
return err
}
listing[migration011ModeratorsKey] = listingRecord.Listing.ModeratorIDs
}
// Write updated index back to disk
listingsJSON, err = json.MarshalIndent(listingRecords, "", " ")
if err != nil {
return err
}
err = ioutil.WriteFile(listingsFilePath, listingsJSON, os.ModePerm)
if err != nil {
return err
}
return writeRepoVer(repoPath, 12)
}
func (Migration011) Down(repoPath string, dbPassword string, testnet bool) error {
return writeRepoVer(repoPath, 11)
}