From f984bc851547ed64d300354543cd76b3ae98ab33 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Thu, 28 Aug 2014 18:53:27 +0800 Subject: [PATCH] Refact code --- anonymous_struct_test.go | 3 +-- callback_create.go | 2 +- callback_update.go | 2 +- migration_test.go | 19 +++++++++---------- 4 files changed, 12 insertions(+), 14 deletions(-) diff --git a/anonymous_struct_test.go b/anonymous_struct_test.go index 4bf1374d..35bee690 100644 --- a/anonymous_struct_test.go +++ b/anonymous_struct_test.go @@ -3,18 +3,17 @@ package gorm_test import "testing" type BasePost struct { + Id int64 Title string Url string } type HNPost struct { - Id int64 BasePost `gorm:"embedded"` Upvotes int32 } type EngadgetPost struct { - Id int64 BasePost ImageUrl string } diff --git a/callback_create.go b/callback_create.go index cef035d6..750ddab9 100644 --- a/callback_create.go +++ b/callback_create.go @@ -26,7 +26,7 @@ func Create(scope *Scope) { var sqls, columns []string for _, field := range scope.Fields() { - if len(field.SqlTag) > 0 && !field.IsIgnored && (field.DBName != scope.PrimaryKey() || !scope.PrimaryKeyZero()) { + if len(field.SqlTag) > 0 && !field.IsIgnored && (!field.IsPrimaryKey || !scope.PrimaryKeyZero()) { columns = append(columns, scope.Quote(field.DBName)) sqls = append(sqls, scope.AddToVars(field.Value)) } diff --git a/callback_update.go b/callback_update.go index eb65e2e0..805cd7c6 100644 --- a/callback_update.go +++ b/callback_update.go @@ -50,7 +50,7 @@ func Update(scope *Scope) { } } else { for _, field := range scope.Fields() { - if field.DBName != scope.PrimaryKey() && len(field.SqlTag) > 0 && !field.IsIgnored { + if !field.IsPrimaryKey && len(field.SqlTag) > 0 && !field.IsIgnored { sqls = append(sqls, fmt.Sprintf("%v = %v", scope.Quote(field.DBName), scope.AddToVars(field.Value))) } } diff --git a/migration_test.go b/migration_test.go index 71fd831c..91761154 100644 --- a/migration_test.go +++ b/migration_test.go @@ -11,15 +11,14 @@ func runMigration() { fmt.Printf("Got error when try to delete table users, %+v\n", err) } - DB.Exec("drop table products;") - DB.Exec("drop table emails;") - DB.Exec("drop table addresses") - DB.Exec("drop table credit_cards") - DB.Exec("drop table roles") - DB.Exec("drop table companies") - DB.Exec("drop table animals") - DB.Exec("drop table user_languages") - DB.Exec("drop table languages") + for _, table := range []string{"animals", "user_languages"} { + DB.Exec(fmt.Sprintf("drop table %v;", table)) + } + + values := []interface{}{&Product{}, &Email{}, &Address{}, &CreditCard{}, &Company{}, &Role{}, &Language{}, &HNPost{}, &EngadgetPost{}} + for _, value := range values { + DB.DropTable(value) + } if err := DB.CreateTable(&Animal{}).Error; err != nil { panic(fmt.Sprintf("No error should happen when create table, but got %+v", err)) @@ -29,7 +28,7 @@ func runMigration() { panic(fmt.Sprintf("No error should happen when create table, but got %+v", err)) } - if err := DB.AutoMigrate(&Product{}, &Email{}, &Address{}, &CreditCard{}, &Company{}, &Role{}, &Language{}, &HNPost{}, &EngadgetPost{}).Error; err != nil { + if err := DB.AutoMigrate(values...).Error; err != nil { panic(fmt.Sprintf("No error should happen when create table, but got %+v", err)) } }