Skip to content

Commit

Permalink
Gorm v2 migration 2024, part 1 (#11)
Browse files Browse the repository at this point in the history
* Add `V1` postfix to callbacks

Signed-off-by: peterdeme <snypox@gmail.com>

* Ability to take both `primary_key` and `primaryKey`

Signed-off-by: peterdeme <snypox@gmail.com>

---------

Signed-off-by: peterdeme <snypox@gmail.com>
  • Loading branch information
peterdeme authored Feb 29, 2024
1 parent 219d979 commit 82bbce3
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 26 deletions.
8 changes: 4 additions & 4 deletions callback_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ func init() {
// beforeCreateCallback will invoke `BeforeSave`, `BeforeCreate` method before creating
func beforeCreateCallback(scope *Scope) {
if !scope.HasError() {
scope.CallMethod("BeforeSave")
scope.CallMethod("BeforeSaveV1")
}
if !scope.HasError() {
scope.CallMethod("BeforeCreate")
scope.CallMethod("BeforeCreateV1")
}
}

Expand Down Expand Up @@ -196,9 +196,9 @@ func forceReloadAfterCreateCallback(scope *Scope) {
// afterCreateCallback will invoke `AfterCreate`, `AfterSave` method after creating
func afterCreateCallback(scope *Scope) {
if !scope.HasError() {
scope.CallMethod("AfterCreate")
scope.CallMethod("AfterCreateV1")
}
if !scope.HasError() {
scope.CallMethod("AfterSave")
scope.CallMethod("AfterSaveV1")
}
}
4 changes: 2 additions & 2 deletions callback_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func beforeDeleteCallback(scope *Scope) {
return
}
if !scope.HasError() {
scope.CallMethod("BeforeDelete")
scope.CallMethod("BeforeDeleteV1")
}
}

Expand Down Expand Up @@ -58,6 +58,6 @@ func deleteCallback(scope *Scope) {
// afterDeleteCallback will invoke `AfterDelete` method after deleting
func afterDeleteCallback(scope *Scope) {
if !scope.HasError() {
scope.CallMethod("AfterDelete")
scope.CallMethod("AfterDeleteV1")
}
}
2 changes: 1 addition & 1 deletion callback_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,6 @@ func queryCallback(scope *Scope) {
// afterQueryCallback will invoke `AfterFind` method after querying
func afterQueryCallback(scope *Scope) {
if !scope.HasError() {
scope.CallMethod("AfterFind")
scope.CallMethod("AfterFindV1")
}
}
8 changes: 4 additions & 4 deletions callback_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ func beforeUpdateCallback(scope *Scope) {
}
if _, ok := scope.Get("gorm:update_column"); !ok {
if !scope.HasError() {
scope.CallMethod("BeforeSave")
scope.CallMethod("BeforeSaveV1")
}
if !scope.HasError() {
scope.CallMethod("BeforeUpdate")
scope.CallMethod("BeforeUpdateV1")
}
}
}
Expand Down Expand Up @@ -112,10 +112,10 @@ func updateCallback(scope *Scope) {
func afterUpdateCallback(scope *Scope) {
if _, ok := scope.Get("gorm:update_column"); !ok {
if !scope.HasError() {
scope.CallMethod("AfterUpdate")
scope.CallMethod("AfterUpdateV1")
}
if !scope.HasError() {
scope.CallMethod("AfterSave")
scope.CallMethod("AfterSaveV1")
}
}
}
18 changes: 9 additions & 9 deletions callbacks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,59 +8,59 @@ import (
"github.com/jinzhu/gorm"
)

func (s *Product) BeforeCreate() (err error) {
func (s *Product) BeforeCreateV1() (err error) {
if s.Code == "Invalid" {
err = errors.New("invalid product")
}
s.BeforeCreateCallTimes = s.BeforeCreateCallTimes + 1
return
}

func (s *Product) BeforeUpdate() (err error) {
func (s *Product) BeforeUpdateV1() (err error) {
if s.Code == "dont_update" {
err = errors.New("can't update")
}
s.BeforeUpdateCallTimes = s.BeforeUpdateCallTimes + 1
return
}

func (s *Product) BeforeSave() (err error) {
func (s *Product) BeforeSaveV1() (err error) {
if s.Code == "dont_save" {
err = errors.New("can't save")
}
s.BeforeSaveCallTimes = s.BeforeSaveCallTimes + 1
return
}

func (s *Product) AfterFind() {
func (s *Product) AfterFindV1() {
s.AfterFindCallTimes = s.AfterFindCallTimes + 1
}

func (s *Product) AfterCreate(tx *gorm.DB) {
func (s *Product) AfterCreateV1(tx *gorm.DB) {
tx.Model(s).UpdateColumn(Product{AfterCreateCallTimes: s.AfterCreateCallTimes + 1})
}

func (s *Product) AfterUpdate() {
func (s *Product) AfterUpdateV1() {
s.AfterUpdateCallTimes = s.AfterUpdateCallTimes + 1
}

func (s *Product) AfterSave() (err error) {
func (s *Product) AfterSaveV1() (err error) {
if s.Code == "after_save_error" {
err = errors.New("can't save")
}
s.AfterSaveCallTimes = s.AfterSaveCallTimes + 1
return
}

func (s *Product) BeforeDelete() (err error) {
func (s *Product) BeforeDeleteV1() (err error) {
if s.Code == "dont_delete" {
err = errors.New("can't delete")
}
s.BeforeDeleteCallTimes = s.BeforeDeleteCallTimes + 1
return
}

func (s *Product) AfterDelete() (err error) {
func (s *Product) AfterDeleteV1() (err error) {
if s.Code == "after_delete_error" {
err = errors.New("can't delete")
}
Expand Down
2 changes: 1 addition & 1 deletion embedded_struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Author struct {

type HNPost struct {
BasePost
Author `gorm:"embedded_prefix:user_"` // Embedded struct
Author `gorm:"embeddedPrefix:user_"` // Embedded struct
Upvotes int32
}

Expand Down
2 changes: 1 addition & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func TestOpen_ReturnsError_WithBadArgs(t *testing.T) {

func TestStringPrimaryKey(t *testing.T) {
type UUIDStruct struct {
ID string `gorm:"primary_key"`
ID string `gorm:"primaryKey"`
Name string
}
DB.DropTable(&UUIDStruct{})
Expand Down
18 changes: 17 additions & 1 deletion model_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,17 +205,24 @@ func (scope *Scope) getModelStruct(rootScope *Scope, allFields []*StructField) *
if _, ok := field.TagSettingsGet("-"); ok {
field.IsIgnored = true
} else {
// Compatibility with GORM v2
if _, ok := field.TagSettingsGet("PRIMARY_KEY"); ok {
field.IsPrimaryKey = true
modelStruct.PrimaryFields = append(modelStruct.PrimaryFields, field)
} else if _, ok := field.TagSettingsGet("PRIMARYKEY"); ok {
field.IsPrimaryKey = true
modelStruct.PrimaryFields = append(modelStruct.PrimaryFields, field)
}

if _, ok := field.TagSettingsGet("DEFAULT"); ok && !field.IsPrimaryKey {
field.HasDefaultValue = true
}

// Compatibility with GORM v2
if _, ok := field.TagSettingsGet("AUTO_INCREMENT"); ok && !field.IsPrimaryKey {
field.HasDefaultValue = true
} else if _, ok := field.TagSettingsGet("AUTOINCREMENT"); ok && !field.IsPrimaryKey {
field.HasDefaultValue = true
}

indirectType := fieldStruct.Type
Expand Down Expand Up @@ -244,12 +251,21 @@ func (scope *Scope) getModelStruct(rootScope *Scope, allFields []*StructField) *
for _, subField := range scope.New(fieldValue).getModelStruct(rootScope, allFields).StructFields {
subField = subField.clone()
subField.Names = append([]string{fieldStruct.Name}, subField.Names...)

// Compatibility with GORM v2
if prefix, ok := field.TagSettingsGet("EMBEDDED_PREFIX"); ok {
subField.DBName = prefix + subField.DBName
} else if prefix, ok := field.TagSettingsGet("EMBEDDEDPREFIX"); ok {
subField.DBName = prefix + subField.DBName
}

if subField.IsPrimaryKey {
if _, ok := subField.TagSettingsGet("PRIMARY_KEY"); ok {
// Compatibility with GORM v2

_, primaryKeyV1 := field.TagSettingsGet("PRIMARY_KEY")
_, primaryKeyV2 := field.TagSettingsGet("PRIMARYKEY")

if primaryKeyV1 || primaryKeyV2 {
modelStruct.PrimaryFields = append(modelStruct.PrimaryFields, subField)
} else {
subField.IsPrimaryKey = false
Expand Down
4 changes: 2 additions & 2 deletions multi_primary_keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
)

type Blog struct {
ID uint `gorm:"primary_key"`
Locale string `gorm:"primary_key"`
ID uint `gorm:"primaryKey"` // New V2 style to define primary key
Locale string `gorm:"primary_key"` // Old V1 style to define primary key
Subject string
Body string
Tags []Tag `gorm:"many2many:blog_tags;"`
Expand Down
8 changes: 7 additions & 1 deletion scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -1303,7 +1303,13 @@ func (scope *Scope) autoIndex() *Scope {
}
}

if name, ok := field.TagSettingsGet("UNIQUE_INDEX"); ok {
// Compatibility with GORM v2
name, ok := field.TagSettingsGet("UNIQUE_INDEX")
if !ok {
name, ok = field.TagSettingsGet("UNIQUEINDEX")
}

if ok {
names := strings.Split(name, ",")

for _, name := range names {
Expand Down

0 comments on commit 82bbce3

Please sign in to comment.