From 9445e8334b3e7c6a8ae410bac49b7ebfcc7c7e9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Sat, 7 Sep 2024 19:53:20 +0800 Subject: [PATCH 1/7] feat: add generated by default --- go.mod | 2 -- models.go | 30 +++++++++++++++++++++--------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/go.mod b/go.mod index 159394d4..f156a61d 100644 --- a/go.mod +++ b/go.mod @@ -31,5 +31,3 @@ require ( gorm.io/hints v1.1.0 // indirect gorm.io/plugin/dbresolver v1.5.0 // indirect ) - -replace gorm.io/gorm => ./gorm diff --git a/models.go b/models.go index 692a6842..865af632 100644 --- a/models.go +++ b/models.go @@ -12,7 +12,10 @@ import ( // He speaks many languages (many to many) and has many friends (many to many - single-table) // His pet also has one Toy (has one - polymorphic) type User struct { - gorm.Model + ID uint `gorm:"primarykey;type:bigint generated by default as identity;default:(-)"` + CreatedAt time.Time + UpdatedAt time.Time + DeletedAt gorm.DeletedAt `gorm:"index"` Name string Age uint Birthday *time.Time @@ -30,20 +33,29 @@ type User struct { } type Account struct { - gorm.Model - UserID sql.NullInt64 - Number string + ID uint `gorm:"primarykey;type:bigint generated by default as identity;default:(-)"` + CreatedAt time.Time + UpdatedAt time.Time + DeletedAt gorm.DeletedAt `gorm:"index"` + UserID sql.NullInt64 + Number string } type Pet struct { - gorm.Model - UserID *uint - Name string - Toy Toy `gorm:"polymorphic:Owner;"` + ID uint `gorm:"primarykey;type:bigint generated by default as identity;default:(-)"` + CreatedAt time.Time + UpdatedAt time.Time + DeletedAt gorm.DeletedAt `gorm:"index"` + UserID *uint + Name string + Toy Toy `gorm:"polymorphic:Owner;"` } type Toy struct { - gorm.Model + ID uint `gorm:"primarykey;type:bigint generated by default as identity;default:(-)"` + CreatedAt time.Time + UpdatedAt time.Time + DeletedAt gorm.DeletedAt `gorm:"index"` Name string OwnerID string OwnerType string From 6db9b0cce341d5ccf3ee219a6679af94c12f33ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Sat, 7 Sep 2024 19:56:36 +0800 Subject: [PATCH 2/7] feat: update test --- .github/workflows/tests.yml | 72 ------------------------------------- 1 file changed, 72 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index e3ea1970..a17660ae 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -8,78 +8,7 @@ on: - 'gh-pages' jobs: - # Label of the container job - sqlite: - strategy: - matrix: - go: ['1.21'] - platform: [ubuntu-latest] - runs-on: ${{ matrix.platform }} - - steps: - - name: Set up Go 1.x - uses: actions/setup-go@v2 - with: - go-version: ${{ matrix.go }} - - - name: Check out code into the Go module directory - uses: actions/checkout@v2 - - - name: go mod pakcage cache - uses: actions/cache@v2 - with: - path: ~/go/pkg/mod - key: ${{ runner.os }}-go-${{ matrix.go }}-${{ hashFiles('go.mod') }} - - - name: Tests - run: GORM_DIALECT=sqlite ./test.sh - - mysql: - needs: sqlite - strategy: - matrix: - dbversion: ['mysql:latest'] # 'mysql:5.7', 'mysql:5.6' - go: ['1.21'] - platform: [ubuntu-latest] - runs-on: ${{ matrix.platform }} - - services: - mysql: - image: ${{ matrix.dbversion }} - env: - MYSQL_DATABASE: gorm - MYSQL_USER: gorm - MYSQL_PASSWORD: gorm - MYSQL_RANDOM_ROOT_PASSWORD: "yes" - ports: - - 9910:3306 - options: >- - --health-cmd "mysqladmin ping -ugorm -pgorm" - --health-interval 10s - --health-start-period 10s - --health-timeout 5s - --health-retries 10 - - steps: - - name: Set up Go 1.x - uses: actions/setup-go@v2 - with: - go-version: ${{ matrix.go }} - - - name: Check out code into the Go module directory - uses: actions/checkout@v2 - - - name: go mod pakcage cache - uses: actions/cache@v2 - with: - path: ~/go/pkg/mod - key: ${{ runner.os }}-go-${{ matrix.go }}-${{ hashFiles('go.mod') }} - - - name: Tests - run: GORM_ENABLE_CACHE=true GORM_DIALECT=mysql GORM_DSN="gorm:gorm@tcp(localhost:9910)/gorm?charset=utf8&parseTime=True" ./test.sh - postgres: - needs: sqlite strategy: matrix: dbversion: ['postgres:latest'] # 'postgres:11', 'postgres:10' @@ -123,7 +52,6 @@ jobs: run: GORM_ENABLE_CACHE=true GORM_DIALECT=postgres GORM_DSN="user=gorm password=gorm dbname=gorm host=localhost port=9920 sslmode=disable TimeZone=Asia/Shanghai" ./test.sh sqlserver: - needs: sqlite strategy: matrix: go: ['1.21'] From f0bda8835539501dfbf991404d3bc38769f330bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Sat, 7 Sep 2024 19:58:51 +0800 Subject: [PATCH 3/7] feat: update test --- .github/workflows/tests.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index a17660ae..1ed23998 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -12,7 +12,7 @@ jobs: strategy: matrix: dbversion: ['postgres:latest'] # 'postgres:11', 'postgres:10' - go: ['1.21'] + go: ['stable'] platform: [ubuntu-latest] # can not run in macOS and widnowsOS runs-on: ${{ matrix.platform }} @@ -54,7 +54,7 @@ jobs: sqlserver: strategy: matrix: - go: ['1.21'] + go: ['stable'] platform: [ubuntu-latest] # can not run test in macOS and windows runs-on: ${{ matrix.platform }} From 0f2fe620dc3b99592dd0a345102c09dc8a367a67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Sat, 7 Sep 2024 20:00:11 +0800 Subject: [PATCH 4/7] feat: update test --- .github/workflows/tests.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 1ed23998..783d0f3b 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -12,7 +12,7 @@ jobs: strategy: matrix: dbversion: ['postgres:latest'] # 'postgres:11', 'postgres:10' - go: ['stable'] + go: ['1.23'] platform: [ubuntu-latest] # can not run in macOS and widnowsOS runs-on: ${{ matrix.platform }} @@ -54,7 +54,7 @@ jobs: sqlserver: strategy: matrix: - go: ['stable'] + go: ['1.23'] platform: [ubuntu-latest] # can not run test in macOS and windows runs-on: ${{ matrix.platform }} From 90e9ae9e3a99c4d2f6d93e19735c7f52a2f722f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Sat, 7 Sep 2024 20:02:03 +0800 Subject: [PATCH 5/7] feat: update test --- gen.go | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/gen.go b/gen.go index 3127d5d6..06ab7d0f 100644 --- a/gen.go +++ b/gen.go @@ -1,21 +1 @@ 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() -} From 912dd7a69283d9a519fe92ce7f56b539882e5fcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Sat, 7 Sep 2024 20:05:17 +0800 Subject: [PATCH 6/7] feat: update test --- db.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/db.go b/db.go index ccab03ed..cc6c0025 100644 --- a/db.go +++ b/db.go @@ -72,11 +72,7 @@ func OpenTestConnection() (db *gorm.DB, err error) { db, err = gorm.Open(sqlite.Open(filepath.Join(os.TempDir(), "gorm.db")), &gorm.Config{}) } - if debug := os.Getenv("DEBUG"); debug == "true" { - db.Logger = db.Logger.LogMode(logger.Info) - } else if debug == "false" { - db.Logger = db.Logger.LogMode(logger.Silent) - } + db.Logger = db.Logger.LogMode(logger.Info) return } From 311334acb67457bdfaff1c3da59930faf36e4194 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Sat, 7 Sep 2024 20:08:59 +0800 Subject: [PATCH 7/7] feat: update test --- go.mod | 2 ++ 1 file changed, 2 insertions(+) diff --git a/go.mod b/go.mod index f156a61d..159394d4 100644 --- a/go.mod +++ b/go.mod @@ -31,3 +31,5 @@ require ( gorm.io/hints v1.1.0 // indirect gorm.io/plugin/dbresolver v1.5.0 // indirect ) + +replace gorm.io/gorm => ./gorm