Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

demonstrate gorm AfterUpdate hook failure #747

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions afterupdate_hook_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package main

import (
"fmt"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"gorm.io/gorm"
"testing"
)

type Product6 struct {
ID uint `gorm:"primarykey"`
Name *string
UniqueId *uuid.UUID `gorm:"type:varchar(36);unique" json:",omitempty"`
}

// Hold copy of the values passed to the AfterUpdate() for subsequent check
var hookName *string
var hookUuid *uuid.UUID

func (p *Product6) AfterUpdate(*gorm.DB) error {

hookName = p.Name
hookUuid = p.UniqueId

name := "nil"
if p.Name != nil {
name = *p.Name
}
uniqueId := "nil"
if p.UniqueId != nil {
uniqueId = p.UniqueId.String()
}
fmt.Printf("After update p.UniqueId %v p.Name %v\n", uniqueId, name)
return nil
}

func StringPtr(s string) *string {
return &s
}
func UuidPtr(u uuid.UUID) *uuid.UUID {
return &u
}

func TestAfterUpdateHook(t *testing.T) {
DB.Migrator().DropTable(&Product6{})
DB.Debug().AutoMigrate(&Product6{})

productName := StringPtr("product name")
productUuid := UuidPtr(uuid.MustParse("5f003fc3-f806-42a3-ae4e-4d4335581de0"))
p := Product6{Name: productName, UniqueId: productUuid}
DB.Model(&Product6{}).Create(&p)

p.Name = nil
p.UniqueId = nil
err := DB.Model(&p).Updates(map[string]interface{}{"name": p.Name, "unique_id": p.UniqueId}).Error
assert.Nil(t, err)
assert.Nil(t, hookName)
assert.Nil(t, hookUuid)

productName = StringPtr("new name")
productUuid = UuidPtr(uuid.MustParse("415d9c8a-8742-4fe6-a6cd-8062a121eeb4"))
err = DB.Model(&p).Updates(map[string]interface{}{"name": productName, "unique_id": productUuid}).Error
assert.Nil(t, err)
assert.Equal(t, hookName, productName)
assert.Equal(t, hookUuid, productUuid)

productName = nil
productUuid = nil
err = DB.Model(&p).Updates(map[string]interface{}{"name": productName, "unique_id": productUuid}).Error
assert.Nil(t, err)
assert.Nil(t, hookName)
assert.Nil(t, hookUuid)
}
21 changes: 0 additions & 21 deletions gen.go

This file was deleted.

38 changes: 23 additions & 15 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,38 @@ module gorm.io/playground
go 1.20

require (
gorm.io/driver/mysql v1.5.2
gorm.io/driver/postgres v1.5.2
gorm.io/driver/sqlite v1.5.3
gorm.io/driver/sqlserver v1.5.1
github.com/google/uuid v1.6.0
github.com/stretchr/testify v1.9.0
gorm.io/driver/mysql v1.5.7
gorm.io/driver/postgres v1.5.9
gorm.io/driver/sqlite v1.5.6
gorm.io/driver/sqlserver v1.5.3
gorm.io/gen v0.3.25
gorm.io/gorm v1.25.4
gorm.io/gorm v1.25.10
)

require (
github.com/go-sql-driver/mysql v1.7.1 // indirect
filippo.io/edwards25519 v1.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-sql-driver/mysql v1.8.1 // indirect
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
github.com/golang-sql/sqlexp v0.1.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.4.3 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/pgx/v5 v5.6.0 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/mattn/go-sqlite3 v1.14.17 // indirect
github.com/microsoft/go-mssqldb v1.5.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/tools v0.15.0 // indirect
github.com/mattn/go-sqlite3 v1.14.22 // indirect
github.com/microsoft/go-mssqldb v1.7.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gorm.io/datatypes v1.1.1-0.20230130040222-c43177d3cf8c // indirect
gorm.io/hints v1.1.0 // indirect
gorm.io/plugin/dbresolver v1.5.0 // indirect
Expand Down
Loading