Skip to content

Commit

Permalink
migrations: Move migration parsing to new package (#427)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleconroy authored Apr 2, 2020
1 parent 2c051a9 commit 0f922ee
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 19 deletions.
6 changes: 3 additions & 3 deletions internal/dinosql/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"unicode"

"github.com/kyleconroy/sqlc/internal/catalog"
"github.com/kyleconroy/sqlc/internal/migrations"
core "github.com/kyleconroy/sqlc/internal/pg"
"github.com/kyleconroy/sqlc/internal/postgres"
"github.com/kyleconroy/sqlc/internal/postgresql/ast"
Expand Down Expand Up @@ -86,8 +87,7 @@ func ReadSQLFiles(path string) ([]string, error) {
if strings.HasPrefix(filepath.Base(filename), ".") {
continue
}
// Remove golang-migrate rollback files.
if strings.HasSuffix(filename, ".down.sql") {
if migrations.IsDown(filename) {
continue
}
sql = append(sql, filename)
Expand All @@ -109,7 +109,7 @@ func ParseCatalog(schema string) (core.Catalog, error) {
merr.Add(filename, "", 0, err)
continue
}
contents := RemoveRollbackStatements(string(blob))
contents := migrations.RemoveRollbackStatements(string(blob))
tree, err := pg.Parse(contents)
if err != nil {
merr.Add(filename, contents, 0, err)
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dinosql
package migrations

import (
"bufio"
Expand Down Expand Up @@ -27,3 +27,8 @@ func RemoveRollbackStatements(contents string) string {
}
return strings.Join(lines, "\n")
}

func IsDown(filename string) bool {
// Remove golang-migrate rollback files.
return strings.HasSuffix(filename, ".down.sql")
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package dinosql
package migrations

import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)

const inputGoose = `
Expand Down Expand Up @@ -60,21 +59,19 @@ func TestRemoveRollback(t *testing.T) {
}

func TestRemoveGolangMigrateRollback(t *testing.T) {
want := []string{
filenames := map[string]bool{
// make sure we let through golang-migrate files that aren't rollbacks
"testdata/migrations/1.up.sql",
"migrations/1.up.sql": false,
// make sure we let through other sql files
"testdata/migrations/2.sql",
"testdata/migrations/foo.sql",
"migrations/2.sql": false,
"migrations/foo.sql": false,
"migrations/1.down.sql": true,
}

got, err := ReadSQLFiles("./testdata/migrations")
if err != nil {
t.Fatal(err)
}

less := func(a, b string) bool { return a < b }
if diff := cmp.Diff(want, got, cmpopts.SortSlices(less)); diff != "" {
t.Errorf("golang-migrate filtering mismatch: \n %s", diff)
for filename, want := range filenames {
got := IsDown(filename)
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("IsDown mismatch: %s\n %s", filename, diff)
}
}
}
3 changes: 2 additions & 1 deletion internal/mysql/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/kyleconroy/sqlc/internal/config"
"github.com/kyleconroy/sqlc/internal/dinosql"
"github.com/kyleconroy/sqlc/internal/migrations"
)

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

0 comments on commit 0f922ee

Please sign in to comment.