github.com/matcornic/migrate@v3.3.2-0.20180717234201-feea45c20506+incompatible/database/stub/stub.go (about) 1 package stub 2 3 import ( 4 "io" 5 "io/ioutil" 6 "reflect" 7 8 "github.com/golang-migrate/migrate/database" 9 ) 10 11 func init() { 12 database.Register("stub", &Stub{}) 13 } 14 15 type Stub struct { 16 Url string 17 Instance interface{} 18 CurrentVersion int 19 MigrationSequence []string 20 LastRunMigration []byte // todo: make []string 21 IsDirty bool 22 IsLocked bool 23 24 Config *Config 25 } 26 27 func (s *Stub) Open(url string) (database.Driver, error) { 28 return &Stub{ 29 Url: url, 30 CurrentVersion: -1, 31 MigrationSequence: make([]string, 0), 32 Config: &Config{}, 33 }, nil 34 } 35 36 type Config struct{} 37 38 func WithInstance(instance interface{}, config *Config) (database.Driver, error) { 39 return &Stub{ 40 Instance: instance, 41 CurrentVersion: -1, 42 MigrationSequence: make([]string, 0), 43 Config: config, 44 }, nil 45 } 46 47 func (s *Stub) Close() error { 48 return nil 49 } 50 51 func (s *Stub) Lock() error { 52 if s.IsLocked { 53 return database.ErrLocked 54 } 55 s.IsLocked = true 56 return nil 57 } 58 59 func (s *Stub) Unlock() error { 60 s.IsLocked = false 61 return nil 62 } 63 64 func (s *Stub) Run(migration io.Reader) error { 65 m, err := ioutil.ReadAll(migration) 66 if err != nil { 67 return err 68 } 69 s.LastRunMigration = m 70 s.MigrationSequence = append(s.MigrationSequence, string(m[:])) 71 return nil 72 } 73 74 func (s *Stub) SetVersion(version int, state bool) error { 75 s.CurrentVersion = version 76 s.IsDirty = state 77 return nil 78 } 79 80 func (s *Stub) Version() (version int, dirty bool, err error) { 81 return s.CurrentVersion, s.IsDirty, nil 82 } 83 84 const DROP = "DROP" 85 86 func (s *Stub) Drop() error { 87 s.CurrentVersion = -1 88 s.LastRunMigration = nil 89 s.MigrationSequence = append(s.MigrationSequence, DROP) 90 return nil 91 } 92 93 func (s *Stub) EqualSequence(seq []string) bool { 94 return reflect.DeepEqual(seq, s.MigrationSequence) 95 }