Skip to content

Commit

Permalink
max identifier length changed to 63 (#6337)
Browse files Browse the repository at this point in the history
* max identifier length changed to 63

* default maxIdentifierLength is 64

* renamed License to LICENSE (#6336)

* Added support of "Violates Foreign Key Constraint" (#6329)

* Added support of "Violates Foreign Key Constraint"

Updated the translator and added the support of "foreign key constraint violation". For this, this error type is needed here.

* changed the description of ErrForeignKeyViolated

* refactor: error translator test (#6350)

Co-authored-by: Saeid Saeidee <s.saeidee@sensysgatso.com>

* fix(nested transaction): SavePoint SQL Statement not support in Prepared Statements (#6220)

* test: add nested transaction and prepareStmt coexist test case

note: please test in the MySQL environment

Change-Id: I0db32adc5f74b0d443e98943d3b182236583b959
Signed-off-by: 王柳洋 <wangliuyang.520@bytedance.com>

* fix(nested transaction): SavePoint SQL Statement not support in Prepared Statements

1. SavetPoint SQL Statement not support in Prepared Statements
 e.g. see mysql8.0 doc: https://dev.mysql.com/doc/refman/8.0/en/sql-prepared-statements.html

Change-Id: I082012db9b140e8ec69764c633724665cc802692
Signed-off-by: 王柳洋 <wangliuyang.520@bytedance.com>

* revert(transaction_api): remove savepoint name pool,meaningless

Change-Id: I84aa9924fc54612005a81c83d66fdf8968ee56ad
Signed-off-by: 王柳洋 <wangliuyang.520@bytedance.com>

---------

Signed-off-by: 王柳洋 <wangliuyang.520@bytedance.com>
Co-authored-by: 王柳洋 <wangliuyang.520@bytedance.com>

* fix: save with hook (#6285) (#6294)

---------

Signed-off-by: 王柳洋 <wangliuyang.520@bytedance.com>
Co-authored-by: Avinaba Bhattacharjee <avinababhattacharjee2002@gmail.com>
Co-authored-by: Muhammad Amir Ejaz <37077032+codingamir@users.noreply.github.com>
Co-authored-by: Saeid <sk.saeidee@yahoo.com>
Co-authored-by: Saeid Saeidee <s.saeidee@sensysgatso.com>
Co-authored-by: wangliuyang <54885906+wangliuyang520@users.noreply.github.com>
Co-authored-by: 王柳洋 <wangliuyang.520@bytedance.com>
Co-authored-by: black-06 <hello.bug@foxmail.com>
  • Loading branch information
8 people committed May 30, 2023
1 parent 11fdf46 commit 26663ab
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: 64} // Default Identifier length is 64
}

if config.Logger == nil {
Expand Down
17 changes: 11 additions & 6 deletions schema/naming.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = 64
}

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
}
Expand Down
11 changes: 10 additions & 1 deletion schema/naming_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,17 @@ func TestCustomReplacerWithNoLowerCase(t *testing.T) {
}
}

func TestFormatNameWithStringLongerThan63Characters(t *testing.T) {
ns := NamingStrategy{IdentifierMaxLength: 63}

formattedName := ns.formatName("prefix", "table", "thisIsAVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString")
if formattedName != "prefix_table_thisIsAVeryVeryVeryVeryVeryVeryVeryVeryVer180f2c67" {
t.Errorf("invalid formatted name generated, got %v", formattedName)
}
}

func TestFormatNameWithStringLongerThan64Characters(t *testing.T) {
ns := NamingStrategy{}
ns := NamingStrategy{IdentifierMaxLength: 64}

formattedName := ns.formatName("prefix", "table", "thisIsAVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString")
if formattedName != "prefix_table_thisIsAVeryVeryVeryVeryVeryVeryVeryVeryVery180f2c67" {
Expand Down
2 changes: 1 addition & 1 deletion schema/relationship_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ func TestParseConstraintNameWithSchemaQualifiedLongTableName(t *testing.T) {
s, err := schema.Parse(
&Book{},
&sync.Map{},
schema.NamingStrategy{},
schema.NamingStrategy{IdentifierMaxLength: 64},
)
if err != nil {
t.Fatalf("Failed to parse schema")
Expand Down

0 comments on commit 26663ab

Please sign in to comment.