From f21ce964fc0dc1f3425eb88d69c7b6a069bd7c75 Mon Sep 17 00:00:00 2001 From: sivyer9303 Date: Sun, 1 Aug 2021 19:31:45 +0800 Subject: [PATCH] add test --- callbacks.go | 2 +- finisher_api.go | 7 ------- statement.go | 11 ----------- tests/hooks_test.go | 14 ++++++++++++++ 4 files changed, 15 insertions(+), 19 deletions(-) diff --git a/callbacks.go b/callbacks.go index 88b295a7..d4b61f73 100644 --- a/callbacks.go +++ b/callbacks.go @@ -127,7 +127,7 @@ func (p *processor) Execute(db *DB) *DB { } for _, c := range p.callbacks { - if stmt.ShouldSkipHook(c) { + if !stmt.ShouldSkipHook(c) { c.handler(db) } } diff --git a/finisher_api.go b/finisher_api.go index e3678683..c2617f13 100644 --- a/finisher_api.go +++ b/finisher_api.go @@ -642,10 +642,3 @@ func (db *DB) SkipHookByName(name ...string) (tx *DB) { tx.Statement.SkipHooksNames = append(tx.Statement.SkipHooksNames, name...) return tx } - -// add SkipHook name -func (db *DB) SkipHookByFunc(fns ...func(*DB)) (tx *DB) { - tx = db.getInstance() - tx.Statement.SkipHooksFunc = append(tx.Statement.SkipHooksFunc, fns...) - return tx -} diff --git a/statement.go b/statement.go index b596e773..d2755610 100644 --- a/statement.go +++ b/statement.go @@ -40,7 +40,6 @@ type Statement struct { RaiseErrorOnNotFound bool SkipHooks bool SkipHooksNames []string - SkipHooksFunc []func(*DB) SQL strings.Builder Vars []interface{} CurDestIndex int @@ -689,16 +688,6 @@ func (stmt *Statement) ShouldSkipHook(c *callback) (skip bool) { } } } - // skip by func - if !skip && len(stmt.SkipHooksFunc) > 0 { - for _, hookFunc := range stmt.SkipHooksFunc { - // compare with ptr - if &hookFunc == &c.handler { - skip = true - break - } - } - } } return } diff --git a/tests/hooks_test.go b/tests/hooks_test.go index 0e6ab2fe..2fec2d4d 100644 --- a/tests/hooks_test.go +++ b/tests/hooks_test.go @@ -493,3 +493,17 @@ func TestFailedToSaveAssociationShouldRollback(t *testing.T) { t.Errorf("should find product, but got error %v", err) } } + +func TestSkipHookByName(t *testing.T) { + product := Product3{Name: "Product", Price: 0} + DB.AutoMigrate(&Product3{}) + // expect price = 0 + DB.SkipHookByName("gorm:before_create").Create(&product) + product2 := Product3{Name: "Product", Price: 0} + // expect price = 100 + DB.Create(&product2) + // expect code = code1 , price = 100 + 20(add in before update) + 30(add in before update) + DB.Model(&product2).Update("code", "code1") + // expect code = code2 , price not change + DB.Model(&product).SkipHookByName("gorm:before_update").Update("code", "code2") +}