Skip to content
Merged
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
15 changes: 15 additions & 0 deletions goose.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package dinosql

import "strings"

// Remove all lines after a `-- +goose Down` comment
func RemoveGooseRollback(contents string) string {
lines := strings.Split(contents, "\n")
for i, line := range lines {
if strings.HasPrefix(strings.TrimSpace(line), "-- +goose Down") {
lines = lines[:i]
break
}
}
return strings.Join(lines, "\n")
}
26 changes: 26 additions & 0 deletions goose_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package dinosql

import (
"testing"

"github.com/google/go-cmp/cmp"
)

const inputMigration = `
-- +goose Up
ALTER TABLE archived_jobs ADD COLUMN expires_at TIMESTAMP WITH TIME ZONE;

-- +goose Down
ALTER TABLE archived_jobs DROP COLUMN expires_at;
`

const outputMigration = `
-- +goose Up
ALTER TABLE archived_jobs ADD COLUMN expires_at TIMESTAMP WITH TIME ZONE;
`

func TestRemoveGooseRollback(t *testing.T) {
if diff := cmp.Diff(outputMigration, RemoveGooseRollback(inputMigration)); diff != "" {
t.Errorf("migration mismatch:\n%s", diff)
}
}
3 changes: 2 additions & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ func ParseSchmea(dir string) (*postgres.Schema, error) {
if err != nil {
return nil, err
}
tree, err := pg.Parse(string(blob))
contents := RemoveGooseRollback(string(blob))
tree, err := pg.Parse(contents)
if err != nil {
return nil, err
}
Expand Down