Skip to content

Commit 9eedf93

Browse files
Merge pull request #47 from ipfs/feat/4-to-5
migration from 4 to 5
2 parents 1e510aa + 964ea3a commit 9eedf93

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+3730
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
fs-repo-migrations
22
ipfs-0-to-1/ipfs-0-to-1
33
ipfs-1-to-2/ipfs-1-to-2
4+
sharness/bin/*

go-migrate/cli.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ import (
77
)
88

99
type Flags struct {
10-
Force bool
11-
Revert bool
12-
Path string // file path to migrate for fs based migrations
13-
Verbose bool
14-
Help bool
10+
Force bool
11+
Revert bool
12+
Path string // file path to migrate for fs based migrations
13+
Verbose bool
14+
Help bool
15+
NoRevert bool
1516
}
1617

1718
func (f *Flags) Setup() {
@@ -20,6 +21,11 @@ func (f *Flags) Setup() {
2021
flag.BoolVar(&f.Verbose, "verbose", false, "enable verbose logging")
2122
flag.BoolVar(&f.Help, "help", false, "display help message")
2223
flag.StringVar(&f.Path, "path", "", "file path to migrate for fs based migrations (required)")
24+
flag.BoolVar(&f.NoRevert, "no-revert", false, "do not attempt to automatically revert on failure")
25+
}
26+
27+
var SupportNoRevert = map[string]bool{
28+
"4-to-5": true,
2329
}
2430

2531
func (f *Flags) Parse() {
@@ -43,13 +49,17 @@ func Run(m Migration) error {
4349

4450
if !m.Reversible() {
4551
if f.Revert {
46-
return fmt.Errorf("migration %d is irreversible", m.Versions())
52+
return fmt.Errorf("migration %s is irreversible", m.Versions())
4753
}
4854
if !f.Force {
49-
return fmt.Errorf("migration %d is irreversible (use -f to proceed)", m.Versions())
55+
return fmt.Errorf("migration %s is irreversible (use -f to proceed)", m.Versions())
5056
}
5157
}
5258

59+
if f.NoRevert && !SupportNoRevert[m.Versions()] {
60+
return fmt.Errorf("migration %s does not support the '-no-revert' option", m.Versions())
61+
}
62+
5363
if f.Revert {
5464
return m.Revert(Options{
5565
Flags: f,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.swp
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.2.0: QmRWDav6mzWseLWeYfVd5fvUKiVe9xNH29YfMF438fG364
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
language: go
2+
3+
go:
4+
- 1.7.3
5+
- tip
6+
7+
script:
8+
- make test
9+
10+
env: TEST_NO_FUSE=1 TEST_VERBOSE=1

ipfs-4-to-5/go-datastore/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License
2+
3+
Copyright (c) 2016 Juan Batiz-Benet
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

ipfs-4-to-5/go-datastore/Makefile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
test: deps
2+
go test -race -v ./...
3+
4+
export IPFS_API ?= v04x.ipfs.io
5+
6+
gx:
7+
go get -u github.com/whyrusleeping/gx
8+
go get -u github.com/whyrusleeping/gx-go
9+
10+
deps: gx
11+
gx --verbose install --global
12+
gx-go rewrite
13+
go get -t ./...
14+

ipfs-4-to-5/go-datastore/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# go-datastore
2+
3+
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)
4+
[![](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://ipfs.io/)
5+
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
6+
[![standard-readme compliant](https://img.shields.io/badge/standard--readme-OK-green.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme)
7+
8+
> key-value datastore interfaces
9+
10+
datastore is a generic layer of abstraction for data store and database access. It is a simple API with the aim to enable application development in a datastore-agnostic way, allowing datastores to be swapped seamlessly without changing application code. Thus, one can leverage different datastores with different strengths without committing the application to one datastore throughout its lifetime.
11+
12+
In addition, grouped datastores significantly simplify interesting data access patterns (such as caching and sharding).
13+
14+
Based on [datastore.py](https://github.com/datastore/datastore).
15+
16+
## Documentation
17+
18+
https://godoc.org/github.com/jbenet/go-datastore
19+
20+
## Contribute
21+
22+
Feel free to join in. All welcome. Open an [issue](https://github.com/ipfs/NAME/issues)!
23+
24+
This repository falls under the IPFS [Code of Conduct](https://github.com/ipfs/community/blob/master/code-of-conduct.md).
25+
26+
### Want to hack on IPFS?
27+
28+
[![](https://cdn.rawgit.com/jbenet/contribute-ipfs-gif/master/img/contribute.gif)](https://github.com/ipfs/community/blob/master/contributing.md)
29+
30+
## License
31+
32+
MIT
33+
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
package datastore
2+
3+
import (
4+
"io"
5+
"log"
6+
7+
dsq "github.com/ipfs/fs-repo-migrations/ipfs-4-to-5/go-datastore/query"
8+
)
9+
10+
// Here are some basic datastore implementations.
11+
12+
type keyMap map[Key]interface{}
13+
14+
// MapDatastore uses a standard Go map for internal storage.
15+
type MapDatastore struct {
16+
values keyMap
17+
}
18+
19+
// NewMapDatastore constructs a MapDatastore
20+
func NewMapDatastore() (d *MapDatastore) {
21+
return &MapDatastore{
22+
values: keyMap{},
23+
}
24+
}
25+
26+
// Put implements Datastore.Put
27+
func (d *MapDatastore) Put(key Key, value interface{}) (err error) {
28+
d.values[key] = value
29+
return nil
30+
}
31+
32+
// Get implements Datastore.Get
33+
func (d *MapDatastore) Get(key Key) (value interface{}, err error) {
34+
val, found := d.values[key]
35+
if !found {
36+
return nil, ErrNotFound
37+
}
38+
return val, nil
39+
}
40+
41+
// Has implements Datastore.Has
42+
func (d *MapDatastore) Has(key Key) (exists bool, err error) {
43+
_, found := d.values[key]
44+
return found, nil
45+
}
46+
47+
// Delete implements Datastore.Delete
48+
func (d *MapDatastore) Delete(key Key) (err error) {
49+
if _, found := d.values[key]; !found {
50+
return ErrNotFound
51+
}
52+
delete(d.values, key)
53+
return nil
54+
}
55+
56+
// Query implements Datastore.Query
57+
func (d *MapDatastore) Query(q dsq.Query) (dsq.Results, error) {
58+
re := make([]dsq.Entry, 0, len(d.values))
59+
for k, v := range d.values {
60+
re = append(re, dsq.Entry{Key: k.String(), Value: v})
61+
}
62+
r := dsq.ResultsWithEntries(q, re)
63+
r = dsq.NaiveQueryApply(q, r)
64+
return r, nil
65+
}
66+
67+
func (d *MapDatastore) Batch() (Batch, error) {
68+
return NewBasicBatch(d), nil
69+
}
70+
71+
func (d *MapDatastore) Close() error {
72+
return nil
73+
}
74+
75+
// NullDatastore stores nothing, but conforms to the API.
76+
// Useful to test with.
77+
type NullDatastore struct {
78+
}
79+
80+
// NewNullDatastore constructs a null datastoe
81+
func NewNullDatastore() *NullDatastore {
82+
return &NullDatastore{}
83+
}
84+
85+
// Put implements Datastore.Put
86+
func (d *NullDatastore) Put(key Key, value interface{}) (err error) {
87+
return nil
88+
}
89+
90+
// Get implements Datastore.Get
91+
func (d *NullDatastore) Get(key Key) (value interface{}, err error) {
92+
return nil, nil
93+
}
94+
95+
// Has implements Datastore.Has
96+
func (d *NullDatastore) Has(key Key) (exists bool, err error) {
97+
return false, nil
98+
}
99+
100+
// Delete implements Datastore.Delete
101+
func (d *NullDatastore) Delete(key Key) (err error) {
102+
return nil
103+
}
104+
105+
// Query implements Datastore.Query
106+
func (d *NullDatastore) Query(q dsq.Query) (dsq.Results, error) {
107+
return dsq.ResultsWithEntries(q, nil), nil
108+
}
109+
110+
func (d *NullDatastore) Batch() (Batch, error) {
111+
return NewBasicBatch(d), nil
112+
}
113+
114+
func (d *NullDatastore) Close() error {
115+
return nil
116+
}
117+
118+
// LogDatastore logs all accesses through the datastore.
119+
type LogDatastore struct {
120+
Name string
121+
child Datastore
122+
}
123+
124+
// Shim is a datastore which has a child.
125+
type Shim interface {
126+
Datastore
127+
128+
Children() []Datastore
129+
}
130+
131+
// NewLogDatastore constructs a log datastore.
132+
func NewLogDatastore(ds Datastore, name string) *LogDatastore {
133+
if len(name) < 1 {
134+
name = "LogDatastore"
135+
}
136+
return &LogDatastore{Name: name, child: ds}
137+
}
138+
139+
// Children implements Shim
140+
func (d *LogDatastore) Children() []Datastore {
141+
return []Datastore{d.child}
142+
}
143+
144+
// Put implements Datastore.Put
145+
func (d *LogDatastore) Put(key Key, value interface{}) (err error) {
146+
log.Printf("%s: Put %s\n", d.Name, key)
147+
// log.Printf("%s: Put %s ```%s```", d.Name, key, value)
148+
return d.child.Put(key, value)
149+
}
150+
151+
// Get implements Datastore.Get
152+
func (d *LogDatastore) Get(key Key) (value interface{}, err error) {
153+
log.Printf("%s: Get %s\n", d.Name, key)
154+
return d.child.Get(key)
155+
}
156+
157+
// Has implements Datastore.Has
158+
func (d *LogDatastore) Has(key Key) (exists bool, err error) {
159+
log.Printf("%s: Has %s\n", d.Name, key)
160+
return d.child.Has(key)
161+
}
162+
163+
// Delete implements Datastore.Delete
164+
func (d *LogDatastore) Delete(key Key) (err error) {
165+
log.Printf("%s: Delete %s\n", d.Name, key)
166+
return d.child.Delete(key)
167+
}
168+
169+
// Query implements Datastore.Query
170+
func (d *LogDatastore) Query(q dsq.Query) (dsq.Results, error) {
171+
log.Printf("%s: Query\n", d.Name)
172+
return d.child.Query(q)
173+
}
174+
175+
func (d *LogDatastore) Batch() (Batch, error) {
176+
log.Printf("%s: Batch\n", d.Name)
177+
if bds, ok := d.child.(Batching); ok {
178+
return bds.Batch()
179+
}
180+
return nil, ErrBatchUnsupported
181+
}
182+
183+
func (d *LogDatastore) Close() error {
184+
log.Printf("%s: Close\n", d.Name)
185+
if cds, ok := d.child.(io.Closer); ok {
186+
return cds.Close()
187+
}
188+
return nil
189+
}

ipfs-4-to-5/go-datastore/batch.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package datastore
2+
3+
// basicBatch implements the transaction interface for datastores who do
4+
// not have any sort of underlying transactional support
5+
type basicBatch struct {
6+
puts map[Key]interface{}
7+
deletes map[Key]struct{}
8+
9+
target Datastore
10+
}
11+
12+
func NewBasicBatch(ds Datastore) Batch {
13+
return &basicBatch{
14+
puts: make(map[Key]interface{}),
15+
deletes: make(map[Key]struct{}),
16+
target: ds,
17+
}
18+
}
19+
20+
func (bt *basicBatch) Put(key Key, val interface{}) error {
21+
bt.puts[key] = val
22+
return nil
23+
}
24+
25+
func (bt *basicBatch) Delete(key Key) error {
26+
bt.deletes[key] = struct{}{}
27+
return nil
28+
}
29+
30+
func (bt *basicBatch) Commit() error {
31+
for k, val := range bt.puts {
32+
if err := bt.target.Put(k, val); err != nil {
33+
return err
34+
}
35+
}
36+
37+
for k, _ := range bt.deletes {
38+
if err := bt.target.Delete(k); err != nil {
39+
return err
40+
}
41+
}
42+
43+
return nil
44+
}

0 commit comments

Comments
 (0)