Skip to content

Commit 0f922ee

Browse files
authored
migrations: Move migration parsing to new package (#427)
1 parent 2c051a9 commit 0f922ee

File tree

9 files changed

+22
-19
lines changed

9 files changed

+22
-19
lines changed

internal/dinosql/parser.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"unicode"
1414

1515
"github.com/kyleconroy/sqlc/internal/catalog"
16+
"github.com/kyleconroy/sqlc/internal/migrations"
1617
core "github.com/kyleconroy/sqlc/internal/pg"
1718
"github.com/kyleconroy/sqlc/internal/postgres"
1819
"github.com/kyleconroy/sqlc/internal/postgresql/ast"
@@ -86,8 +87,7 @@ func ReadSQLFiles(path string) ([]string, error) {
8687
if strings.HasPrefix(filepath.Base(filename), ".") {
8788
continue
8889
}
89-
// Remove golang-migrate rollback files.
90-
if strings.HasSuffix(filename, ".down.sql") {
90+
if migrations.IsDown(filename) {
9191
continue
9292
}
9393
sql = append(sql, filename)
@@ -109,7 +109,7 @@ func ParseCatalog(schema string) (core.Catalog, error) {
109109
merr.Add(filename, "", 0, err)
110110
continue
111111
}
112-
contents := RemoveRollbackStatements(string(blob))
112+
contents := migrations.RemoveRollbackStatements(string(blob))
113113
tree, err := pg.Parse(contents)
114114
if err != nil {
115115
merr.Add(filename, contents, 0, err)

internal/dinosql/testdata/migrations/1.down.sql

Whitespace-only changes.

internal/dinosql/testdata/migrations/1.up.sql

Whitespace-only changes.

internal/dinosql/testdata/migrations/2.down.sql

Whitespace-only changes.

internal/dinosql/testdata/migrations/2.sql

Whitespace-only changes.

internal/dinosql/testdata/migrations/foo.sql

Whitespace-only changes.

internal/dinosql/migrations.go renamed to internal/migrations/migrations.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package dinosql
1+
package migrations
22

33
import (
44
"bufio"
@@ -27,3 +27,8 @@ func RemoveRollbackStatements(contents string) string {
2727
}
2828
return strings.Join(lines, "\n")
2929
}
30+
31+
func IsDown(filename string) bool {
32+
// Remove golang-migrate rollback files.
33+
return strings.HasSuffix(filename, ".down.sql")
34+
}

internal/dinosql/migrations_test.go renamed to internal/migrations/migrations_test.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
package dinosql
1+
package migrations
22

33
import (
44
"testing"
55

66
"github.com/google/go-cmp/cmp"
7-
"github.com/google/go-cmp/cmp/cmpopts"
87
)
98

109
const inputGoose = `
@@ -60,21 +59,19 @@ func TestRemoveRollback(t *testing.T) {
6059
}
6160

6261
func TestRemoveGolangMigrateRollback(t *testing.T) {
63-
want := []string{
62+
filenames := map[string]bool{
6463
// make sure we let through golang-migrate files that aren't rollbacks
65-
"testdata/migrations/1.up.sql",
64+
"migrations/1.up.sql": false,
6665
// make sure we let through other sql files
67-
"testdata/migrations/2.sql",
68-
"testdata/migrations/foo.sql",
66+
"migrations/2.sql": false,
67+
"migrations/foo.sql": false,
68+
"migrations/1.down.sql": true,
6969
}
7070

71-
got, err := ReadSQLFiles("./testdata/migrations")
72-
if err != nil {
73-
t.Fatal(err)
74-
}
75-
76-
less := func(a, b string) bool { return a < b }
77-
if diff := cmp.Diff(want, got, cmpopts.SortSlices(less)); diff != "" {
78-
t.Errorf("golang-migrate filtering mismatch: \n %s", diff)
71+
for filename, want := range filenames {
72+
got := IsDown(filename)
73+
if diff := cmp.Diff(want, got); diff != "" {
74+
t.Errorf("IsDown mismatch: %s\n %s", filename, diff)
75+
}
7976
}
8077
}

internal/mysql/parse.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/kyleconroy/sqlc/internal/config"
1313
"github.com/kyleconroy/sqlc/internal/dinosql"
14+
"github.com/kyleconroy/sqlc/internal/migrations"
1415
)
1516

1617
// Query holds the data for walking and validating mysql querys
@@ -44,7 +45,7 @@ func parsePath(sqlPath string, generator PackageGenerator) (*Result, error) {
4445
if err != nil {
4546
parseErrors.Add(filename, "", 0, err)
4647
}
47-
contents := dinosql.RemoveRollbackStatements(string(blob))
48+
contents := migrations.RemoveRollbackStatements(string(blob))
4849
if err != nil {
4950
parseErrors.Add(filename, "", 0, err)
5051
continue

0 commit comments

Comments
 (0)