From e4b130d2d7fc45a8b93180950d73ddc368f0dda4 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Wed, 1 Feb 2017 21:33:36 +0800 Subject: [PATCH] Fix customize DeletedAt's column name --- callback_delete.go | 7 +++++-- delete_test.go | 23 +++++++++++++++++++++++ migration_test.go | 2 +- scope.go | 5 +++-- 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/callback_delete.go b/callback_delete.go index 6217706e..73d90880 100644 --- a/callback_delete.go +++ b/callback_delete.go @@ -33,10 +33,13 @@ func deleteCallback(scope *Scope) { extraOption = fmt.Sprint(str) } - if !scope.Search.Unscoped && scope.HasColumn("DeletedAt") { + deletedAtField, hasDeletedAtField := scope.FieldByName("DeletedAt") + + if !scope.Search.Unscoped && hasDeletedAtField { scope.Raw(fmt.Sprintf( - "UPDATE %v SET deleted_at=%v%v%v", + "UPDATE %v SET %v=%v%v%v", scope.QuotedTableName(), + scope.Quote(deletedAtField.DBName), scope.AddToVars(NowFunc()), addExtraSpaceIfExist(scope.CombinedConditionSql()), addExtraSpaceIfExist(extraOption), diff --git a/delete_test.go b/delete_test.go index d3de0a6d..043641f7 100644 --- a/delete_test.go +++ b/delete_test.go @@ -66,3 +66,26 @@ func TestSoftDelete(t *testing.T) { t.Errorf("Can't find permanently deleted record") } } + +func TestSoftDeleteWithCustomizedDeletedAtColumnName(t *testing.T) { + creditCard := CreditCard{Number: "411111111234567"} + DB.Save(&creditCard) + DB.Delete(&creditCard) + + if deletedAtField, ok := DB.NewScope(&CreditCard{}).FieldByName("DeletedAt"); !ok || deletedAtField.DBName != "deleted_time" { + t.Errorf("CreditCard's DeletedAt's column name should be `deleted_time`") + } + + if DB.First(&CreditCard{}, "number = ?", creditCard.Number).Error == nil { + t.Errorf("Can't find a soft deleted record") + } + + if err := DB.Unscoped().First(&CreditCard{}, "number = ?", creditCard.Number).Error; err != nil { + t.Errorf("Should be able to find soft deleted record with Unscoped, but err=%s", err) + } + + DB.Unscoped().Delete(&creditCard) + if !DB.Unscoped().First(&CreditCard{}, "number = ?", creditCard.Number).RecordNotFound() { + t.Errorf("Can't find permanently deleted record") + } +} diff --git a/migration_test.go b/migration_test.go index 8b3c4ab6..95c2c571 100644 --- a/migration_test.go +++ b/migration_test.go @@ -66,7 +66,7 @@ type CreditCard struct { UserId sql.NullInt64 CreatedAt time.Time `sql:"not null"` UpdatedAt time.Time - DeletedAt *time.Time + DeletedAt *time.Time `sql:"column:deleted_time"` } type Email struct { diff --git a/scope.go b/scope.go index c36dbb89..45f7185f 100644 --- a/scope.go +++ b/scope.go @@ -673,11 +673,12 @@ func (scope *Scope) buildSelectQuery(clause map[string]interface{}) (str string) func (scope *Scope) whereSQL() (sql string) { var ( quotedTableName = scope.QuotedTableName() + deletedAtField, hasDeletedAtField = scope.FieldByName("DeletedAt") primaryConditions, andConditions, orConditions []string ) - if !scope.Search.Unscoped && scope.HasColumn("deleted_at") { - sql := fmt.Sprintf("%v.deleted_at IS NULL", quotedTableName) + if !scope.Search.Unscoped && hasDeletedAtField { + sql := fmt.Sprintf("%v.%v IS NULL", quotedTableName, scope.Quote(deletedAtField.DBName)) primaryConditions = append(primaryConditions, sql) }