diff --git a/schema/naming.go b/schema/naming.go index dbc71e04..12df63dd 100644 --- a/schema/naming.go +++ b/schema/naming.go @@ -53,25 +53,23 @@ 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, toDBName(rel.Name)), ".", "_", -1) + return genPatternName("fk", charUnderscore, charPoint, rel.Schema.Table, 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 genPatternName("chk", charUnderscore, charPoint, table, toDBName(column)) } // IndexName generate index name func (ns NamingStrategy) IndexName(table, column string) string { - idxName := fmt.Sprintf("idx_%v_%v", table, toDBName(column)) - idxName = strings.Replace(idxName, ".", "_", -1) - + idxName := genPatternName("idx", charUnderscore, charPoint, table, toDBName(column)) if utf8.RuneCountInString(idxName) > 64 { h := sha1.New() h.Write([]byte(idxName)) bs := h.Sum(nil) - idxName = fmt.Sprintf("idx%v%v", table, column)[0:56] + string(bs)[:8] + idxName = genPatternName("idx", charBlank, charBlank, table, column)[0:56] + string(bs)[:8] } return idxName } diff --git a/schema/utils.go b/schema/utils.go index 55cbdeb4..484aeadb 100644 --- a/schema/utils.go +++ b/schema/utils.go @@ -195,3 +195,24 @@ type embeddedNamer struct { Table string Namer } + +var ( + charBlank = "" + charPoint = "." + charUnderscore = "_" +) + +// genPatternName generate pattern string, use to replace fmt.Sprintf() +func genPatternName(prefix, intervalBefore, intervalAfter string, vars ...string) string { + for i := range vars { + if intervalBefore != charBlank { + prefix += intervalBefore + vars[i] + continue + } + prefix += vars[i] + } + if intervalAfter == charBlank { + return prefix + } + return strings.Replace(prefix, intervalAfter, intervalBefore, -1) +} diff --git a/schema/utils_test.go b/schema/utils_test.go index 1b47ef25..1f3832f4 100644 --- a/schema/utils_test.go +++ b/schema/utils_test.go @@ -22,3 +22,48 @@ func TestRemoveSettingFromTag(t *testing.T) { } } } + +func TestGenPatternName(t *testing.T) { + gives := []struct { + Prefix string + IntervalBefore string + IntervalAfter string + Vars []string + Want string + }{ + { + Prefix: "fk", + IntervalBefore: charUnderscore, + IntervalAfter: charPoint, + Vars: []string{"a", "b"}, + Want: "fk_a_b", + }, + { + Prefix: "idx", + IntervalBefore: charUnderscore, + IntervalAfter: charPoint, + Vars: []string{"a", "b"}, + Want: "idx_a_b", + }, + { + Prefix: "chk", + IntervalBefore: charUnderscore, + IntervalAfter: charPoint, + Vars: []string{"a", "b"}, + Want: "chk_a_b", + }, + { + Prefix: "idx", + IntervalBefore: charBlank, + IntervalAfter: charBlank, + Vars: []string{"a", "b"}, + Want: "idxab", + }, + } + + for i := range gives { + if genPatternName(gives[i].Prefix, gives[i].IntervalBefore, gives[i].IntervalAfter, gives[i].Vars...) != gives[i].Want { + t.Errorf("want %s, but got %s", gives[i].Want, genPatternName(gives[i].Prefix, gives[i].IntervalBefore, gives[i].IntervalAfter, gives[i].Vars...)) + } + } +}