diff --git a/db.go b/db.go index ccab03ed..3d1360ee 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{}, &Price{}} 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 d1ba4316..5a4790f4 100644 --- a/go.mod +++ b/go.mod @@ -5,17 +5,17 @@ go 1.23.0 toolchain go1.24.3 require ( - gorm.io/driver/mysql v1.5.7 - gorm.io/driver/postgres v1.5.11 - gorm.io/driver/sqlite v1.5.7 - gorm.io/driver/sqlserver v1.6.0 + gorm.io/driver/mysql v1.6.0 + gorm.io/driver/postgres v1.6.0 + gorm.io/driver/sqlite v1.6.0 + gorm.io/driver/sqlserver v1.6.1 gorm.io/gen v0.3.27 gorm.io/gorm v1.30.0 ) require ( filippo.io/edwards25519 v1.1.0 // indirect - github.com/go-sql-driver/mysql v1.9.2 // indirect + github.com/go-sql-driver/mysql v1.9.3 // indirect github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect github.com/golang-sql/sqlexp v0.1.0 // indirect github.com/google/uuid v1.6.0 // indirect @@ -25,16 +25,16 @@ require ( github.com/jackc/puddle/v2 v2.2.2 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect - github.com/mattn/go-sqlite3 v1.14.28 // indirect - github.com/microsoft/go-mssqldb v1.8.1 // indirect - golang.org/x/crypto v0.38.0 // indirect - golang.org/x/mod v0.24.0 // indirect - golang.org/x/sync v0.14.0 // indirect - golang.org/x/text v0.25.0 // indirect - golang.org/x/tools v0.33.0 // indirect - gorm.io/datatypes v1.2.5 // indirect + github.com/mattn/go-sqlite3 v1.14.29 // indirect + github.com/microsoft/go-mssqldb v1.9.2 // indirect + golang.org/x/crypto v0.40.0 // indirect + golang.org/x/mod v0.26.0 // indirect + golang.org/x/sync v0.16.0 // indirect + golang.org/x/text v0.27.0 // indirect + golang.org/x/tools v0.35.0 // indirect + gorm.io/datatypes v1.2.6 // indirect gorm.io/hints v1.1.2 // indirect - gorm.io/plugin/dbresolver v1.6.0 // indirect + gorm.io/plugin/dbresolver v1.6.2 // indirect ) replace gorm.io/gorm => ./gorm diff --git a/main_test.go b/main_test.go index 60a388f7..d44e7724 100644 --- a/main_test.go +++ b/main_test.go @@ -2,6 +2,7 @@ package main import ( "testing" + "time" ) // GORM_REPO: https://github.com/go-gorm/gorm.git @@ -18,3 +19,31 @@ func TestGORM(t *testing.T) { t.Errorf("Failed, got error: %v", err) } } + +func TestUpdateTime(t *testing.T) { + prices := []Price{ + {ProductId: 1, Price: 100, SomeTime: time.Now()}, + {ProductId: 2, Price: 150, SomeTime: time.Now()}, + } + + if err := DB.Save(prices).Error; err != nil { + t.Errorf("Failed to save: %v", err) + } + + // Change time for the second model, and persist models + tim := time.Now().Add(-10000 * time.Hour) + prices[1].SomeTime = tim + + if err := DB.Save(prices).Error; err != nil { + t.Errorf("Failed to save: %v", err) + } + + var p Price + if err := DB.First(&p, 2).Error; err != nil { + t.Errorf("Failed to get model: %v", err) + } + + if !p.SomeTime.Equal(tim) { + t.Errorf("Time was not updated: %v != %v", p.SomeTime, tim) + } +} diff --git a/models.go b/models.go index 692a6842..80e72597 100644 --- a/models.go +++ b/models.go @@ -58,3 +58,9 @@ type Language struct { Code string `gorm:"primarykey"` Name string } + +type Price struct { + ProductId int `gorm:"primaryKey"` + Price int `gorm:"default:1"` + SomeTime time.Time `gorm:"default:'2015-10-22T14:00:00Z'"` +}