-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource.go
77 lines (62 loc) · 1.91 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
package goembedmigrationsource
import (
"bytes"
"embed"
"fmt"
"io/ioutil"
"sort"
"strings"
migrate "github.com/rubenv/sql-migrate"
)
// EmbedFileSystemMigrationSource implements migrate.MigrationSource interface for embed.FS.
type EmbedFileSystemMigrationSource struct {
FileSystem embed.FS
Dir string
}
// FindMigrations returns a collection of migrations based on found .sql files in the FS.
func (f EmbedFileSystemMigrationSource) FindMigrations() ([]*migrate.Migration, error) {
return f.findMigrations()
}
type byID []*migrate.Migration
func (b byID) Len() int { return len(b) }
func (b byID) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func (b byID) Less(i, j int) bool { return b[i].Less(b[j]) }
func (f EmbedFileSystemMigrationSource) findMigrations() ([]*migrate.Migration, error) {
migrations := make([]*migrate.Migration, 0)
dir, err := f.FileSystem.ReadDir(f.Dir)
if err != nil {
return nil, err
}
for _, file := range dir {
if file.IsDir() {
continue
}
if strings.HasSuffix(file.Name(), ".sql") {
migration, err := f.migrationFromFile(file.Name())
if err != nil {
return nil, err
}
migrations = append(migrations, migration)
}
}
// Make sure migrations are sorted
sort.Sort(byID(migrations))
return migrations, nil
}
func (f EmbedFileSystemMigrationSource) migrationFromFile(filename string) (*migrate.Migration, error) {
path := fmt.Sprintf("%s/%s", f.Dir, filename)
file, err := f.FileSystem.Open(path)
if err != nil {
return nil, fmt.Errorf("Error while opening %s: %s", filename, err)
}
defer func() { _ = file.Close() }()
content, err := ioutil.ReadAll(file)
if err != nil {
return nil, fmt.Errorf("Error while reading %s: %s", filename, err)
}
migration, err := migrate.ParseMigration(filename, bytes.NewReader(content))
if err != nil {
return nil, fmt.Errorf("Error while parsing %s: %s", filename, err)
}
return migration, nil
}