Skip to content

Commit f266a24

Browse files
kyleconroyuraldav
andauthored
Tern (#286)
* add support for tern migration tool (#284) Co-authored-by: Kyle Conroy <[email protected]> * dinosql/migrate: Fix tern tests Co-authored-by: Ural Davletshin <[email protected]>
1 parent 046cb81 commit f266a24

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

docs/migrations.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ sqlc will ignore rollback statements when parsing migration SQL files. The follo
44

55
- [goose](https://github.com/pressly/goose)
66
- [sql-migrate](https://github.com/rubenv/sql-migrate)
7+
- [tern](https://github.com/jackc/tern)
78

89
## goose
910

@@ -50,3 +51,20 @@ type People struct {
5051
ID int32
5152
}
5253
```
54+
55+
### tern
56+
57+
```sql
58+
CREATE TABLE comment (id int NOT NULL, text text NOT NULL);
59+
---- create above / drop below ----
60+
DROP TABLE comment;
61+
```
62+
63+
```go
64+
package db
65+
66+
type Comment struct {
67+
ID int32
68+
Text string
69+
}
70+
```

internal/dinosql/migrations.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
//
1010
// goose: -- +goose Down
1111
// sql-migrate: -- +migrate Down
12+
// tern: ---- create above / drop below ----
1213
func RemoveRollbackStatements(contents string) string {
1314
s := bufio.NewScanner(strings.NewReader(contents))
1415
var lines []string
@@ -19,6 +20,9 @@ func RemoveRollbackStatements(contents string) string {
1920
if strings.HasPrefix(s.Text(), "-- +migrate Down") {
2021
break
2122
}
23+
if strings.HasPrefix(s.Text(), "---- create above / drop below ----") {
24+
break
25+
}
2226
lines = append(lines, s.Text())
2327
}
2428
return strings.Join(lines, "\n")

internal/dinosql/migrations_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,25 @@ const outputMigrate = `
3535
CREATE TABLE people (id int);
3636
`
3737

38+
const inputTern = `
39+
-- Write your migrate up statements here
40+
ALTER TABLE todo RENAME COLUMN done TO is_done;
41+
---- create above / drop below ----
42+
ALTER TABLE todo RENAME COLUMN is_done TO done;
43+
`
44+
45+
const outputTern = `
46+
-- Write your migrate up statements here
47+
ALTER TABLE todo RENAME COLUMN done TO is_done;`
48+
3849
func TestRemoveRollback(t *testing.T) {
3950
if diff := cmp.Diff(outputGoose, RemoveRollbackStatements(inputGoose)); diff != "" {
4051
t.Errorf("goose migration mismatch:\n%s", diff)
4152
}
4253
if diff := cmp.Diff(outputMigrate, RemoveRollbackStatements(inputMigrate)); diff != "" {
4354
t.Errorf("sql-migrate migration mismatch:\n%s", diff)
4455
}
56+
if diff := cmp.Diff(outputTern, RemoveRollbackStatements(inputTern)); diff != "" {
57+
t.Errorf("tern migration mismatch:\n%s", diff)
58+
}
4559
}

0 commit comments

Comments
 (0)