github.com/mg98/scriptup@v0.1.0/pkg/scriptup/migration/parse.go (about) 1 package migration 2 3 import ( 4 "errors" 5 "strings" 6 ) 7 8 var ( 9 ErrWrongOrder = errors.New("migrate up section must appear before migrate down section") 10 ErrMissingUpSection = errors.New("missing up section in migration file") 11 ) 12 13 // migrateUpHeader identifies the start of the up-script in the migration. 14 const migrateUpHeader = "### migrate up ###" 15 16 // migrateDownHeader identifies the start of the down-script in the migration. 17 const migrateDownHeader = "### migrate down ###" 18 19 // parse returns the up and down commands extracted from a migration file's body 20 func parse(body string) (up string, down string, err error) { 21 upPos := strings.Index(body, migrateUpHeader) 22 downPos := strings.Index(body, migrateDownHeader) 23 if upPos == -1 { 24 return "", "", nil 25 } 26 if downPos != -1 && downPos < upPos { 27 return "", "", ErrWrongOrder 28 } 29 if downPos != -1 { 30 up = body[upPos+len(migrateUpHeader) : downPos] 31 down = body[downPos+len(migrateDownHeader):] 32 } else { 33 up = body[upPos+len(migrateUpHeader):] 34 } 35 return 36 }