remove migrations code from go-ipfs, in favor of fs-repo-migrations repo
Jeromy committed
Apr 15, 2015 at 14:28 UTC
591cca9507f05f913b3874ed0c649001635b53ff
3 files changed
-373
repo/fsrepo/migrations/0-to-1/main.go
deleted
-69
@@ -1,69 +0,0 @@
1
-package main
2
-
3
-import (
4
- "fmt"
5
- "os"
6
- "strings"
7
-
8
- migrate "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-migrate"
9
- mfsr "github.com/ipfs/go-ipfs/repo/fsrepo/migrations"
10
-)
11
-
12
-type migration struct {
13
-}
14
-
15
-// Version is the int version number. This could be a string
16
-// in future versions
17
-func (m migration) Versions() string {
18
- return "0-to-1"
19
-}
20
-
21
-// Reversible returns whether this migration can be reverted.
22
-// Endeavor to make them all reversible. This is here only to warn users
23
-// in case this is not the case.
24
-func (m migration) Reversible() bool {
25
- return true
26
-}
27
-
28
-// Apply applies the migration in question.
29
-// This migration merely adds a version file.
30
-func (m migration) Apply(opts migrate.Options) error {
31
- repo := mfsr.RepoPath(opts.Path)
32
-
33
- // first, check if there is a version file.
34
- // if there is, bail out.
35
- if v, err := repo.Version(); err == nil {
36
- return fmt.Errorf("repo at %s is version %s (not 0)", opts.Path, v)
37
- } else if !strings.Contains(err.Error(), "no version file in repo") {
38
- return err
39
- }
40
-
41
- // add the version file
42
- if err := repo.WriteVersion("1"); err != nil {
43
- return err
44
- }
45
-
46
- return nil
47
-}
48
-
49
-// Revert un-applies the migration in question. This should be best-effort.
50
-// Some migrations are definitively one-way. If so, return an error.
51
-func (m migration) Revert(opts migrate.Options) error {
52
- repo := mfsr.RepoPath(opts.Path)
53
-
54
- if err := repo.CheckVersion("1"); err != nil {
55
- return err
56
- }
57
-
58
- // remove the version file
59
- if err := os.Remove(repo.VersionFile()); err != nil {
60
- return err
61
- }
62
-
63
- return nil
64
-}
65
-
66
-func main() {
67
- m := migration{}
68
- migrate.Main(&m)
69
-}
repo/fsrepo/migrations/1-to-2/main.go
deleted
-248
@@ -1,248 +0,0 @@
1
-package main
2
-
3
-import (
4
- "encoding/json"
5
- "fmt"
6
- "os"
7
- "path"
8
- "strings"
9
-
10
- dstore "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
11
- flatfs "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/flatfs"
12
- leveldb "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/leveldb"
13
- dsq "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/query"
14
- migrate "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-migrate"
15
- mfsr "github.com/ipfs/go-ipfs/repo/fsrepo/migrations"
16
-
17
- context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
18
-)
19
-
20
-var _ = context.Background
21
-
22
-const peerKeyName = "peer.key"
23
-
24
-type migration struct{}
25
-
26
-func (m migration) Versions() string {
27
- return "1-to-2"
28
-}
29
-
30
-func (m migration) Reversible() bool {
31
- return true
32
-}
33
-
34
-func (m migration) Apply(opts migrate.Options) error {
35
- repo := mfsr.RepoPath(opts.Path)
36
-
37
- if err := repo.CheckVersion("1"); err != nil {
38
- return err
39
- }
40
-
41
- // 1) run some sanity checks to make sure we should even bother
42
- err := sanityChecks(opts)
43
- if err != nil {
44
- return err
45
- }
46
-
47
- // 2) Transfer blocks out of leveldb into flatDB
48
- err = transferBlocksToFlatDB(opts.Path)
49
- if err != nil {
50
- return err
51
- }
52
-
53
- // 3) move ipfs path from .go-ipfs to .ipfs
54
- newpath, err := moveIpfsDir(opts.Path)
55
- if err != nil {
56
- return err
57
- }
58
-
59
- // 4) Update version number
60
- repo = mfsr.RepoPath(newpath)
61
- err = repo.WriteVersion("2")
62
- if err != nil {
63
- return err
64
- }
65
-
66
- return nil
67
-}
68
-
69
-func (m migration) Revert(opts migrate.Options) error {
70
- repo := mfsr.RepoPath(opts.Path)
71
- if err := repo.CheckVersion("2"); err != nil {
72
- return err
73
- }
74
-
75
- // 1) Move directory back to .go-ipfs
76
- npath, err := reverseIpfsDir(opts.Path)
77
- if err != nil {
78
- return err
79
- }
80
-
81
- // 2) move blocks back from flatfs to leveldb
82
- err = transferBlocksFromFlatDB(npath)
83
- if err != nil {
84
- return err
85
- }
86
-
87
- // 3) change version number back down
88
- repo = mfsr.RepoPath(npath)
89
- err = repo.WriteVersion("1")
90
- if err != nil {
91
- return err
92
- }
93
-
94
- return nil
95
-}
96
-
97
-// sanityChecks performs a set of tests to make sure the migration will go
98
-// smoothly
99
-func sanityChecks(opts migrate.Options) error {
100
- npath := strings.Replace(opts.Path, ".go-ipfs", ".ipfs", 1)
101
-
102
- // make sure we can move the repo from .go-ipfs to .ipfs
103
- err := os.Mkdir(npath, 0777)
104
- if err != nil {
105
- return err
106
- }
107
-
108
- // we can? good, remove it now
109
- err = os.Remove(npath)
110
- if err != nil {
111
- // this is weird... not worth continuing
112
- return err
113
- }
114
-
115
- return nil
116
-}
117
-
118
-func transferBlocksToFlatDB(repopath string) error {
119
- ldbpath := path.Join(repopath, "datastore")
120
- ldb, err := leveldb.NewDatastore(ldbpath, nil)
121
- if err != nil {
122
- return err
123
- }
124
-
125
- blockspath := path.Join(repopath, "blocks")
126
- err = os.Mkdir(blockspath, 0777)
127
- if err != nil && !os.IsExist(err) {
128
- return err
129
- }
130
-
131
- fds, err := flatfs.New(blockspath, 4)
132
- if err != nil {
133
- return err
134
- }
135
-
136
- return transferBlocks(ldb, fds, "/b/", "")
137
-}
138
-
139
-func transferBlocksFromFlatDB(repopath string) error {
140
-
141
- ldbpath := path.Join(repopath, "datastore")
142
- blockspath := path.Join(repopath, "blocks")
143
- fds, err := flatfs.New(blockspath, 4)
144
- if err != nil {
145
- return err
146
- }
147
-
148
- ldb, err := leveldb.NewDatastore(ldbpath, nil)
149
- if err != nil {
150
- return err
151
- }
152
-
153
- err = transferBlocks(fds, ldb, "", "/b/")
154
- if err != nil {
155
- return err
156
- }
157
-
158
- // Now remove the blocks directory
159
- err = os.RemoveAll(blockspath)
160
- if err != nil {
161
- return err
162
- }
163
-
164
- return nil
165
-}
166
-
167
-func transferBlocks(from, to dstore.Datastore, fpref, tpref string) error {
168
- q := dsq.Query{Prefix: fpref, KeysOnly: true}
169
- res, err := from.Query(q)
170
- if err != nil {
171
- return err
172
- }
173
-
174
- fmt.Println("Starting query")
175
- for result := range res.Next() {
176
- nkey := fmt.Sprintf("%s%s", tpref, result.Key[len(fpref):])
177
-
178
- fkey := dstore.NewKey(result.Key)
179
- val, err := from.Get(fkey)
180
- if err != nil {
181
- return err
182
- }
183
-
184
- err = to.Put(dstore.NewKey(nkey), val)
185
- if err != nil {
186
- return err
187
- }
188
-
189
- err = from.Delete(fkey)
190
- if err != nil {
191
- return err
192
- }
193
- }
194
- fmt.Println("Query done")
195
-
196
- return nil
197
-}
198
-
199
-func moveIpfsDir(curpath string) (string, error) {
200
- newpath := strings.Replace(curpath, ".go-ipfs", ".ipfs", 1)
201
- return newpath, os.Rename(curpath, newpath)
202
-}
203
-
204
-func reverseIpfsDir(curpath string) (string, error) {
205
- newpath := strings.Replace(curpath, ".ipfs", ".go-ipfs", 1)
206
- return newpath, os.Rename(curpath, newpath)
207
-}
208
-
209
-func loadConfigJSON(repoPath string) (map[string]interface{}, error) {
210
- cfgPath := path.Join(repoPath, "config")
211
- fi, err := os.Open(cfgPath)
212
- if err != nil {
213
- return nil, err
214
- }
215
-
216
- var out map[string]interface{}
217
- err = json.NewDecoder(fi).Decode(&out)
218
- if err != nil {
219
- return nil, err
220
- }
221
-
222
- return out, nil
223
-}
224
-
225
-func saveConfigJSON(repoPath string, cfg map[string]interface{}) error {
226
- cfgPath := path.Join(repoPath, "config")
227
- fi, err := os.Create(cfgPath)
228
- if err != nil {
229
- return err
230
- }
231
-
232
- out, err := json.MarshalIndent(cfg, "", "\t")
233
- if err != nil {
234
- return err
235
- }
236
-
237
- _, err = fi.Write(out)
238
- if err != nil {
239
- return err
240
- }
241
-
242
- return nil
243
-}
244
-
245
-func main() {
246
- m := migration{}
247
- migrate.Main(&m)
248
-}
repo/fsrepo/migrations/mfsr.go
deleted
-56
@@ -1,56 +0,0 @@
1
-package mfsr
2
-
3
-import (
4
- "errors"
5
- "fmt"
6
- "io/ioutil"
7
- "os"
8
- "path"
9
- "strings"
10
-)
11
-
12
-const VersionFile = "version"
13
-
14
-type RepoPath string
15
-
16
-func (rp RepoPath) VersionFile() string {
17
- return path.Join(string(rp), VersionFile)
18
-}
19
-
20
-func (rp RepoPath) Version() (string, error) {
21
- if rp == "" {
22
- return "", fmt.Errorf("invalid repo path \"%s\"", rp)
23
- }
24
-
25
- fn := rp.VersionFile()
26
- if _, err := os.Stat(fn); os.IsNotExist(err) {
27
- return "", errors.New("no version file in repo at " + string(rp))
28
- }
29
-
30
- c, err := ioutil.ReadFile(fn)
31
- if err != nil {
32
- return "", err
33
- }
34
-
35
- s := string(c)
36
- s = strings.TrimSpace(s)
37
- return s, nil
38
-}
39
-
40
-func (rp RepoPath) CheckVersion(version string) error {
41
- v, err := rp.Version()
42
- if err != nil {
43
- return err
44
- }
45
-
46
- if v != version {
47
- return fmt.Errorf("versions differ (expected: %s, actual:%s)", version, v)
48
- }
49
-
50
- return nil
51
-}
52
-
53
-func (rp RepoPath) WriteVersion(version string) error {
54
- fn := rp.VersionFile()
55
- return ioutil.WriteFile(fn, []byte(version+"\n"), 0644)
56
-}