woodpecker/pkg/database/migrate/util.go
Nurahmadie 21f7fcb853 Fix index operations:
- Pass args as variadic params
- Tests for index operations
- Refactor fetchColumns
2014-02-19 01:11:41 +07:00

32 lines
766 B
Go

package migrate
import (
"fmt"
"strings"
)
func fetchColumns(sql string) ([]string, error) {
if !strings.HasPrefix(sql, "CREATE ") {
return []string{}, fmt.Errorf("Sql input is not a DDL statement.")
}
parenIdx := strings.Index(sql, "(")
return strings.Split(sql[parenIdx+1:strings.LastIndex(sql, ")")], ","), nil
}
func selectName(columns []string) []string {
var results []string
for _, column := range columns {
col := strings.SplitN(strings.Trim(column, " \n\t"), " ", 2)
results = append(results, col[0])
}
return results
}
func setForUpdate(left []string, right []string) string {
var results []string
for k, str := range left {
results = append(results, fmt.Sprintf("%s = %s", str, right[k]))
}
return strings.Join(results, ", ")
}