-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsource_code.go
More file actions
61 lines (52 loc) · 1.86 KB
/
source_code.go
File metadata and controls
61 lines (52 loc) · 1.86 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
package migration
import "fmt"
// CodeSource is migration.Source implementation. It provides the development
// of migrations using Golang code.
//
// TODO add examples
type CodeSource struct {
migrations []Migration
}
var defaultCode *CodeSource
func init() {
defaultCode = NewCodeSource()
}
// DefaultCodeSource implements a singleton of the default CodeSource. It enable
// the developer to use the migration.Register without needing to deal with the
// migration.NewCodeSource.
//
// On normal situations only one CodeSource is enough through the whole system.
// And many sources are needed, the developer can use multiple instances of the
// migration.CodeSource and use the migration.CodeSource.Register, instead of
// using the default implementation migration.Register.
func DefaultCodeSource() *CodeSource {
return defaultCode
}
// NewCodeSource returns a new instance of a migration.CodeSource.
func NewCodeSource() *CodeSource {
return &CodeSource{
migrations: make([]Migration, 0),
}
}
// List implements the migration.Source.List by listing all the registered
// migrations of this instance.
func (s *CodeSource) List() ([]Migration, error) {
return s.migrations, nil
}
// Register registers the migration for further use.
func (s *CodeSource) Register(migration Migration) {
for i := 0; i < len(s.migrations); i++ {
if s.migrations[i].GetID() == migration.GetID() {
panic(fmt.Sprintf("Migrations %s and %s have the same ID", s.migrations[i].GetDescription(), migration.GetDescription()))
}
if s.migrations[i].GetID().After(migration.GetID()) {
s.migrations = append(s.migrations[:i], append([]Migration{migration}, s.migrations[i:]...)...)
return
}
}
s.migrations = append(s.migrations, migration)
}
// Register registers the migration on the migration.DefaultCodeSource instance.
func Register(migration Migration) {
defaultCode.Register(migration)
}