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