-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrationlister.go
More file actions
65 lines (53 loc) · 1.64 KB
/
migrationlister.go
File metadata and controls
65 lines (53 loc) · 1.64 KB
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
package migrate
import (
"embed"
"path/filepath"
)
// Lister is an interface that defines a method for listing
// the contents of the underlying data source.
type Lister interface {
List() ([]string, error)
}
// StringMigrations is a slice of plain string migration script queries to be applied.
type StringMigrations []string
func (s StringMigrations) List() ([]string, error) {
return s, nil
}
// EmbeddedMigrations wraps the [embed.FS] and the path to the migration scripts directory.
type EmbeddedMigrations struct {
FS embed.FS
Path string
}
// List returns a list of migration script queries from the embedded file system.
//
// It reads migration scripts from the directory specified
// in the [EmbeddedMigrations.Path] field within the embedded file system [EmbeddedMigrations.FS]
// and returns them as a slice of strings.
//
// This function does not recursively read subdirectories.
//
// Queries are ordered lexicographically rather than naturally.
// For example, the files "1.sql", "2.sql", and "03.sql"
// will be read in the order: "03.sql", "1.sql", "2.sql".
//
// To ensure correct ordering, use zero-padding for numbers, e.g.,
// "001.sql", "002.sql", "003.sql".
func (e EmbeddedMigrations) List() ([]string, error) {
files, err := e.FS.ReadDir(e.Path)
if err != nil {
return nil, errf("reading embedded migration directory: %v", err)
}
ss := make([]string, 0, len(files))
for _, f := range files {
if f.IsDir() {
continue
}
p := filepath.Join(e.Path, f.Name())
s, err := e.FS.ReadFile(p)
if err != nil {
return nil, errf("reading embedded migration file: %v", err)
}
ss = append(ss, string(s))
}
return ss, nil
}