Skip to content

Commit 4ac3d6b

Browse files
authored
exclude golang-migrate .down.sql files in dirs + single files (#445)
1 parent 378497d commit 4ac3d6b

File tree

2 files changed

+68
-5
lines changed

2 files changed

+68
-5
lines changed

internal/dinosql/parser.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ func (e *ParserErr) Error() string {
6060
return fmt.Sprintf("multiple errors: %d errors", len(e.Errs))
6161
}
6262

63+
func addFile(files []string, newFile string) []string {
64+
if migrations.IsDown(newFile) {
65+
return files
66+
}
67+
return append(files, newFile)
68+
}
69+
6370
func ReadSQLFiles(paths []string) ([]string, error) {
6471
var files []string
6572
for _, path := range paths {
@@ -74,13 +81,10 @@ func ReadSQLFiles(paths []string) ([]string, error) {
7481
return nil, err
7582
}
7683
for _, f := range listing {
77-
files = append(files, filepath.Join(path, f.Name()))
84+
files = addFile(files, filepath.Join(path, f.Name()))
7885
}
7986
} else {
80-
files = append(files, path)
81-
}
82-
if migrations.IsDown(path) {
83-
continue
87+
files = addFile(files, path)
8488
}
8589
}
8690
return files, nil

internal/dinosql/parser_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package dinosql
22

33
import (
4+
"io/ioutil"
5+
"os"
6+
"path"
47
"testing"
58

69
"github.com/google/go-cmp/cmp"
@@ -184,3 +187,59 @@ func TestExpand(t *testing.T) {
184187
t.Errorf("mismatch:\nexpected: %s\n acutal: %s", expected, actual)
185188
}
186189
}
190+
191+
func TestReadFiles(t *testing.T) {
192+
dir, err := ioutil.TempDir("", "sqlc-test-read-files")
193+
if err != nil {
194+
t.Error(err)
195+
}
196+
defer os.RemoveAll(dir)
197+
198+
subdir1 := path.Join(dir, "subdir1")
199+
if err := os.Mkdir(subdir1, 0777); err != nil {
200+
t.Error(err)
201+
}
202+
203+
subdir2 := path.Join(dir, "subdir2")
204+
if err := os.Mkdir(subdir2, 0777); err != nil {
205+
t.Error(err)
206+
}
207+
208+
files := []string{
209+
path.Join(subdir1, "include-me.sql"),
210+
path.Join(subdir1, "include-me.up.sql"),
211+
path.Join(subdir1, "not-me.down.sql"),
212+
path.Join(subdir2, "include-me.sql"),
213+
path.Join(subdir2, "include-me.up.sql"),
214+
path.Join(subdir2, "not-me.down.sql"),
215+
}
216+
for _, filename := range files {
217+
fd, err := os.Create(filename)
218+
if err != nil {
219+
t.Error(err)
220+
}
221+
defer fd.Close()
222+
}
223+
224+
input := []string{
225+
subdir1,
226+
path.Join(subdir2, "include-me.sql"),
227+
path.Join(subdir2, "include-me.up.sql"),
228+
path.Join(subdir2, "not-me.down.sql"),
229+
}
230+
231+
expectedFiles := []string{
232+
path.Join(subdir1, "include-me.sql"),
233+
path.Join(subdir1, "include-me.up.sql"),
234+
path.Join(subdir2, "include-me.sql"),
235+
path.Join(subdir2, "include-me.up.sql"),
236+
}
237+
238+
filesRead, err := ReadSQLFiles(input)
239+
if err != nil {
240+
t.Error(err)
241+
}
242+
if !cmp.Equal(expectedFiles, filesRead) {
243+
t.Errorf("unexpected files: %s", cmp.Diff(expectedFiles, filesRead))
244+
}
245+
}

0 commit comments

Comments
 (0)