From 3a528413d790155452af40cee615d94540f9db0b Mon Sep 17 00:00:00 2001 From: Zacama Date: Thu, 4 Aug 2022 14:24:05 +0800 Subject: [PATCH] try to discribe issue --- db.go | 2 +- go.mod | 12 +++++++----- main_test.go | 31 ++++++++++++++++++++++++++----- models.go | 8 ++++++++ test.sh | 18 +++++++++--------- 5 files changed, 51 insertions(+), 20 deletions(-) diff --git a/db.go b/db.go index ccab03ed..1f946c25 100644 --- a/db.go +++ b/db.go @@ -83,7 +83,7 @@ func OpenTestConnection() (db *gorm.DB, err error) { func RunMigrations() { var err error - allModels := []interface{}{&User{}, &Account{}, &Pet{}, &Company{}, &Toy{}, &Language{}} + allModels := []interface{}{&User{}, &Account{}, &Pet{}, &Company{}, &Toy{}, &Language{}, &ProcessTable{}} rand.Seed(time.Now().UnixNano()) rand.Shuffle(len(allModels), func(i, j int) { allModels[i], allModels[j] = allModels[j], allModels[i] }) diff --git a/go.mod b/go.mod index 4da7ed1f..974c6f41 100644 --- a/go.mod +++ b/go.mod @@ -3,14 +3,16 @@ module gorm.io/playground go 1.16 require ( + github.com/denisenkom/go-mssqldb v0.12.2 // indirect github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect github.com/jackc/pgx/v4 v4.16.1 // indirect - golang.org/x/crypto v0.0.0-20220507011949-2cf3adece122 // indirect - gorm.io/driver/mysql v1.3.3 - gorm.io/driver/postgres v1.3.5 - gorm.io/driver/sqlite v1.3.2 + github.com/mattn/go-sqlite3 v1.14.14 // indirect + golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect + gorm.io/driver/mysql v1.3.5 + gorm.io/driver/postgres v1.3.8 + gorm.io/driver/sqlite v1.3.6 gorm.io/driver/sqlserver v1.3.2 - gorm.io/gorm v1.23.4 + gorm.io/gorm v1.23.8 ) replace gorm.io/gorm => ./gorm diff --git a/main_test.go b/main_test.go index 60a388f7..aaeb4237 100644 --- a/main_test.go +++ b/main_test.go @@ -1,6 +1,8 @@ package main import ( + "gorm.io/gorm" + "gorm.io/gorm/clause" "testing" ) @@ -9,12 +11,31 @@ import ( // TEST_DRIVERS: sqlite, mysql, postgres, sqlserver func TestGORM(t *testing.T) { - user := User{Name: "jinzhu"} + // Insert + DB.Transaction(func(tx *gorm.DB) error { + process := ProcessTable{Name: "test"} + if err := tx.Create(&process).Error; err != nil { + t.Errorf("Failed process !!!!!!!, got error: %v", err) + return err + } + return nil + }) - DB.Create(&user) + // Ensure record does exist. + k := &ProcessTable{} + DB.First(k) + t.Log("My Name is --------------------------", k.Name) + var processes []ProcessTable - var result User - if err := DB.First(&result, user.ID).Error; err != nil { - t.Errorf("Failed, got error: %v", err) + // Try to Delete and return the record using Clauses(clause.Returning{}) + res := DB.Debug().Clauses(clause.Returning{}).Where(&ProcessTable{Name: "test"}).Delete(&processes) + if res.Error != nil { + t.Errorf("Failed process !!!!!!!, got error: %v", res.Error) + return } + if len(processes) == 0 { + t.Errorf("Empty processes !!!!!!!") + return + } + t.Log(processes[0].Name) } diff --git a/models.go b/models.go index 692a6842..346fd4c4 100644 --- a/models.go +++ b/models.go @@ -58,3 +58,11 @@ type Language struct { Code string `gorm:"primarykey"` Name string } + +type ProcessTable struct { + ID uint64 `gorm:"column:f_id;primaryKey;autoIncrement;not null;comment:'主键'"` + Index string `gorm:"column:f_index;type:char(36);uniqueIndex;not null;comment:'UUID4'"` + Name string `gorm:"column:f_name;type:varchar(128);uniqueIndex;not null;comment:''"` + CreateTime time.Time `gorm:"column:f_create_time;autoCreateTime;not null;comment:''"` + UpdateTime time.Time `gorm:"column:f_update_time;autoUpdateTime;not null;comment:''"` +} diff --git a/test.sh b/test.sh index 16901b05..90afb939 100755 --- a/test.sh +++ b/test.sh @@ -1,15 +1,15 @@ #!/bin/bash -e -dialects=("sqlite" "mysql" "postgres" "sqlserver") +dialects=("sqlite" "mysql") -if [ "$GORM_ENABLE_CACHE" = "" ] -then -rm -rf gorm -fi - -[ -d gorm ] || (echo "git clone --depth 1 -b $(cat main_test.go | grep GORM_BRANCH | awk '{print $3}') $(cat main_test.go | grep GORM_REPO | awk '{print $3}')"; git clone --depth 1 -b $(cat main_test.go | grep GORM_BRANCH | awk '{print $3}') $(cat main_test.go | grep GORM_REPO | awk '{print $3}')) - -go get -u ./... +#if [ "$GORM_ENABLE_CACHE" = "" ] +#then +#rm -rf gorm +#fi +# +#[ -d gorm ] || (echo "git clone --depth 1 -b $(cat main_test.go | grep GORM_BRANCH | awk '{print $3}') $(cat main_test.go | grep GORM_REPO | awk '{print $3}')"; git clone --depth 1 -b $(cat main_test.go | grep GORM_BRANCH | awk '{print $3}') $(cat main_test.go | grep GORM_REPO | awk '{print $3}')) +# +#go get -u ./... for dialect in "${dialects[@]}" ; do if [ "$GORM_DIALECT" = "" ] || [ "$GORM_DIALECT" = "${dialect}" ]