Skip to content

Commit 8601eec

Browse files
authored
internal/dinosql: ignore golang-migrate rollbacks (#320)
1 parent 2ef0ffe commit 8601eec

File tree

8 files changed

+55
-1
lines changed

8 files changed

+55
-1
lines changed

docs/migrations.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ sqlc will ignore rollback statements when parsing migration SQL files. The follo
55
- [goose](https://github.com/pressly/goose)
66
- [sql-migrate](https://github.com/rubenv/sql-migrate)
77
- [tern](https://github.com/jackc/tern)
8+
- [golang-migrate](https://github.com/golang-migrate/migrate)
89

910
## goose
1011

@@ -67,4 +68,32 @@ type Comment struct {
6768
ID int32
6869
Text string
6970
}
70-
```
71+
```
72+
73+
## golang-migrate
74+
75+
In `20060102.up.sql`:
76+
77+
```sql
78+
CREATE TABLE post (
79+
id int NOT NULL,
80+
title text,
81+
body text,
82+
PRIMARY KEY(id)
83+
);
84+
```
85+
86+
In `20060102.down.sql`:
87+
```sql
88+
DROP TABLE post;
89+
```
90+
91+
```go
92+
package db
93+
94+
type Post struct {
95+
ID int
96+
Title sql.NullString
97+
Body sql.NullString
98+
}
99+
```

internal/dinosql/migrations_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"testing"
55

66
"github.com/google/go-cmp/cmp"
7+
"github.com/google/go-cmp/cmp/cmpopts"
78
)
89

910
const inputGoose = `
@@ -57,3 +58,23 @@ func TestRemoveRollback(t *testing.T) {
5758
t.Errorf("tern migration mismatch:\n%s", diff)
5859
}
5960
}
61+
62+
func TestRemoveGolangMigrateRollback(t *testing.T) {
63+
want := []string{
64+
// make sure we let through golang-migrate files that aren't rollbacks
65+
"testdata/migrations/1.up.sql",
66+
// make sure we let through other sql files
67+
"testdata/migrations/2.sql",
68+
"testdata/migrations/foo.sql",
69+
}
70+
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)
79+
}
80+
}

internal/dinosql/parser.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ func ReadSQLFiles(path string) ([]string, error) {
8686
if strings.HasPrefix(filepath.Base(filename), ".") {
8787
continue
8888
}
89+
// Remove golang-migrate rollback files.
90+
if strings.HasSuffix(filename, ".down.sql") {
91+
continue
92+
}
8993
sql = append(sql, filename)
9094
}
9195
return sql, nil

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.

0 commit comments

Comments
 (0)