-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmigration_default.go
More file actions
126 lines (111 loc) · 3.54 KB
/
migration_default.go
File metadata and controls
126 lines (111 loc) · 3.54 KB
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
package migration
import (
"fmt"
"path"
"regexp"
"runtime"
"time"
)
// BaseMigration is the default structure that all base migrations returns.
type BaseMigration struct {
id time.Time
description string
}
// GetID returns the ID of the migration.
func (m *BaseMigration) GetID() time.Time {
return m.id
}
// GetDescription returns the ID of the migration.
func (m *BaseMigration) GetDescription() string {
return m.description
}
// DefaultMigration is the default implementation of the migration.Migration.
//
// It is designed to provide a coded implementaiton of a migration. It receives
// an up and down anonymous methods to be ran while executing the migration.
//
// This implementation is used by the migration.CodeSource implemenation of the
// migration.Source.
type DefaultMigration struct {
BaseMigration
do Handler
undo Handler
manager Manager
}
// Handler is the signature of the up and down methods that a migration
// will receive.
type Handler func(executionContext interface{}) error
// NewMigration returns a new instance of migration.Migration with all the
// required properties initialized.
//
// If a handler is provided it will assigned to the Up method. If a second is
// provided, it will be assigned to the Down method.
func NewMigration(id time.Time, description string, handlers ...Handler) *DefaultMigration {
var do, undo Handler
if len(handlers) > 0 {
do = handlers[0]
}
if len(handlers) > 1 {
undo = handlers[1]
}
return &DefaultMigration{
BaseMigration: BaseMigration{
id: id,
description: description,
},
do: do,
undo: undo,
}
}
var codeMigrationRegex = regexp.MustCompile("^([0-9]{4}[0-9]{2}[0-9]{2}[0-9]{2}[0-9]{2}[0-9]{2})_(.*).go$")
// CodeMigrationDateFormat is the format understood by the CodeMigration
var CodeMigrationDateFormat = "20060102150405"
// NewCodeMigration uses the regex to extract data NewMigration.
//
// It uses the `NewCodeMigrationCustom`.
func NewCodeMigration(handlers ...Handler) *DefaultMigration {
m := NewCodeMigrationCustom(1, handlers...)
DefaultCodeSource().Register(m)
return m
}
// NewCodeMigrationCustom uses the regex to extract data NewMigration.
//
// If a handler is provided it will assigned to the Up method. If a second is
// provided, it will be assigned to the Down method.
func NewCodeMigrationCustom(skip int, handlers ...Handler) *DefaultMigration {
_, file, _, ok := runtime.Caller(1 + skip)
if ok {
groups := codeMigrationRegex.FindStringSubmatch(path.Base(file))
if len(groups) == 3 {
id, err := time.Parse(CodeMigrationDateFormat, groups[1])
if err != nil {
panic(fmt.Sprintf("the file name '%s' has an invalid datetime", file))
}
m := NewMigration(id, groups[2], handlers...)
return m
}
panic(fmt.Sprintf("the file name '%s' has an invalid format", file))
} else {
panic(fmt.Sprintf("the file name '%s' has an invalid format", file))
}
}
// Do calls the up action of the migration.
func (m *DefaultMigration) Do(executionContext interface{}) error {
return m.do(executionContext)
}
// Undo calls the down action of the migration.
func (m *DefaultMigration) Undo(executionContext interface{}) error {
return m.undo(executionContext)
}
// GetManager returns the reference of the manager that is executing the
// migration.
func (m *DefaultMigration) GetManager() Manager {
return m.manager
}
// SetManager set the reference of the manager that is executing the migration.
//
// It returns itself for sugar syntax.
func (m *DefaultMigration) SetManager(manager Manager) Migration {
m.manager = manager
return m
}