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{}, &App{}, &Env{}}
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(allModels), func(i, j int) { allModels[i], allModels[j] = allModels[j], allModels[i] })

Expand Down
12 changes: 6 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ 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/mysql v1.6.0
gorm.io/driver/postgres v1.6.0
gorm.io/driver/sqlite v1.6.0
gorm.io/driver/sqlserver v1.6.0
gorm.io/gen v0.3.27
gorm.io/gorm v1.30.0
Expand All @@ -26,10 +26,10 @@ require (
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
github.com/microsoft/go-mssqldb v1.8.2 // 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/mod v0.25.0 // indirect
golang.org/x/sync v0.15.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
Expand Down
54 changes: 48 additions & 6 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,61 @@ package main

import (
"testing"

"gorm.io/gorm"
)

// GORM_REPO: https://github.com/go-gorm/gorm.git
// GORM_BRANCH: master
// TEST_DRIVERS: sqlite, mysql, postgres, sqlserver
// TEST_DRIVERS: mysql
// __TEST_DRIVERS__: sqlite, mysql, postgres, sqlserver

func TestGORM(t *testing.T) {
user := User{Name: "jinzhu"}
appInitial := &App{
Name: "foo",
Envs: []*Env{
{Key: "KEY1", Value: "value1"},
},
}
DB.Create(appInitial)
t.Logf("appInitial: %+v", appInitial)
for i, env := range appInitial.Envs {
t.Logf(" env[%d]: %+v", i, env)
}

appUpdated := &App{
ID: appInitial.ID,
Name: "foo",
Envs: []*Env{
{Key: "KEY1", Value: "value1-updated"},
{Key: "KEY2", Value: "value2-added"},
},
}
DB.Session(&gorm.Session{FullSaveAssociations: true}).Updates(appUpdated)
t.Logf("appUpdated: %+v", appUpdated)
for i, env := range appUpdated.Envs {
t.Logf(" env[%d]: %+v", i, env)
}

appFetched := &App{ID: appInitial.ID}
DB.Preload("Envs").First(appFetched)
t.Logf("appFetched: %+v", appFetched)
for i, env := range appFetched.Envs {
t.Logf(" env[%d]: %+v", i, env)
}

DB.Create(&user)
for i := range appFetched.Envs {
envFetched := appFetched.Envs[i]
envUpdated := appUpdated.Envs[i]

var result User
if err := DB.First(&result, user.ID).Error; err != nil {
t.Errorf("Failed, got error: %v", err)
if envFetched.Key != envUpdated.Key {
t.Errorf("Expected env key %s, got %s", envFetched.Key, envUpdated.Key)
}
if envFetched.Value != envUpdated.Value {
t.Errorf("Expected env value %s, got %s", envFetched.Value, envUpdated.Value)
}
if envFetched.ID != envUpdated.ID {
t.Errorf("Expected env ID %d, got %d", envFetched.ID, envUpdated.ID)
}
}
}
14 changes: 14 additions & 0 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,17 @@ type Language struct {
Code string `gorm:"primarykey"`
Name string
}

type App struct {
ID int
Name string

Envs []*Env `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
}

type Env struct {
ID int
AppID int `gorm:"uniqueIndex:idx_app_id_key"`
Key string `gorm:"size:64; uniqueIndex:idx_app_id_key"`
Value string
}
Loading