mirror of https://github.com/go-gorm/gorm.git
Fixed naming longer than 64 characters (#4310)
Co-authored-by: Mickael MAUGER <mickael.mauger@almerys.com>
This commit is contained in:
parent
d327926425
commit
a855fe6402
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue