From 563d9b588ac212e2795248125ba87af97aab10b3 Mon Sep 17 00:00:00 2001 From: mohammad ali ashraf Date: Sun, 21 May 2023 01:46:27 +0500 Subject: [PATCH] max identifier length changed to 63 --- gorm.go | 2 +- schema/naming.go | 17 +++++++++++------ schema/naming_test.go | 6 +++--- schema/relationship_test.go | 4 ++-- 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/gorm.go b/gorm.go index 07a913fc..32c7cef4 100644 --- a/gorm.go +++ b/gorm.go @@ -146,7 +146,7 @@ func Open(dialector Dialector, opts ...Option) (db *DB, err error) { } if config.NamingStrategy == nil { - config.NamingStrategy = schema.NamingStrategy{} + config.NamingStrategy = schema.NamingStrategy{IdentifierMaxLength: 63} } if config.Logger == nil { diff --git a/schema/naming.go b/schema/naming.go index a258beed..37b94980 100644 --- a/schema/naming.go +++ b/schema/naming.go @@ -28,10 +28,11 @@ type Replacer interface { // NamingStrategy tables, columns naming strategy type NamingStrategy struct { - TablePrefix string - SingularTable bool - NameReplacer Replacer - NoLowerCase bool + TablePrefix string + SingularTable bool + NameReplacer Replacer + NoLowerCase bool + IdentifierMaxLength int } // TableName convert string to table name @@ -89,12 +90,16 @@ func (ns NamingStrategy) formatName(prefix, table, name string) string { prefix, table, name, }, "_"), ".", "_") - if utf8.RuneCountInString(formattedName) > 64 { + if ns.IdentifierMaxLength == 0 { + ns.IdentifierMaxLength = 63 + } + + if utf8.RuneCountInString(formattedName) > ns.IdentifierMaxLength { h := sha1.New() h.Write([]byte(formattedName)) bs := h.Sum(nil) - formattedName = formattedName[0:56] + hex.EncodeToString(bs)[:8] + formattedName = formattedName[0:ns.IdentifierMaxLength-8] + hex.EncodeToString(bs)[:8] } return formattedName } diff --git a/schema/naming_test.go b/schema/naming_test.go index 3f598c33..9ce8f099 100644 --- a/schema/naming_test.go +++ b/schema/naming_test.go @@ -189,11 +189,11 @@ func TestCustomReplacerWithNoLowerCase(t *testing.T) { } } -func TestFormatNameWithStringLongerThan64Characters(t *testing.T) { - ns := NamingStrategy{} +func TestFormatNameWithStringLongerThan63Characters(t *testing.T) { + ns := NamingStrategy{IdentifierMaxLength: 63} formattedName := ns.formatName("prefix", "table", "thisIsAVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString") - if formattedName != "prefix_table_thisIsAVeryVeryVeryVeryVeryVeryVeryVeryVery180f2c67" { + if formattedName != "prefix_table_thisIsAVeryVeryVeryVeryVeryVeryVeryVeryVer180f2c67" { t.Errorf("invalid formatted name generated, got %v", formattedName) } } diff --git a/schema/relationship_test.go b/schema/relationship_test.go index 732f6f75..de5fa044 100644 --- a/schema/relationship_test.go +++ b/schema/relationship_test.go @@ -768,13 +768,13 @@ func TestParseConstraintNameWithSchemaQualifiedLongTableName(t *testing.T) { s, err := schema.Parse( &Book{}, &sync.Map{}, - schema.NamingStrategy{}, + schema.NamingStrategy{IdentifierMaxLength: 63}, ) if err != nil { t.Fatalf("Failed to parse schema") } - expectedConstraintName := "fk_my_schema_a_very_very_very_very_very_very_very_very_l4db13eec" + expectedConstraintName := "fk_my_schema_a_very_very_very_very_very_very_very_very_4db13eec" constraint := s.Relationships.Relations["Author"].ParseConstraint() if constraint.Name != expectedConstraintName {