forked from golang-migrate/migrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stub.go
85 lines (71 loc) · 2.12 KB
/
stub.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
package stub
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"github.com/golang-migrate/migrate/source"
)
func init() {
source.Register("stub", &Stub{})
}
type Config struct{}
// d, _ := source.Open("stub://")
// d.(*stub.Stub).Migrations =
type Stub struct {
Url string
Instance interface{}
Migrations *source.Migrations
Config *Config
}
func (s *Stub) Open(url string) (source.Driver, error) {
return &Stub{
Url: url,
Migrations: source.NewMigrations(),
Config: &Config{},
}, nil
}
func WithInstance(instance interface{}, config *Config) (source.Driver, error) {
return &Stub{
Instance: instance,
Migrations: source.NewMigrations(),
Config: config,
}, nil
}
func (s *Stub) Close() error {
return nil
}
func (s *Stub) First() (version uint, err error) {
if v, ok := s.Migrations.First(); !ok {
return 0, &os.PathError{"first", s.Url, os.ErrNotExist} // TODO: s.Url can be empty when called with WithInstance
} else {
return v, nil
}
}
func (s *Stub) Prev(version uint) (prevVersion uint, err error) {
if v, ok := s.Migrations.Prev(version); !ok {
return 0, &os.PathError{fmt.Sprintf("prev for version %v", version), s.Url, os.ErrNotExist}
} else {
return v, nil
}
}
func (s *Stub) Next(version uint) (nextVersion uint, err error) {
if v, ok := s.Migrations.Next(version); !ok {
return 0, &os.PathError{fmt.Sprintf("next for version %v", version), s.Url, os.ErrNotExist}
} else {
return v, nil
}
}
func (s *Stub) ReadUp(version uint) (r io.ReadCloser, identifier string, err error) {
if m, ok := s.Migrations.Up(version); ok {
return ioutil.NopCloser(bytes.NewBufferString(m.Identifier)), fmt.Sprintf("%v.up.stub", version), nil
}
return nil, "", &os.PathError{fmt.Sprintf("read up version %v", version), s.Url, os.ErrNotExist}
}
func (s *Stub) ReadDown(version uint) (r io.ReadCloser, identifier string, err error) {
if m, ok := s.Migrations.Down(version); ok {
return ioutil.NopCloser(bytes.NewBufferString(m.Identifier)), fmt.Sprintf("%v.down.stub", version), nil
}
return nil, "", &os.PathError{fmt.Sprintf("read down version %v", version), s.Url, os.ErrNotExist}
}