2014-02-15 12:29:53 +00:00
|
|
|
package migrate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2014-03-10 00:11:15 +00:00
|
|
|
|
|
|
|
"github.com/dchest/uniuri"
|
2014-02-15 12:29:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func fetchColumns(sql string) ([]string, error) {
|
2014-02-18 18:11:41 +00:00
|
|
|
if !strings.HasPrefix(sql, "CREATE ") {
|
2014-02-15 12:29:53 +00:00
|
|
|
return []string{}, fmt.Errorf("Sql input is not a DDL statement.")
|
|
|
|
}
|
|
|
|
|
|
|
|
parenIdx := strings.Index(sql, "(")
|
2014-02-18 18:11:41 +00:00
|
|
|
return strings.Split(sql[parenIdx+1:strings.LastIndex(sql, ")")], ","), nil
|
2014-02-15 12:29:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func selectName(columns []string) []string {
|
2014-02-15 15:17:22 +00:00
|
|
|
var results []string
|
2014-02-15 12:29:53 +00:00
|
|
|
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 {
|
2014-02-15 15:17:22 +00:00
|
|
|
var results []string
|
2014-02-15 12:29:53 +00:00
|
|
|
for k, str := range left {
|
|
|
|
results = append(results, fmt.Sprintf("%s = %s", str, right[k]))
|
|
|
|
}
|
|
|
|
return strings.Join(results, ", ")
|
|
|
|
}
|
2014-03-10 00:11:15 +00:00
|
|
|
|
|
|
|
func proxyName(tableName string) string {
|
|
|
|
return fmt.Sprintf("%s_%s", tableName, uniuri.NewLen(16))
|
|
|
|
}
|
|
|
|
|
|
|
|
func indexName(tableName string, columns []string) string {
|
|
|
|
return fmt.Sprintf("idx_%s_on_%s", tableName, strings.Join(columns, "_and_"))
|
|
|
|
}
|