-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
137 lines (109 loc) · 2.84 KB
/
types.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package go_migrator
import (
"fmt"
"path/filepath"
"github.com/amanangira/go-migrator/db"
)
type MigrationDirection int
type MigrationInstruction struct {
Version int
Direction MigrationDirection
Command string
}
type Config struct {
AbsoluteMigrationsDirectory string
Debug bool
// Filename config
// TODO - allow overriding of this value via interface implementation
// TODO - Filename config made part of this config in order to allow customization in future
FileNameVersionDelimiter string
VersionIndexByDelimiter int
PartsCountByDelimiter int
FileNameFormat string
}
type Migrator struct {
config Config
driver db.DriverInterface
}
func NewConfig(
directory string,
debug bool,
) Config {
sanitizedDirectory := sanitizeDirectoryPath(directory)
return Config{
Debug: debug,
AbsoluteMigrationsDirectory: sanitizedDirectory,
FileNameVersionDelimiter: FileNameDefaultExtensionDelimiter,
VersionIndexByDelimiter: 0,
PartsCountByDelimiter: DefaultPartsCountForFileName,
FileNameFormat: DefaultFileNameFormat(),
}
}
func DefaultFileNameFormat() string {
return fmt.Sprintf(`%s%s%s%s%s`,
FileNameVersionPlaceholder,
FileNameDefaultExtensionDelimiter,
FileNameDirectionPlaceholder,
FileNameDefaultExtensionDelimiter,
FileNameExtensionPlaceholder)
}
func NewMigratorWithConfig(
config Config,
driver db.DriverInterface,
) *Migrator {
return &Migrator{
config: config,
driver: driver,
}
}
func NewMigrator(
directory string,
debug bool,
driver db.DriverInterface,
) (*Migrator, error) {
sanitizedDirectory := sanitizeDirectoryPath(directory)
config := Config{
Debug: debug,
AbsoluteMigrationsDirectory: sanitizedDirectory,
FileNameVersionDelimiter: FileNameDefaultExtensionDelimiter,
VersionIndexByDelimiter: 0,
PartsCountByDelimiter: DefaultPartsCountForFileName,
FileNameFormat: DefaultFileNameFormat(),
}
migrator := NewMigratorWithConfig(config, driver)
err := migrator.driver.Connect()
return migrator, err
}
func (m MigrationDirection) String() string {
switch m {
case MigrationDirectionUp:
return "Up"
case MigrationDirectionDown:
return "Down"
}
return ""
}
func (m MigrationDirection) IsValid() bool {
switch m {
case MigrationDirectionUp:
return true
case MigrationDirectionDown:
return true
}
return false
}
func sanitizeDirectoryPath(directory string) string {
if directory == "" {
panic(fmt.Sprintf(`invalid directory path %s`, directory))
}
directory = filepath.Clean(directory)
if directory[0:1] == "/" {
return directory
}
var err error
directory, err = filepath.Abs(directory)
if err != nil {
panic(fmt.Sprintf(`error generating absolute path for directory %s and error %s`, directory, err.Error()))
}
return directory
}