mirror of https://github.com/go-gorm/gorm.git
Refact code
This commit is contained in:
parent
d806b7084f
commit
f984bc8515
|
@ -3,18 +3,17 @@ package gorm_test
|
||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
type BasePost struct {
|
type BasePost struct {
|
||||||
|
Id int64
|
||||||
Title string
|
Title string
|
||||||
Url string
|
Url string
|
||||||
}
|
}
|
||||||
|
|
||||||
type HNPost struct {
|
type HNPost struct {
|
||||||
Id int64
|
|
||||||
BasePost `gorm:"embedded"`
|
BasePost `gorm:"embedded"`
|
||||||
Upvotes int32
|
Upvotes int32
|
||||||
}
|
}
|
||||||
|
|
||||||
type EngadgetPost struct {
|
type EngadgetPost struct {
|
||||||
Id int64
|
|
||||||
BasePost
|
BasePost
|
||||||
ImageUrl string
|
ImageUrl string
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,7 +26,7 @@ func Create(scope *Scope) {
|
||||||
var sqls, columns []string
|
var sqls, columns []string
|
||||||
|
|
||||||
for _, field := range scope.Fields() {
|
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))
|
columns = append(columns, scope.Quote(field.DBName))
|
||||||
sqls = append(sqls, scope.AddToVars(field.Value))
|
sqls = append(sqls, scope.AddToVars(field.Value))
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,7 +50,7 @@ func Update(scope *Scope) {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for _, field := range scope.Fields() {
|
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)))
|
sqls = append(sqls, fmt.Sprintf("%v = %v", scope.Quote(field.DBName), scope.AddToVars(field.Value)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,15 +11,14 @@ func runMigration() {
|
||||||
fmt.Printf("Got error when try to delete table users, %+v\n", err)
|
fmt.Printf("Got error when try to delete table users, %+v\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
DB.Exec("drop table products;")
|
for _, table := range []string{"animals", "user_languages"} {
|
||||||
DB.Exec("drop table emails;")
|
DB.Exec(fmt.Sprintf("drop table %v;", table))
|
||||||
DB.Exec("drop table addresses")
|
}
|
||||||
DB.Exec("drop table credit_cards")
|
|
||||||
DB.Exec("drop table roles")
|
values := []interface{}{&Product{}, &Email{}, &Address{}, &CreditCard{}, &Company{}, &Role{}, &Language{}, &HNPost{}, &EngadgetPost{}}
|
||||||
DB.Exec("drop table companies")
|
for _, value := range values {
|
||||||
DB.Exec("drop table animals")
|
DB.DropTable(value)
|
||||||
DB.Exec("drop table user_languages")
|
}
|
||||||
DB.Exec("drop table languages")
|
|
||||||
|
|
||||||
if err := DB.CreateTable(&Animal{}).Error; err != nil {
|
if err := DB.CreateTable(&Animal{}).Error; err != nil {
|
||||||
panic(fmt.Sprintf("No error should happen when create table, but got %+v", err))
|
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))
|
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))
|
panic(fmt.Sprintf("No error should happen when create table, but got %+v", err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue