mirror of
https://github.com/hanzoai/migrate.git
synced 2026-09-30 22:55:10 +00:00
go fix applied the modernizers. One rewrite it got wrong is corrected here: a reverse loop over runes became a range over slices.Backward that bound the index and the value to the SAME name, which does not compile and would have compared a rune to an index if it had. The value gets its own name. Co-authored-by: Hanzo Dev <[email protected]>
97 lines
1.9 KiB
Go
97 lines
1.9 KiB
Go
package stub
|
|
|
|
import (
|
|
"io"
|
|
"reflect"
|
|
"sync/atomic"
|
|
|
|
"github.com/golang-migrate/migrate/v4/database"
|
|
)
|
|
|
|
func init() {
|
|
database.Register("stub", &Stub{})
|
|
}
|
|
|
|
type Stub struct {
|
|
Url string
|
|
Instance any
|
|
CurrentVersion int
|
|
MigrationSequence []string
|
|
LastRunMigration []byte // todo: make []string
|
|
IsDirty bool
|
|
isLocked atomic.Bool
|
|
|
|
Config *Config
|
|
}
|
|
|
|
func (s *Stub) Open(url string) (database.Driver, error) {
|
|
return &Stub{
|
|
Url: url,
|
|
CurrentVersion: database.NilVersion,
|
|
MigrationSequence: make([]string, 0),
|
|
Config: &Config{},
|
|
}, nil
|
|
}
|
|
|
|
type Config struct{}
|
|
|
|
func WithInstance(instance any, config *Config) (database.Driver, error) {
|
|
return &Stub{
|
|
Instance: instance,
|
|
CurrentVersion: database.NilVersion,
|
|
MigrationSequence: make([]string, 0),
|
|
Config: config,
|
|
}, nil
|
|
}
|
|
|
|
func (s *Stub) Close() error {
|
|
return nil
|
|
}
|
|
|
|
func (s *Stub) Lock() error {
|
|
if !s.isLocked.CompareAndSwap(false, true) {
|
|
return database.ErrLocked
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *Stub) Unlock() error {
|
|
if !s.isLocked.CompareAndSwap(true, false) {
|
|
return database.ErrNotLocked
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *Stub) Run(migration io.Reader) error {
|
|
m, err := io.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 = database.NilVersion
|
|
s.LastRunMigration = nil
|
|
s.MigrationSequence = append(s.MigrationSequence, DROP)
|
|
return nil
|
|
}
|
|
|
|
func (s *Stub) EqualSequence(seq []string) bool {
|
|
return reflect.DeepEqual(seq, s.MigrationSequence)
|
|
}
|