-
Notifications
You must be signed in to change notification settings - Fork 0
/
fofm.go
311 lines (253 loc) · 7.23 KB
/
fofm.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package fofm
import (
"fmt"
"reflect"
"regexp"
"runtime"
"strconv"
"strings"
"time"
)
const (
up = "up"
down = "down"
migration_prefix = "Migration"
STATUS_SUCCESS = "success"
STATUS_FAILURE = "failure"
)
type FunctionalMigration interface {
// GetMigrationsPath this should return the directory where your migration
// manager lives. This path is used when new migration files are created
GetMigrationsPath() string
// GetPackageName should return the name of the package where your migration
// manager lives. This is sued when migration files are created
GetPackageName() string
}
// BaseMigration provides an embed struct to easily adhere to the FunctionalMigration interface
type BaseMigration struct{}
func (b BaseMigration) GetMigrationsPath() string {
// get the path of the file where this is called from
_, curFile, _, _ := runtime.Caller(1)
parts := strings.Split(curFile, "/")
return strings.Join(parts[0:len(parts)-1], "/")
}
// New will creae a new instance of FOFM. It will apply the DefaultSettings which
// can be overwritten by passing in settings
func New(db Store, migrationInstance FunctionalMigration, settings ...Setting) (*FOFM, error) {
manager := &FOFM{
DB: db,
Migration: migrationInstance,
UpMigrations: MigrationStack{},
DownMigrations: MigrationStack{},
migrationStuctName: reflect.TypeOf(migrationInstance).Name(),
}
settings = append(DefaultSettings, settings...)
for _, setting := range settings {
err := setting(manager)
if err != nil {
return nil, fmt.Errorf(`error when calling a setting -- %w`, err)
}
}
err := manager.init()
if err != nil {
return nil, err
}
return manager, nil
}
type FOFM struct {
_ struct{}
DB Store
Migration FunctionalMigration
UpMigrations MigrationStack
DownMigrations MigrationStack
migrationStuctName string
Seeded bool
Writer WriteFile
}
func (f *FOFM) init() error {
if f.Seeded {
return nil
}
ins := reflect.TypeOf(f.Migration)
if ins.Kind() == reflect.Pointer {
ins = ins.Elem()
}
for i := 0; i < ins.NumMethod(); i++ {
method := ins.Method(i)
name := method.Name
mTime, direction, err := MigrationNameParts(name)
if err != nil {
continue
}
switch direction {
case up:
f.UpMigrations.Add(name, direction, mTime)
case down:
f.DownMigrations.Add(name, direction, mTime)
}
}
err := f.DB.CreateStore()
if err != nil {
return err
}
f.UpMigrations.Order()
f.DownMigrations.Reverse()
f.Seeded = true
return nil
}
func (m *FOFM) ClearStore() error {
return m.DB.ClearStore()
}
// GetNextMigrationTemplate will return a migration template and its unix time
func (m *FOFM) GetNextMigrationTemplate() (string, int64) {
now := time.Now().Unix()
sName := m.migrationStuctName
template := fmt.Sprintf(`package %s
func (i %s) Migration_%v_up() error {
// up migration here
return nil
}
func (i %s) Migration_%v_down() error {
// down migration here
return nil
}
`, m.Migration.GetPackageName(), sName, now, sName, now)
return template, now
}
// CreateMigration will create a new migration template based on the current unix time
// and it will call the defined Writer (which is a file writer by default)
func (m *FOFM) CreateMigration() (string, error) {
template, now := m.GetNextMigrationTemplate()
fileName := fmt.Sprintf(`migration_%v.go`, now)
fullPath := fmt.Sprintf(`%s/%s`, m.Migration.GetMigrationsPath(), fileName)
b := []byte(template)
err := m.Writer(fullPath, b, 0644)
if err != nil {
return "", err
}
return fullPath, nil
}
// Status returns a list of all migrations and all of the times when they've been run
// since migrations can be run mulitple times
func (m *FOFM) Status() (MigrationSetStatus, error) {
status := MigrationSetStatus{}
for _, mig := range m.UpMigrations {
all, err := m.DB.GetAllByName(mig.Name)
if err != nil {
return status, err
}
status.Migrations = append(status.Migrations, Status{
Migration: mig,
Runs: all.ToRuns(),
})
}
return status, nil
}
func (m *FOFM) run(names ...string) error {
for _, name := range names {
if strings.TrimSpace(name) == "" {
return nil
}
ret := reflect.ValueOf(m.Migration).MethodByName(name).Call([]reflect.Value{})
err, _ := ret[0].Interface().(error)
mig := Migration{
Name: name,
Status: STATUS_SUCCESS,
Timestamp: time.Now().UTC(),
}
if err != nil {
err := fmt.Errorf(`error running: %v -- %v`, name, err)
mig.Status = STATUS_FAILURE
m.DB.Save(mig, err)
return err
}
err = m.DB.Save(mig, nil)
if err != nil {
return err
}
}
return nil
}
// Latest will run all up migrations between the last migration that was run and the
// latest defined migration. If the last migration was a failure, it will attempt to rerun it
// if the last run migration is the latest and it was successful, it will return an error. If that
// migration was a failure, it will attempt to rerun it
func (m *FOFM) Latest() error {
lastRun, err := m.DB.LastRun()
if err != nil {
if _, ok := err.(NoResultsError); !ok {
return err
}
}
if lastRun != nil {
switch lastRun.Status {
case STATUS_SUCCESS:
// do not run anything if the lastRun is actually the latest migration
if last := m.UpMigrations.Last(); last != nil {
if lastRun.Name == last.Name {
return nil
}
}
// the last run should be the next one after the successful run
after := m.UpMigrations.After(lastRun)
if len(after) > 1 {
lastRun = &after[1]
} else {
lastRun = nil
}
case STATUS_FAILURE:
lastRun, err = m.DB.LastStatusRun(STATUS_SUCCESS)
if err != nil {
if _, ok := err.(NoResultsError); !ok {
return err
}
}
}
}
toRun := m.UpMigrations.After(lastRun)
return m.run(toRun.Names()...)
}
// UP will run all migrations, in order, up to and inclduing the named one passed in
func (m *FOFM) Up(name string) error {
// ensure that the latest migration with the name arg
// was not successful
latest, err := m.DB.LastRunByName(name)
if err == nil && latest != nil {
if latest.Status == STATUS_SUCCESS {
return fmt.Errorf(`migration "%s" latest run was successful. will not run again`, name)
}
}
toRun := m.UpMigrations.BeforeName(name)
return m.run(toRun.Names()...)
}
// Down will run all migrations, in reverse order, up to and including the named one
// passed in
func (m *FOFM) Down(name string) error {
toRun := m.DownMigrations.BeforeName(name)
return m.run(toRun.Names()...)
}
// utility funcs
func MigrationNameParts(name string) (timestamp time.Time, direction string, err error) {
parts := strings.Split(name, "_")
if len(parts) != 3 {
err = fmt.Errorf(`incorrect name: %v must be in the format of Migration_1658164360_up`, name)
return
}
if parts[0] != migration_prefix {
err = fmt.Errorf(`non-migration method %v`, name)
return
}
direction = parts[2]
var ts int64
ts, err = strconv.ParseInt(parts[1], 10, 64)
timestamp = time.Unix(ts, 0)
return
}
var fileTime = regexp.MustCompile("[\\d]+")
func MigrationFileNameTime(name string) (timestamp time.Time, err error) {
fTime := fileTime.FindString(name)
var ts int64
ts, err = strconv.ParseInt(fTime, 10, 64)
timestamp = time.Unix(ts, 0)
return
}