Fixed naming longer than 64 characters

This commit is contained in:
Mickael MAUGER 2021-04-21 14:52:38 +02:00
parent d327926425
commit 15328f49b9
2 changed files with 11 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package schema
import (
"crypto/sha1"
"encoding/hex"
"fmt"
"strings"
"unicode/utf8"
@ -80,7 +81,7 @@ func (ns NamingStrategy) formatName(prefix, table, name string) string {
h.Write([]byte(formattedName))
bs := h.Sum(nil)
formattedName = fmt.Sprintf("%v%v%v", prefix, table, name)[0:56] + string(bs)[:8]
formattedName = fmt.Sprintf("%v%v%v", prefix, table, name)[0:56] + hex.EncodeToString(bs)[:8]
}
return formattedName
}

View File

@ -168,3 +168,12 @@ func TestCustomReplacerWithNoLowerCase(t *testing.T) {
t.Errorf("invalid column name generated, got %v", columdName)
}
}
func TestFormatNameWithStringLongerThan64Characters(t *testing.T) {
var ns = NamingStrategy{}
formattedName := ns.formatName("prefix", "table", "thisIsAVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString")
if formattedName != "prefixtablethisIsAVeryVeryVeryVeryVeryVeryVeryVeryVeryLo180f2c67" {
t.Errorf("invalid formatted name generated, got %v", formattedName)
}
}