-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigration_reader_filesystem.go
105 lines (88 loc) · 2.26 KB
/
migration_reader_filesystem.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
93
94
95
96
97
98
99
100
101
102
103
104
105
package pgutil
import (
"fmt"
"io"
"io/fs"
"net/http"
"os"
"path"
"sort"
"strconv"
"strings"
)
type FilesystemMigrationReader struct {
name string
fs fs.FS
}
func NewFilesystemMigrationReader(dirname string) MigrationReader {
return newFilesystemMigrationReader(dirname, os.DirFS(dirname))
}
func newFilesystemMigrationReader(name string, fs fs.FS) MigrationReader {
return &FilesystemMigrationReader{
name: name,
fs: fs,
}
}
func (r *FilesystemMigrationReader) ReadAll() (definitions []RawDefinition, _ error) {
root, err := http.FS(r.fs).Open("/")
if err != nil {
if os.IsNotExist(err) {
return nil, fmt.Errorf("migration directory %q does not exist", r.name)
}
return nil, err
}
defer root.Close()
entries, err := root.Readdir(0)
if err != nil {
return nil, err
}
for _, entry := range entries {
if entry.IsDir() {
if definition, ok, err := r.readDefinition(entry.Name()); err != nil {
return nil, err
} else if ok {
definitions = append(definitions, definition)
}
}
}
sort.Slice(definitions, func(i, j int) bool { return definitions[i].ID < definitions[j].ID })
return definitions, nil
}
func (r *FilesystemMigrationReader) readDefinition(dirname string) (RawDefinition, bool, error) {
upPath := path.Join(dirname, "up.sql")
downPath := path.Join(dirname, "down.sql")
upFileContents, upErr := readFile(r.fs, upPath)
downFileContents, downErr := readFile(r.fs, downPath)
if os.IsNotExist(upErr) && os.IsNotExist(downErr) {
return RawDefinition{}, false, nil
} else if upErr != nil {
return RawDefinition{}, false, upErr
} else if downErr != nil {
return RawDefinition{}, false, downErr
}
nameParts := strings.SplitN(dirname, "_", 2)
id, err := strconv.Atoi(nameParts[0])
if err != nil {
return RawDefinition{}, false, err
}
name := strings.Replace(nameParts[1], "_", " ", -1)
definition := RawDefinition{
ID: id,
Name: name,
RawUpQuery: string(upFileContents),
RawDownQuery: string(downFileContents),
}
return definition, true, nil
}
func readFile(fs fs.FS, filepath string) ([]byte, error) {
file, err := fs.Open(filepath)
if err != nil {
return nil, err
}
defer file.Close()
contents, err := io.ReadAll(file)
if err != nil {
return nil, err
}
return contents, nil
}