forked from golang-migrate/migrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stub.go
95 lines (78 loc) · 1.77 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
86
87
88
89
90
91
92
93
94
95
package stub
import (
"io"
"io/ioutil"
"reflect"
"github.com/golang-migrate/migrate/database"
)
func init() {
database.Register("stub", &Stub{})
}
type Stub struct {
Url string
Instance interface{}
CurrentVersion int
MigrationSequence []string
LastRunMigration []byte // todo: make []string
IsDirty bool
IsLocked bool
Config *Config
}
func (s *Stub) Open(url string) (database.Driver, error) {
return &Stub{
Url: url,
CurrentVersion: -1,
MigrationSequence: make([]string, 0),
Config: &Config{},
}, nil
}
type Config struct{}
func WithInstance(instance interface{}, config *Config) (database.Driver, error) {
return &Stub{
Instance: instance,
CurrentVersion: -1,
MigrationSequence: make([]string, 0),
Config: config,
}, nil
}
func (s *Stub) Close() error {
return nil
}
func (s *Stub) Lock() error {
if s.IsLocked {
return database.ErrLocked
}
s.IsLocked = true
return nil
}
func (s *Stub) Unlock() error {
s.IsLocked = false
return nil
}
func (s *Stub) Run(migration io.Reader) error {
m, err := ioutil.ReadAll(migration)
if err != nil {
return err
}
s.LastRunMigration = m
s.MigrationSequence = append(s.MigrationSequence, string(m[:]))
return nil
}
func (s *Stub) SetVersion(version int, state bool) error {
s.CurrentVersion = version
s.IsDirty = state
return nil
}
func (s *Stub) Version() (version int, dirty bool, err error) {
return s.CurrentVersion, s.IsDirty, nil
}
const DROP = "DROP"
func (s *Stub) Drop() error {
s.CurrentVersion = -1
s.LastRunMigration = nil
s.MigrationSequence = append(s.MigrationSequence, DROP)
return nil
}
func (s *Stub) EqualSequence(seq []string) bool {
return reflect.DeepEqual(seq, s.MigrationSequence)
}