diff --git a/docs/migrations.md b/docs/migrations.md index 33bdb4347f..83d0ba9a91 100644 --- a/docs/migrations.md +++ b/docs/migrations.md @@ -5,6 +5,7 @@ sqlc will ignore rollback statements when parsing migration SQL files. The follo - [goose](https://github.com/pressly/goose) - [sql-migrate](https://github.com/rubenv/sql-migrate) - [tern](https://github.com/jackc/tern) +- [golang-migrate](https://github.com/golang-migrate/migrate) ## goose @@ -67,4 +68,32 @@ type Comment struct { ID int32 Text string } -``` \ No newline at end of file +``` + +## golang-migrate + +In `20060102.up.sql`: + +```sql +CREATE TABLE post ( + id int NOT NULL, + title text, + body text, + PRIMARY KEY(id) +); +``` + +In `20060102.down.sql`: +```sql +DROP TABLE post; +``` + +```go +package db + +type Post struct { + ID int + Title sql.NullString + Body sql.NullString +} +``` diff --git a/internal/dinosql/migrations_test.go b/internal/dinosql/migrations_test.go index 5d03da66b9..223156d33e 100644 --- a/internal/dinosql/migrations_test.go +++ b/internal/dinosql/migrations_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" ) const inputGoose = ` @@ -57,3 +58,23 @@ func TestRemoveRollback(t *testing.T) { t.Errorf("tern migration mismatch:\n%s", diff) } } + +func TestRemoveGolangMigrateRollback(t *testing.T) { + want := []string{ + // make sure we let through golang-migrate files that aren't rollbacks + "testdata/migrations/1.up.sql", + // make sure we let through other sql files + "testdata/migrations/2.sql", + "testdata/migrations/foo.sql", + } + + 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) + } +} diff --git a/internal/dinosql/parser.go b/internal/dinosql/parser.go index 381f415007..06d18ebcdd 100644 --- a/internal/dinosql/parser.go +++ b/internal/dinosql/parser.go @@ -86,6 +86,10 @@ func ReadSQLFiles(path string) ([]string, error) { if strings.HasPrefix(filepath.Base(filename), ".") { continue } + // Remove golang-migrate rollback files. + if strings.HasSuffix(filename, ".down.sql") { + continue + } sql = append(sql, filename) } return sql, nil diff --git a/internal/dinosql/testdata/migrations/1.down.sql b/internal/dinosql/testdata/migrations/1.down.sql new file mode 100644 index 0000000000..e69de29bb2 diff --git a/internal/dinosql/testdata/migrations/1.up.sql b/internal/dinosql/testdata/migrations/1.up.sql new file mode 100644 index 0000000000..e69de29bb2 diff --git a/internal/dinosql/testdata/migrations/2.down.sql b/internal/dinosql/testdata/migrations/2.down.sql new file mode 100644 index 0000000000..e69de29bb2 diff --git a/internal/dinosql/testdata/migrations/2.sql b/internal/dinosql/testdata/migrations/2.sql new file mode 100644 index 0000000000..e69de29bb2 diff --git a/internal/dinosql/testdata/migrations/foo.sql b/internal/dinosql/testdata/migrations/foo.sql new file mode 100644 index 0000000000..e69de29bb2