Fixed naming longer than 64 characters (#4310)

Co-authored-by: Mickael MAUGER <mickael.mauger@almerys.com>
This commit is contained in:
Sky34gl3 2021-04-22 07:11:19 +02:00 committed by GitHub
parent d327926425
commit a855fe6402
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package schema
import ( import (
"crypto/sha1" "crypto/sha1"
"encoding/hex"
"fmt" "fmt"
"strings" "strings"
"unicode/utf8" "unicode/utf8"
@ -80,7 +81,7 @@ func (ns NamingStrategy) formatName(prefix, table, name string) string {
h.Write([]byte(formattedName)) h.Write([]byte(formattedName))
bs := h.Sum(nil) 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 return formattedName
} }

View File

@ -168,3 +168,12 @@ func TestCustomReplacerWithNoLowerCase(t *testing.T) {
t.Errorf("invalid column name generated, got %v", columdName) 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)
}
}