Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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] })

Expand Down
28 changes: 14 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
29 changes: 29 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"testing"
"time"
)

// GORM_REPO: https://github.com/go-gorm/gorm.git
Expand All @@ -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)
}
}
6 changes: 6 additions & 0 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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'"`
}
Loading