/
parse.go
59 lines (54 loc) · 1.43 KB
/
parse.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
package sqlparser
import (
"fmt"
"io/fs"
"go.uber.org/multierr"
"golang.org/x/sync/errgroup"
)
type ParsedSQL struct {
UseTx bool
Up, Down []string
}
func ParseAllFromFS(fsys fs.FS, filename string, debug bool) (*ParsedSQL, error) {
parsedSQL := new(ParsedSQL)
// TODO(mf): parse is called twice, once for up and once for down. This is inefficient. It
// should be possible to parse both directions in one pass. Also, UseTx is set once (but
// returned twice), which is unnecessary and potentially error-prone if the two calls to
// parseSQL disagree based on direction.
var g errgroup.Group
g.Go(func() error {
up, useTx, err := parse(fsys, filename, DirectionUp, debug)
if err != nil {
return err
}
parsedSQL.Up = up
parsedSQL.UseTx = useTx
return nil
})
g.Go(func() error {
down, _, err := parse(fsys, filename, DirectionDown, debug)
if err != nil {
return err
}
parsedSQL.Down = down
return nil
})
if err := g.Wait(); err != nil {
return nil, err
}
return parsedSQL, nil
}
func parse(fsys fs.FS, filename string, direction Direction, debug bool) (_ []string, _ bool, retErr error) {
r, err := fsys.Open(filename)
if err != nil {
return nil, false, err
}
defer func() {
retErr = multierr.Append(retErr, r.Close())
}()
stmts, useTx, err := ParseSQLMigration(r, direction, debug)
if err != nil {
return nil, false, fmt.Errorf("failed to parse %s: %w", filename, err)
}
return stmts, useTx, nil
}