From 62eeb71c682963ab874327ac4c4d75bc6c83da89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9F=A5=E4=BC=9F=E6=98=8C?= <806248599@qq.com> Date: Wed, 17 Jul 2024 13:44:23 +0800 Subject: [PATCH 1/2] data race with cycle relation --- db.go | 2 +- main_test.go | 20 ++++++++++++++------ models.go | 25 ++++++++++++++++++++----- 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/db.go b/db.go index ccab03ed..6c7c0a3a 100644 --- a/db.go +++ b/db.go @@ -32,7 +32,7 @@ func init() { log.Printf("failed to connect database, got error %v\n", err) } - RunMigrations() + //RunMigrations() if DB.Dialector.Name() == "sqlite" { DB.Exec("PRAGMA foreign_keys = ON") } diff --git a/main_test.go b/main_test.go index 60a388f7..313a32a6 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 @@ -9,12 +10,19 @@ import ( // TEST_DRIVERS: sqlite, mysql, postgres, sqlserver func TestGORM(t *testing.T) { - user := User{Name: "jinzhu"} - - DB.Create(&user) + for i := 0; i < 1; i++ { + go func() { + var result User + DB.First(&result) + }() + } - var result User - if err := DB.First(&result, user.ID).Error; err != nil { - t.Errorf("Failed, got error: %v", err) + for i := 0; i < 1; i++ { + go func() { + var result Account + DB.First(&result) + }() } + + time.Sleep(time.Second) } diff --git a/models.go b/models.go index 692a6842..3a5cb99f 100644 --- a/models.go +++ b/models.go @@ -16,7 +16,6 @@ type User struct { Name string Age uint Birthday *time.Time - Account Account Pets []*Pet Toys []Toy `gorm:"polymorphic:Owner"` CompanyID *int @@ -27,12 +26,26 @@ type User struct { Languages []Language `gorm:"many2many:UserSpeak"` Friends []*User `gorm:"many2many:user_friends"` Active bool + DepotID *uint + Depot Depot + Account Account } type Account struct { gorm.Model - UserID sql.NullInt64 - Number string + UserID sql.NullInt64 + Number string + User *User + DepotID *uint + Depot Depot +} + +type Depot struct { + ID int + Name string + User *User + CompanyID *int + Company Company } type Pet struct { @@ -50,8 +63,10 @@ type Toy struct { } type Company struct { - ID int - Name string + ID int + Name string + UserID *int + User *User } type Language struct { From 68047dd8062406adaecdd90525041cac749a572b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9F=A5=E4=BC=9F=E6=98=8C?= <806248599@qq.com> Date: Wed, 17 Jul 2024 14:01:58 +0800 Subject: [PATCH 2/2] data race with cycle relation --- gen.go | 21 --------------------- go.mod | 33 ++++++++++++++++++--------------- 2 files changed, 18 insertions(+), 36 deletions(-) delete mode 100644 gen.go diff --git a/gen.go b/gen.go deleted file mode 100644 index 3127d5d6..00000000 --- a/gen.go +++ /dev/null @@ -1,21 +0,0 @@ -package main - -import ( - "gorm.io/gen" - "gorm.io/gen/examples/dal" -) - -func generate() { - g := gen.NewGenerator(gen.Config{ - OutPath: "./dal/query", - Mode: gen.WithDefaultQuery, /*WithQueryInterface, WithoutContext*/ - - WithUnitTest: true, - }) - g.UseDB(dal.DB) - - g.ApplyBasic(Company{}, Language{}) // Associations - g.ApplyBasic(g.GenerateModel("user"), g.GenerateModelAs("account", "AccountInfo")) - - g.Execute() -} diff --git a/go.mod b/go.mod index 159394d4..beadfbae 100644 --- a/go.mod +++ b/go.mod @@ -3,30 +3,33 @@ module gorm.io/playground go 1.20 require ( - gorm.io/driver/mysql v1.5.2 - gorm.io/driver/postgres v1.5.2 - gorm.io/driver/sqlite v1.5.3 - gorm.io/driver/sqlserver v1.5.1 + gorm.io/driver/mysql v1.5.7 + gorm.io/driver/postgres v1.5.9 + gorm.io/driver/sqlite v1.5.6 + gorm.io/driver/sqlserver v1.5.3 gorm.io/gen v0.3.25 - gorm.io/gorm v1.25.4 + gorm.io/gorm v1.25.11 ) require ( - github.com/go-sql-driver/mysql v1.7.1 // indirect + filippo.io/edwards25519 v1.1.0 // indirect + github.com/go-sql-driver/mysql v1.8.1 // indirect github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect github.com/golang-sql/sqlexp v0.1.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect - github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect - github.com/jackc/pgx/v5 v5.4.3 // indirect + github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect + github.com/jackc/pgx/v5 v5.6.0 // indirect + github.com/jackc/puddle/v2 v2.2.1 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect - github.com/mattn/go-sqlite3 v1.14.17 // indirect - github.com/microsoft/go-mssqldb v1.5.0 // indirect - golang.org/x/crypto v0.14.0 // indirect - golang.org/x/mod v0.14.0 // indirect - golang.org/x/sys v0.14.0 // indirect - golang.org/x/text v0.13.0 // indirect - golang.org/x/tools v0.15.0 // indirect + github.com/mattn/go-sqlite3 v1.14.22 // indirect + github.com/microsoft/go-mssqldb v1.7.2 // indirect + golang.org/x/crypto v0.25.0 // indirect + golang.org/x/mod v0.17.0 // indirect + golang.org/x/sync v0.7.0 // indirect + golang.org/x/sys v0.22.0 // indirect + golang.org/x/text v0.16.0 // indirect + golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect gorm.io/datatypes v1.1.1-0.20230130040222-c43177d3cf8c // indirect gorm.io/hints v1.1.0 // indirect gorm.io/plugin/dbresolver v1.5.0 // indirect