From a855fe64026a65bba106d6614873638c64b3fc8b Mon Sep 17 00:00:00 2001 From: Sky34gl3 Date: Thu, 22 Apr 2021 07:11:19 +0200 Subject: [PATCH] Fixed naming longer than 64 characters (#4310) Co-authored-by: Mickael MAUGER --- schema/naming.go | 3 ++- schema/naming_test.go | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/schema/naming.go b/schema/naming.go index 1962c3c6..d53942e4 100644 --- a/schema/naming.go +++ b/schema/naming.go @@ -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 } diff --git a/schema/naming_test.go b/schema/naming_test.go index 08f8d498..face9364 100644 --- a/schema/naming_test.go +++ b/schema/naming_test.go @@ -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) + } +}