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
126 changes: 63 additions & 63 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,44 @@ name: tests

on:
push:
branches: [ master ]
branches: [master]
pull_request:
branches-ignore:
- 'gh-pages'
- "gh-pages"

jobs:
# Label of the container job
sqlite:
strategy:
matrix:
go: ['1.24']
go: ["1.24"]
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: 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: Check out code into the Go module directory
uses: actions/checkout@v2

- name: go mod pakcage cache
uses: actions/cache@v4
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ matrix.go }}-${{ hashFiles('go.mod') }}
- name: go mod pakcage cache
uses: actions/cache@v4
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ matrix.go }}-${{ hashFiles('go.mod') }}

- name: Tests
run: GORM_DIALECT=sqlite ./test.sh
- name: Tests
run: GORM_DIALECT=sqlite ./test.sh

mysql:
needs: sqlite
strategy:
matrix:
dbversion: ['mysql:latest'] # 'mysql:5.7', 'mysql:5.6'
go: ['1.24']
dbversion: ["mysql:latest"] # 'mysql:5.7', 'mysql:5.6'
go: ["1.24"]
platform: [ubuntu-latest]
runs-on: ${{ matrix.platform }}

Expand All @@ -61,29 +61,29 @@ jobs:
--health-retries 10

steps:
- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go }}
- 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: 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: go mod pakcage cache
uses: actions/cache@v4
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
- 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'
go: ['1.24']
dbversion: ["postgres:latest"] # 'postgres:11', 'postgres:10'
go: ["1.24"]
platform: [ubuntu-latest] # can not run in macOS and widnowsOS
runs-on: ${{ matrix.platform }}

Expand All @@ -105,28 +105,28 @@ jobs:
--health-retries 5

steps:
- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go }}
- 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: 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: go mod pakcage cache
uses: actions/cache@v4
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ matrix.go }}-${{ hashFiles('go.mod') }}

- name: Tests
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
- name: Tests
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.24']
go: ["1.24"]
platform: [ubuntu-latest] # can not run test in macOS and windows
runs-on: ${{ matrix.platform }}

Expand All @@ -149,19 +149,19 @@ jobs:
--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=sqlserver GORM_DSN="sqlserver://gorm:LoremIpsum86@localhost:9930?database=gorm" ./test.sh
- 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@v4
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ matrix.go }}-${{ hashFiles('go.mod') }}

- name: Tests
run: GORM_ENABLE_CACHE=true GORM_DIALECT=sqlserver GORM_DSN="sqlserver://gorm:LoremIpsum86@localhost:9930?database=gorm" ./test.sh
43 changes: 28 additions & 15 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"gorm.io/driver/sqlserver"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"gorm.io/gorm/schema"
)

var DB *gorm.DB
Expand All @@ -22,23 +23,24 @@ func init() {
if DB, err = OpenTestConnection(); err != nil {
log.Printf("failed to connect database, got error %v\n", err)
os.Exit(1)
} else {
sqlDB, err := DB.DB()
if err == nil {
err = sqlDB.Ping()
}
}

if err != nil {
log.Printf("failed to connect database, got error %v\n", err)
}
sqlDB, err := DB.DB()
if err == nil {
err = sqlDB.Ping()
}

RunMigrations()
if DB.Dialector.Name() == "sqlite" {
DB.Exec("PRAGMA foreign_keys = ON")
}
if err != nil {
log.Printf("failed to connect database, got error %v\n", err)
}

DB.Logger = DB.Logger.LogMode(logger.Info)
RunMigrations()
if DB.Dialector.Name() == "sqlite" {
DB.Exec("PRAGMA foreign_keys = ON")
}

DB.Logger = DB.Logger.LogMode(logger.Info)

}

func OpenTestConnection() (db *gorm.DB, err error) {
Expand Down Expand Up @@ -66,7 +68,17 @@ func OpenTestConnection() (db *gorm.DB, err error) {
if dbDSN == "" {
dbDSN = "sqlserver://gorm:LoremIpsum86@localhost:9930?database=gorm"
}
db, err = gorm.Open(sqlserver.Open(dbDSN), &gorm.Config{})
db, err = gorm.Open(sqlserver.Open(dbDSN), &gorm.Config{
NamingStrategy: schema.NamingStrategy{TablePrefix: "testing_schema."},
})
db.Exec(`
IF NOT EXISTS(
SELECT * FROM sys.schemas WHERE name = 'testing_schema'
)
BEGIN
EXEC('CREATE SCHEMA testing_schema')
END
`)
default:
log.Println("testing sqlite3...")
db, err = gorm.Open(sqlite.Open(filepath.Join(os.TempDir(), "gorm.db")), &gorm.Config{})
Expand All @@ -83,7 +95,8 @@ func OpenTestConnection() (db *gorm.DB, err error) {

func RunMigrations() {
var err error
allModels := []interface{}{&User{}, &Account{}, &Pet{}, &Company{}, &Toy{}, &Language{}}

allModels := []interface{}{&Company{}, &TestStruct{}}
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(allModels), func(i, j int) { allModels[i], allModels[j] = allModels[j], allModels[i] })

Expand Down
27 changes: 16 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@ go 1.23.0

toolchain go1.24.3

// For SQL Server
godebug (
x509negativeserial=1
)

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
)

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 @@ -26,13 +31,13 @@ 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
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/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
)
Expand Down
30 changes: 25 additions & 5 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,39 @@ 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

func TestGORM(t *testing.T) {
user := User{Name: "jinzhu"}
var result *gorm.DB

// Try inserting into DB without manually specifying identity column value
workTestStruct := TestStruct{Value: "hello"}
result = DB.Create(&workTestStruct)
if err := result.Error; err != nil {
t.Errorf("Failed to create workTestStruct: %+v", err)
}

DB.Create(&user)
var ts1 TestStruct
if err := DB.First(&ts1, workTestStruct.ID).Error; err != nil {
t.Errorf("Failed to read workTestStruct, got error: %v", err)
}

// Try again but specifying the identity column value.
// The insert should fail here due to the schema not being included in the `SET IDENTITY_INSERT` statement
failTestStruct := TestStruct{ID: 100, Value: "there"}
result = DB.Create(&failTestStruct)
if err := result.Error; err != nil {
t.Errorf("Failed to create failTestStruct: %+v", err)
}

var result User
if err := DB.First(&result, user.ID).Error; err != nil {
t.Errorf("Failed, got error: %v", err)
var ts2 TestStruct
if err := DB.First(&ts2, failTestStruct.ID).Error; err != nil {
t.Errorf("Failed to read failTestStruct, got error: %v", err)
}
}
7 changes: 7 additions & 0 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,10 @@ type Language struct {
Code string `gorm:"primarykey"`
Name string
}

type TestStruct struct {
ID uint `gorm:"primarykey,autoIncrement"`
CreatedAt time.Time
UpdatedAt time.Time
Value string
}
Loading