-
Notifications
You must be signed in to change notification settings - Fork 5
/
file.go
76 lines (67 loc) · 2.33 KB
/
file.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
package schema
import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
)
// MigrationIDFromFilename removes directory paths and extensions
// from the filename to make a friendlier Migration ID
func MigrationIDFromFilename(filename string) string {
return strings.TrimSuffix(filepath.Base(filename), filepath.Ext(filename))
}
// MigrationsFromDirectoryPath retrieves a slice of Migrations from the
// contents of the directory. Only .sql files are read
func MigrationsFromDirectoryPath(dirPath string) (migrations []*Migration, err error) {
migrations = make([]*Migration, 0)
// Assemble a glob of the .sql files in the directory. This can
// only fail if the dirPath itself contains invalid glob characters
filenames, err := filepath.Glob(filepath.Join(dirPath, "*.sql"))
if err != nil {
return migrations, err
}
// Friendly failure: if the user provides a valid-looking, but nonexistent
// directory, we want to error instead of returning an empty set
if _, err := os.Stat(dirPath); os.IsNotExist(err) {
return migrations, fmt.Errorf("migrations directory does not exist: %w", err)
}
for _, filename := range filenames {
migration, err := MigrationFromFilePath(filename)
if err != nil {
return migrations, err
}
migrations = append(migrations, migration)
}
return
}
// MigrationFromFilePath creates a Migration from a path on disk
func MigrationFromFilePath(filename string) (migration *Migration, err error) {
migration = &Migration{}
migration.ID = MigrationIDFromFilename(filename)
contents, err := ioutil.ReadFile(path.Clean(filename))
if err != nil {
return migration, fmt.Errorf("failed to read migration from '%s': %w", filename, err)
}
migration.Script = string(contents)
return migration, err
}
// File wraps the standard library io.Read and os.File.Name methods
type File interface {
Name() string
Read(b []byte) (n int, err error)
}
// MigrationFromFile builds a migration by reading from an open File-like
// object. The migration's ID will be based on the file's name. The file
// will *not* be closed after being read.
func MigrationFromFile(file File) (migration *Migration, err error) {
migration = &Migration{}
migration.ID = MigrationIDFromFilename(file.Name())
content, err := ioutil.ReadAll(file)
if err != nil {
return migration, err
}
migration.Script = string(content)
return migration, err
}