migrations.go 884 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package migrations
  2. import (
  3. "bufio"
  4. "strings"
  5. )
  6. // Remove all lines after a rollback comment.
  7. //
  8. // goose: -- +goose Down
  9. // sql-migrate: -- +migrate Down
  10. // tern: ---- create above / drop below ----
  11. // dbmate: -- 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. if strings.HasPrefix(s.Text(), "---- create above / drop below ----") {
  23. break
  24. }
  25. if strings.HasPrefix(s.Text(), "-- migrate:down") {
  26. break
  27. }
  28. lines = append(lines, s.Text())
  29. }
  30. return strings.Join(lines, "\n")
  31. }
  32. func IsDown(filename string) bool {
  33. // Remove golang-migrate rollback files.
  34. return strings.HasSuffix(filename, ".down.sql")
  35. }