-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigration.go
71 lines (59 loc) · 1.93 KB
/
migration.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
package migrate
import (
"io/ioutil"
"path"
"path/filepath"
"strconv"
)
// Migration represents a single migration, most importantly containing its
// version number and all the Parts contained within it.
type Migration struct {
Name string
Path string
Version int
Parts []*Part
}
// NewMigration takes a directory path and parses the version number contained
// within the directory name component. It loops through this directory
// checking for files with the .sql extension, parsing them into Parts.
// NewMigration returns a pointer to a Migration if successful and an error if
// anything goes wrong.
func NewMigration(root string) (*Migration, error) {
_, name := filepath.Split(root)
if len(name) < 9 || name[:8] != "version_" {
return nil, NewFatalf("NewMigration: expected migration directory name to be formatted as "+
"'version_<number>', got '%s'", name)
}
// Parse the name component of the directory for the migration version
// number, ignoring `version_` prefix in the first eight characters
version, err := strconv.Atoi(name[8:])
if err != nil {
return nil, err
}
if version == 0 {
return nil, NewFatalf("NewMigration: got disallowed migration version '0', reserved to represent " +
"the initial state of the database")
}
root = filepath.Clean(root)
migration := &Migration{Name: name, Path: root, Version: version}
files, err := ioutil.ReadDir(root)
if err != nil {
return nil, err
}
for _, file := range files {
// if the file has a .sql extension, add it to the Migration
if !file.IsDir() && filepath.Ext(file.Name()) == ".sql" {
filePath := path.Join(root, file.Name())
part, err := NewPart(filePath)
if err != nil {
return nil, err
}
migration.Parts = append(migration.Parts, part)
}
}
// if no parts were added, return an error
if len(migration.Parts) == 0 {
return nil, NewFatalf("NewMigration: no migration parts found in '%s'", root)
}
return migration, nil
}