-
Notifications
You must be signed in to change notification settings - Fork 4
/
source.go
92 lines (75 loc) · 2.27 KB
/
source.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
package gloat
import (
"io/ioutil"
"os"
"path/filepath"
)
// Source is an interface representing a migrations source.
type Source interface {
Collect() (Migrations, error)
}
// FileSystemSource is a file system source of migrations. The migrations are
// stored in folders with the following structure:
//
// migrations/
// └── 20170329154959_introduce_domain_model
// ├── down.sql
// └── up.sql
type FileSystemSource struct {
Dir string
}
// Collect builds migrations stored in a folder like the following structure:
//
// migrations/
// └── 20170329154959_introduce_domain_model
// ├── down.sql
// └── up.sql
func (s *FileSystemSource) Collect() (migrations Migrations, err error) {
err = filepath.Walk(s.Dir, func(path string, info os.FileInfo, err error) error {
if info != nil && info.IsDir() && path != s.Dir {
migration, err := MigrationFromBytes(path, ioutil.ReadFile)
if err != nil {
return err
}
migrations = append(migrations, migration)
}
return nil
})
migrations.Sort()
return
}
// NewFileSystemSource creates a new source of migrations that takes them right
// out of the file system.
func NewFileSystemSource(dir string) Source {
return &FileSystemSource{Dir: dir}
}
// AssetSource is a go-bindata migration source for binary embedded migrations.
// You need to pass a prefix, the Asset and AssetDir functions, go-bindata
// generates.
type AssetSource struct {
Prefix string
Asset func(string) ([]byte, error)
AssetDir func(string) ([]string, error)
}
// Collect builds migrations from a go-bindata embedded migrations.
func (s *AssetSource) Collect() (migrations Migrations, err error) {
dirs, err := s.AssetDir(s.Prefix)
if err != nil {
return
}
for _, path := range dirs {
var migration *Migration
migration, err = MigrationFromBytes(filepath.Join(s.Prefix, path), s.Asset)
if err != nil {
return
}
migrations = append(migrations, migration)
}
migrations.Sort()
return
}
// NewAssetSource creates a new source of binary migrations embedded into the
// program with go-bindata.
func NewAssetSource(prefix string, asset func(string) ([]byte, error), assetDir func(string) ([]string, error)) Source {
return &AssetSource{Prefix: prefix, Asset: asset, AssetDir: assetDir}
}