-
Notifications
You must be signed in to change notification settings - Fork 15
/
mock.go
67 lines (52 loc) · 1.16 KB
/
mock.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
package migration
import (
"errors"
"strings"
"github.com/Boostport/migration/parser"
)
type mockDriver struct {
applied []string
}
func (m *mockDriver) Close() error {
return nil
}
func (m *mockDriver) Migrate(migration *PlannedMigration) error {
var migrationStatements *parser.ParsedMigration
if migration.Direction == Up {
migrationStatements = migration.Up
} else {
migrationStatements = migration.Down
}
errStatement := ""
if len(migrationStatements.Statements) > 0 {
errStatement = migrationStatements.Statements[0]
}
if strings.Contains(errStatement, "error") {
return errors.New("error executing migration")
}
versionIndex := -1
for i, version := range m.applied {
if version == migration.ID {
versionIndex = i
break
}
}
if migration.Direction == Up {
if versionIndex == -1 {
m.applied = append(m.applied, migration.ID)
}
} else {
if versionIndex != -1 {
m.applied = append(m.applied[:versionIndex], m.applied[versionIndex+1:]...)
}
}
return nil
}
func (m *mockDriver) Versions() ([]string, error) {
return m.applied, nil
}
func getMockDriver() *mockDriver {
return &mockDriver{
applied: []string{},
}
}