Skip to content

Tern #286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 22, 2020
Merged

Tern #286

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions docs/migrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,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)

## goose

Expand Down Expand Up @@ -50,3 +51,20 @@ type People struct {
ID int32
}
```

### tern

```sql
CREATE TABLE comment (id int NOT NULL, text text NOT NULL);
---- create above / drop below ----
DROP TABLE comment;
```

```go
package db

type Comment struct {
ID int32
Text string
}
```
4 changes: 4 additions & 0 deletions internal/dinosql/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
//
// goose: -- +goose Down
// sql-migrate: -- +migrate Down
// tern: ---- create above / drop below ----
func RemoveRollbackStatements(contents string) string {
s := bufio.NewScanner(strings.NewReader(contents))
var lines []string
Expand All @@ -19,6 +20,9 @@ func RemoveRollbackStatements(contents string) string {
if strings.HasPrefix(s.Text(), "-- +migrate Down") {
break
}
if strings.HasPrefix(s.Text(), "---- create above / drop below ----") {
break
}
lines = append(lines, s.Text())
}
return strings.Join(lines, "\n")
Expand Down
14 changes: 14 additions & 0 deletions internal/dinosql/migrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,25 @@ const outputMigrate = `
CREATE TABLE people (id int);
`

const inputTern = `
-- Write your migrate up statements here
ALTER TABLE todo RENAME COLUMN done TO is_done;
---- create above / drop below ----
ALTER TABLE todo RENAME COLUMN is_done TO done;
`

const outputTern = `
-- Write your migrate up statements here
ALTER TABLE todo RENAME COLUMN done TO is_done;`

func TestRemoveRollback(t *testing.T) {
if diff := cmp.Diff(outputGoose, RemoveRollbackStatements(inputGoose)); diff != "" {
t.Errorf("goose migration mismatch:\n%s", diff)
}
if diff := cmp.Diff(outputMigrate, RemoveRollbackStatements(inputMigrate)); diff != "" {
t.Errorf("sql-migrate migration mismatch:\n%s", diff)
}
if diff := cmp.Diff(outputTern, RemoveRollbackStatements(inputTern)); diff != "" {
t.Errorf("tern migration mismatch:\n%s", diff)
}
}