github.com/marinho/drone@v0.2.1-0.20140504195434-d3ba962e89a7/pkg/database/migrate/util.go (about) 1 package migrate 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/dchest/uniuri" 8 ) 9 10 func fetchColumns(sql string) ([]string, error) { 11 if !strings.HasPrefix(sql, "CREATE ") { 12 return []string{}, fmt.Errorf("Sql input is not a DDL statement.") 13 } 14 15 parenIdx := strings.Index(sql, "(") 16 return strings.Split(sql[parenIdx+1:strings.LastIndex(sql, ")")], ","), nil 17 } 18 19 func selectName(columns []string) []string { 20 var results []string 21 for _, column := range columns { 22 col := strings.SplitN(strings.Trim(column, " \n\t"), " ", 2) 23 results = append(results, col[0]) 24 } 25 return results 26 } 27 28 func setForUpdate(left []string, right []string) string { 29 var results []string 30 for k, str := range left { 31 results = append(results, fmt.Sprintf("%s = %s", str, right[k])) 32 } 33 return strings.Join(results, ", ") 34 } 35 36 func proxyName(tableName string) string { 37 return fmt.Sprintf("%s_%s", tableName, uniuri.NewLen(16)) 38 } 39 40 func indexName(tableName string, columns []string) string { 41 return fmt.Sprintf("idx_%s_on_%s", tableName, strings.Join(columns, "_and_")) 42 }