From 26262ef9bb897b06d4e7ad6f1316e1037e030283 Mon Sep 17 00:00:00 2001 From: Wing Gao Date: Tue, 28 Nov 2017 13:05:10 +0800 Subject: [PATCH 1/3] autoIndex should throw an error on failed --- scope.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/scope.go b/scope.go index 51ebd5a0..f1b9da4b 100644 --- a/scope.go +++ b/scope.go @@ -1228,11 +1228,19 @@ func (scope *Scope) autoIndex() *Scope { } for name, columns := range indexes { - scope.NewDB().Model(scope.Value).AddIndex(name, columns...) + db := scope.NewDB().Model(scope.Value).AddIndex(name, columns...) + if db.Error != nil { + scope.db.Error = db.Error + return scope + } } for name, columns := range uniqueIndexes { - scope.NewDB().Model(scope.Value).AddUniqueIndex(name, columns...) + db := scope.NewDB().Model(scope.Value).AddUniqueIndex(name, columns...) + if db.Error != nil { + scope.db.Error = db.Error + return scope + } } return scope From b9035a7602b7734076ac4a3146fc88d285e326a5 Mon Sep 17 00:00:00 2001 From: s-takehana Date: Wed, 31 Jan 2018 17:32:36 +0900 Subject: [PATCH 2/3] Fix regex in BuildForeignKeyName #1681 (#1728) --- dialect_common.go | 2 +- dialect_mysql.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/dialect_common.go b/dialect_common.go index a99627f2..7d0c3ce7 100644 --- a/dialect_common.go +++ b/dialect_common.go @@ -146,7 +146,7 @@ func (commonDialect) LastInsertIDReturningSuffix(tableName, columnName string) s func (DefaultForeignKeyNamer) BuildForeignKeyName(tableName, field, dest string) string { keyName := fmt.Sprintf("%s_%s_%s_foreign", tableName, field, dest) - keyName = regexp.MustCompile("(_*[^a-zA-Z]+_*|_+)").ReplaceAllString(keyName, "_") + keyName = regexp.MustCompile("[^a-zA-Z0-9]+").ReplaceAllString(keyName, "_") return keyName } diff --git a/dialect_mysql.go b/dialect_mysql.go index 6fcd0079..686ad1ee 100644 --- a/dialect_mysql.go +++ b/dialect_mysql.go @@ -166,8 +166,8 @@ func (s mysql) BuildForeignKeyName(tableName, field, dest string) string { h.Write([]byte(keyName)) bs := h.Sum(nil) - // sha1 is 40 digits, keep first 24 characters of destination - destRunes := []rune(regexp.MustCompile("(_*[^a-zA-Z]+_*|_+)").ReplaceAllString(dest, "_")) + // sha1 is 40 characters, keep first 24 characters of destination + destRunes := []rune(regexp.MustCompile("[^a-zA-Z0-9]+").ReplaceAllString(dest, "_")) if len(destRunes) > 24 { destRunes = destRunes[:24] } From 630c12b54936a0b20a6ddf8a35dab18279165dd8 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Wed, 31 Jan 2018 17:14:21 +0800 Subject: [PATCH 3/3] Refactor #1693 --- scope.go | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/scope.go b/scope.go index f1b9da4b..0a7e8861 100644 --- a/scope.go +++ b/scope.go @@ -1228,18 +1228,14 @@ func (scope *Scope) autoIndex() *Scope { } for name, columns := range indexes { - db := scope.NewDB().Model(scope.Value).AddIndex(name, columns...) - if db.Error != nil { - scope.db.Error = db.Error - return scope + if db := scope.NewDB().Model(scope.Value).AddIndex(name, columns...); db.Error != nil { + scope.db.AddError(db.Error) } } for name, columns := range uniqueIndexes { - db := scope.NewDB().Model(scope.Value).AddUniqueIndex(name, columns...) - if db.Error != nil { - scope.db.Error = db.Error - return scope + if db := scope.NewDB().Model(scope.Value).AddUniqueIndex(name, columns...); db.Error != nil { + scope.db.AddError(db.Error) } }