diff --git a/schema/naming.go b/schema/naming.go index 63296967..f6d15f5a 100644 --- a/schema/naming.go +++ b/schema/naming.go @@ -54,27 +54,30 @@ func (ns NamingStrategy) JoinTableName(str string) string { // RelationshipFKName generate fk name for relation func (ns NamingStrategy) RelationshipFKName(rel Relationship) string { - return strings.Replace(fmt.Sprintf("fk_%s_%s", rel.Schema.Table, ns.toDBName(rel.Name)), ".", "_", -1) + return ns.formatName("fk", rel.Schema.Table, ns.toDBName(rel.Name)) } // CheckerName generate checker name func (ns NamingStrategy) CheckerName(table, column string) string { - return strings.Replace(fmt.Sprintf("chk_%s_%s", table, column), ".", "_", -1) + return ns.formatName("chk", table, column) } // IndexName generate index name func (ns NamingStrategy) IndexName(table, column string) string { - idxName := fmt.Sprintf("idx_%v_%v", table, ns.toDBName(column)) - idxName = strings.Replace(idxName, ".", "_", -1) + return ns.formatName("idx", table, ns.toDBName(column)) +} - if utf8.RuneCountInString(idxName) > 64 { +func (ns NamingStrategy) formatName(prefix, table, name string) string { + formatedName := strings.Replace(fmt.Sprintf("%v_%v_%v", prefix, table, name), ".", "_", -1) + + if utf8.RuneCountInString(formatedName) > 64 { h := sha1.New() - h.Write([]byte(idxName)) + h.Write([]byte(formatedName)) bs := h.Sum(nil) - idxName = fmt.Sprintf("idx%v%v", table, column)[0:56] + string(bs)[:8] + formatedName = fmt.Sprintf("%v%v%v", prefix, table, name)[0:56] + string(bs)[:8] } - return idxName + return formatedName } var (