Skip to content

Commit ada53d9

Browse files
authored
internal/dinosql: Ignore sql-migrate rollbacks (#160)
1 parent 19c7fcd commit ada53d9

File tree

8 files changed

+124
-70
lines changed

8 files changed

+124
-70
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ Your favorite PostgreSQL / Go features are supported:
249249
- [ALTER TABLE](./examples/alter_table.md)
250250
- Go
251251
- [JSON struct tags](./examples/json_tags.md)
252-
- [Goose migrations](./examples/goose.md)
252+
- [Migration tools](./examples/migrations.md)
253253

254254
A full, end-to-end example can be found in the sample
255255
[`ondeck`](./internal/dinosql/testdata/ondeck) package.

examples/goose.md

Lines changed: 0 additions & 27 deletions
This file was deleted.

examples/migrations.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Migrations
2+
3+
sqlc will ignore rollback statements when parsing migration SQL files. The following tools are current supported:
4+
5+
- [goose](https://github.com/pressly/goose)
6+
- [sql-migrate](https://github.com/rubenv/sql-migrate)
7+
8+
## goose
9+
10+
```sql
11+
-- +goose Up
12+
CREATE TABLE post (
13+
id int NOT NULL,
14+
title text,
15+
body text,
16+
PRIMARY KEY(id)
17+
);
18+
19+
-- +goose Down
20+
DROP TABLE post;
21+
```
22+
23+
```go
24+
package db
25+
26+
type Post struct {
27+
ID int
28+
Title sql.NullString
29+
Body sql.NullString
30+
}
31+
```
32+
33+
### sql-migrate
34+
35+
```sql
36+
-- +migrate Up
37+
-- SQL in section 'Up' is executed when this migration is applied
38+
CREATE TABLE people (id int);
39+
40+
41+
-- +migrate Down
42+
-- SQL section 'Down' is executed when this migration is rolled back
43+
DROP TABLE people;
44+
```
45+
46+
```go
47+
package db
48+
49+
type People struct {
50+
ID int32
51+
}
52+
```

internal/dinosql/goose.go

Lines changed: 0 additions & 15 deletions
This file was deleted.

internal/dinosql/goose_test.go

Lines changed: 0 additions & 26 deletions
This file was deleted.

internal/dinosql/migrations.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package dinosql
2+
3+
import (
4+
"bufio"
5+
"strings"
6+
)
7+
8+
// Remove all lines after a rollback comment.
9+
//
10+
// goose: -- +goose Down
11+
// sql-migrate: -- +migrate Down
12+
func RemoveRollbackStatements(contents string) string {
13+
s := bufio.NewScanner(strings.NewReader(contents))
14+
var lines []string
15+
for s.Scan() {
16+
if strings.HasPrefix(s.Text(), "-- +goose Down") {
17+
break
18+
}
19+
if strings.HasPrefix(s.Text(), "-- +migrate Down") {
20+
break
21+
}
22+
lines = append(lines, s.Text())
23+
}
24+
return strings.Join(lines, "\n")
25+
}

internal/dinosql/migrations_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package dinosql
2+
3+
import (
4+
"testing"
5+
6+
"github.com/google/go-cmp/cmp"
7+
)
8+
9+
const inputGoose = `
10+
-- +goose Up
11+
ALTER TABLE archived_jobs ADD COLUMN expires_at TIMESTAMP WITH TIME ZONE;
12+
13+
-- +goose Down
14+
ALTER TABLE archived_jobs DROP COLUMN expires_at;
15+
`
16+
17+
const outputGoose = `
18+
-- +goose Up
19+
ALTER TABLE archived_jobs ADD COLUMN expires_at TIMESTAMP WITH TIME ZONE;
20+
`
21+
22+
const inputMigrate = `
23+
-- +migrate Up
24+
-- SQL in section 'Up' is executed when this migration is applied
25+
CREATE TABLE people (id int);
26+
27+
-- +migrate Down
28+
-- SQL section 'Down' is executed when this migration is rolled back
29+
DROP TABLE people;
30+
`
31+
32+
const outputMigrate = `
33+
-- +migrate Up
34+
-- SQL in section 'Up' is executed when this migration is applied
35+
CREATE TABLE people (id int);
36+
`
37+
38+
func TestRemoveRollback(t *testing.T) {
39+
if diff := cmp.Diff(outputGoose, RemoveRollbackStatements(inputGoose)); diff != "" {
40+
t.Errorf("goose migration mismatch:\n%s", diff)
41+
}
42+
if diff := cmp.Diff(outputMigrate, RemoveRollbackStatements(inputMigrate)); diff != "" {
43+
t.Errorf("sql-migrate migration mismatch:\n%s", diff)
44+
}
45+
}

internal/dinosql/parser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func ParseCatalog(schema string) (core.Catalog, error) {
9191
merr.Add(filename, "", 0, err)
9292
continue
9393
}
94-
contents := RemoveGooseRollback(string(blob))
94+
contents := RemoveRollbackStatements(string(blob))
9595
tree, err := pg.Parse(contents)
9696
if err != nil {
9797
merr.Add(filename, contents, 0, err)

0 commit comments

Comments
 (0)