Compare commits

...

14 Commits

Author SHA1 Message Date
贾一饼
49eaeacb89
optimize: field.ReflectValueOf (#7530)
Some checks failed
tests / mysql (mysql:5.7, 1.24, ubuntu-latest) (push) Failing after 59s
tests / mysql (mysql:8, 1.23, ubuntu-latest) (push) Failing after 59s
tests / mysql (mysql:8, 1.24, ubuntu-latest) (push) Failing after 5s
tests / sqlite (1.23, ubuntu-latest) (push) Failing after 1m26s
tests / mysql (mysql:9, 1.23, ubuntu-latest) (push) Failing after 36s
tests / mysql (mysql:9, 1.24, ubuntu-latest) (push) Failing after 32s
tests / mariadb (mariadb:latest, 1.24, ubuntu-latest) (push) Failing after 9s
tests / mariadb (mariadb:latest, 1.23, ubuntu-latest) (push) Failing after 22s
tests / postgres (postgres:13, 1.23, ubuntu-latest) (push) Failing after 24s
tests / mysql (mysql:5.7, 1.23, ubuntu-latest) (push) Failing after 2m1s
tests / postgres (postgres:14, 1.23, ubuntu-latest) (push) Failing after 20s
tests / postgres (postgres:14, 1.24, ubuntu-latest) (push) Failing after 6s
tests / postgres (postgres:15, 1.24, ubuntu-latest) (push) Failing after 14s
tests / postgres (postgres:15, 1.23, ubuntu-latest) (push) Failing after 29s
tests / postgres (postgres:latest, 1.23, ubuntu-latest) (push) Failing after 26s
tests / postgres (postgres:latest, 1.24, ubuntu-latest) (push) Failing after 18s
tests / postgres (postgres:13, 1.24, ubuntu-latest) (push) Failing after 2m55s
tests / sqlite (1.24, ubuntu-latest) (push) Failing after 4m53s
tests / tidb (v6.5.0, 1.24, ubuntu-latest) (push) Failing after 2m6s
golangci-lint / lint (push) Failing after 6m41s
tests / gaussdb (opengauss/opengauss:7.0.0-RC1.B023, 1.23, ubuntu-latest) (push) Failing after 3m31s
tests / gaussdb (opengauss/opengauss:7.0.0-RC1.B023, 1.24, ubuntu-latest) (push) Failing after 3m38s
tests / sqlserver (1.23, ubuntu-latest) (push) Failing after 7m36s
tests / tidb (v6.5.0, 1.23, ubuntu-latest) (push) Failing after 12m41s
tests / sqlserver (1.24, ubuntu-latest) (push) Failing after 14m12s
Stale / stale (push) Successful in 6s
Close Missing Playground issues / stale (push) Successful in 6s
Close invalid questions issues / stale (push) Successful in 6s
2025-07-23 13:02:49 +08:00
贾一饼
52b4744410
optimize: performance optimization (#7526) 2025-07-22 18:32:28 +08:00
Jinzhu
9af6d510b5 Fix query when map keys include table-qualified column names, close #7507 2025-07-22 14:21:04 +08:00
Jinzhu
c63374f5d1 Don't request LastInsertID from database if not necessary, close #7469 2025-07-21 17:55:20 +08:00
jc
b9c7e562b0
fix(schema): check the hook function parameter type (#7468)
* fix(schema): Check the callback function parameter type

* fix log

* fix
2025-07-21 17:06:16 +08:00
Riseif
985940f0d8
should check inner condition length (#7512) 2025-07-21 11:57:12 +08:00
moseszane168
991c2d4891
Add GaussDB Database Support (#7508)
* support gaussdb

* use github CI

* change function name

* use gorm.io/driver/gaussdb

---------

Co-authored-by: bing.ma <bing.ma@daocloud.io>
2025-07-21 10:46:58 +08:00
Jinzhu
751a6dde7a
Call after initialize for gorm.Config (#7518) 2025-07-15 12:05:03 +08:00
贾一饼
2f4925e017
A little optimization for filed.ValueOf (#7499)
Co-authored-by: 贾一饼 <Boyang.Liang@apulis.com>
2025-07-07 11:15:10 +08:00
Eshan-Jogwar
1e8baf5459
fixes #7486 (#7492)
* fixes #7486

* Added A test case for subset changes of model

* completed the test file for check_subset_model_change_test.go
2025-06-25 11:11:08 +08:00
Salent Olivick
842ee527eb
fix decimal migrate error.(#7450) (#7450)
Signed-off-by: Chise1 <chise123@live.com>
2025-06-06 10:35:01 +08:00
enomotodev
23c0d7cf05
test: update MySQL test matrix to use official images and add 9.0, 8.4 versions (#7476)
* test: update MySQL test matrix to use official images and add 9.0, 8.4 versions

* test: use major version tags for MySQL test matrix
2025-06-06 10:10:23 +08:00
Jinzhu
718eae4fdd fix tests for mysql 9.0 2025-06-05 19:34:13 +08:00
Jinzhu
49b01a3e93 Fix Generics Scan, close https://github.com/go-gorm/playground/pull/803 2025-05-29 14:23:57 +08:00
26 changed files with 930 additions and 139 deletions

View File

@ -41,7 +41,7 @@ jobs:
mysql:
strategy:
matrix:
dbversion: ['mysql/mysql-server:latest', 'mysql:5.7']
dbversion: ['mysql:9', 'mysql:8', 'mysql:5.7']
go: ['1.23', '1.24']
platform: [ubuntu-latest]
runs-on: ${{ matrix.platform }}
@ -240,3 +240,71 @@ jobs:
- name: Tests
run: GITHUB_ACTION=true GORM_DIALECT=tidb GORM_DSN="root:@tcp(localhost:9940)/test?charset=utf8&parseTime=True&loc=Local" ./tests/tests_all.sh
gaussdb:
strategy:
matrix:
dbversion: ['opengauss/opengauss:7.0.0-RC1.B023']
go: ['1.23', '1.24']
platform: [ubuntu-latest] # can not run in macOS and Windows
runs-on: ${{ matrix.platform }}
services:
gaussdb:
image: ${{ matrix.dbversion }}
env:
# GaussDB has password limitations
GS_PASSWORD: Gaussdb@123
TZ: Asia/Shanghai
ports:
- 9950:5432
steps:
- name: Set up Go 1.x
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go }}
- name: Check out code into the Go module directory
uses: actions/checkout@v4
- name: Waiting for GaussDB to be ready
run: |
container_name=$(docker ps --filter "ancestor=opengauss/opengauss:7.0.0-RC1.B023" --format "{{.Names}}")
if [ -z "$container_name" ]; then
echo "Error: failed to find a container created from the 'opengauss/opengauss:7.0.0-RC1.B023' image."
exit 1
fi
max_retries=12
retry_count=0
if [ -t 0 ]; then
TTY_FLAG="-t"
else
TTY_FLAG=""
fi
while [ $retry_count -lt $max_retries ]; do
if docker exec -i "${container_name}" bash -c "su - omm -c 'gsql -U omm -c \"select 1;\"'"
then
echo "Creating database gorm..."
sql_file='/tmp/create_database.sql'
echo "CREATE DATABASE gorm DBCOMPATIBILITY 'PG';" > ${sql_file}
docker cp "${sql_file}" "${container_name}":"${sql_file}"
docker exec -i ${TTY_FLAG} "${container_name}" bash -c "su - omm -c 'gsql -U omm -f ${sql_file}'"
echo "Database initialization completed."
break
fi
echo "Waiting for database to be ready... (attempt $((retry_count + 1))/$max_retries)"
sleep 10
((++retry_count))
done
exit 0
- name: go mod package cache
uses: actions/cache@v4
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ matrix.go }}-${{ hashFiles('tests/go.mod') }}
- name: Tests
run: GITHUB_ACTION=true GORM_DIALECT=gaussdb GORM_DSN="user=gaussdb password=Gaussdb@123 dbname=gorm host=localhost port=9950 sslmode=disable TimeZone=Asia/Shanghai" ./tests/tests_all.sh

View File

@ -53,9 +53,13 @@ func Create(config *Config) func(db *gorm.DB) {
if _, ok := db.Statement.Clauses["RETURNING"]; !ok {
fromColumns := make([]clause.Column, 0, len(db.Statement.Schema.FieldsWithDefaultDBValue))
for _, field := range db.Statement.Schema.FieldsWithDefaultDBValue {
fromColumns = append(fromColumns, clause.Column{Name: field.DBName})
if field.Readable {
fromColumns = append(fromColumns, clause.Column{Name: field.DBName})
}
}
if len(fromColumns) > 0 {
db.Statement.AddClause(clause.Returning{Columns: fromColumns})
}
db.Statement.AddClause(clause.Returning{Columns: fromColumns})
}
}
}
@ -122,6 +126,16 @@ func Create(config *Config) func(db *gorm.DB) {
pkFieldName = "@id"
)
if db.Statement.Schema != nil {
if db.Statement.Schema.PrioritizedPrimaryField == nil ||
!db.Statement.Schema.PrioritizedPrimaryField.HasDefaultValue ||
!db.Statement.Schema.PrioritizedPrimaryField.Readable {
return
}
pkField = db.Statement.Schema.PrioritizedPrimaryField
pkFieldName = db.Statement.Schema.PrioritizedPrimaryField.DBName
}
insertID, err := result.LastInsertId()
insertOk := err == nil && insertID > 0
@ -132,14 +146,6 @@ func Create(config *Config) func(db *gorm.DB) {
return
}
if db.Statement.Schema != nil {
if db.Statement.Schema.PrioritizedPrimaryField == nil || !db.Statement.Schema.PrioritizedPrimaryField.HasDefaultValue {
return
}
pkField = db.Statement.Schema.PrioritizedPrimaryField
pkFieldName = db.Statement.Schema.PrioritizedPrimaryField.DBName
}
// append @id column with value for auto-increment primary key
// the @id value is correct, when: 1. without setting auto-increment primary key, 2. database AutoIncrementIncrement = 1
switch values := db.Statement.Dest.(type) {

View File

@ -567,7 +567,7 @@ func (g execG[T]) First(ctx context.Context) (T, error) {
func (g execG[T]) Scan(ctx context.Context, result interface{}) error {
var r T
err := g.g.apply(ctx).Model(r).Find(&result).Error
err := g.g.apply(ctx).Model(r).Find(result).Error
return err
}

View File

@ -137,6 +137,14 @@ func Open(dialector Dialector, opts ...Option) (db *DB, err error) {
return isConfig && !isConfig2
})
if len(opts) > 0 {
if c, ok := opts[0].(*Config); ok {
config = c
} else {
opts = append([]Option{config}, opts...)
}
}
var skipAfterInitialize bool
for _, opt := range opts {
if opt != nil {

View File

@ -474,7 +474,6 @@ func (m Migrator) MigrateColumn(value interface{}, field *schema.Field, columnTy
// found, smart migrate
fullDataType := strings.TrimSpace(strings.ToLower(m.DB.Migrator().FullDataTypeOf(field).SQL))
realDataType := strings.ToLower(columnType.DatabaseTypeName())
var (
alterColumn bool
isSameType = fullDataType == realDataType
@ -513,8 +512,19 @@ func (m Migrator) MigrateColumn(value interface{}, field *schema.Field, columnTy
}
}
}
}
// check precision
// check precision
if realDataType == "decimal" || realDataType == "numeric" &&
regexp.MustCompile(realDataType+`\(.*\)`).FindString(fullDataType) != "" { // if realDataType has no precision,ignore
precision, scale, ok := columnType.DecimalSize()
if ok {
if !strings.HasPrefix(fullDataType, fmt.Sprintf("%s(%d,%d)", realDataType, precision, scale)) &&
!strings.HasPrefix(fullDataType, fmt.Sprintf("%s(%d)", realDataType, precision)) {
alterColumn = true
}
}
} else {
if precision, _, ok := columnType.DecimalSize(); ok && int64(field.Precision) != precision {
if regexp.MustCompile(fmt.Sprintf("[^0-9]%d[^0-9]", field.Precision)).MatchString(m.DataTypeOf(field)) {
alterColumn = true

View File

@ -448,21 +448,30 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
}
// create valuer, setter when parse struct
func (field *Field) setupValuerAndSetter() {
func (field *Field) setupValuerAndSetter(modelType reflect.Type) {
// Setup NewValuePool
field.setupNewValuePool()
// ValueOf returns field's value and if it is zero
fieldIndex := field.StructField.Index[0]
switch {
case len(field.StructField.Index) == 1 && fieldIndex > 0:
field.ValueOf = func(ctx context.Context, value reflect.Value) (interface{}, bool) {
fieldValue := reflect.Indirect(value).Field(fieldIndex)
case len(field.StructField.Index) == 1 && fieldIndex >= 0:
field.ValueOf = func(ctx context.Context, v reflect.Value) (interface{}, bool) {
v = reflect.Indirect(v)
if v.Type() != modelType {
fieldValue := v.FieldByName(field.Name)
return fieldValue.Interface(), fieldValue.IsZero()
}
fieldValue := v.Field(fieldIndex)
return fieldValue.Interface(), fieldValue.IsZero()
}
default:
field.ValueOf = func(ctx context.Context, v reflect.Value) (interface{}, bool) {
v = reflect.Indirect(v)
if v.Type() != modelType {
fieldValue := v.FieldByName(field.Name)
return fieldValue.Interface(), fieldValue.IsZero()
}
for _, fieldIdx := range field.StructField.Index {
if fieldIdx >= 0 {
v = v.Field(fieldIdx)
@ -504,13 +513,20 @@ func (field *Field) setupValuerAndSetter() {
// ReflectValueOf returns field's reflect value
switch {
case len(field.StructField.Index) == 1 && fieldIndex > 0:
field.ReflectValueOf = func(ctx context.Context, value reflect.Value) reflect.Value {
return reflect.Indirect(value).Field(fieldIndex)
case len(field.StructField.Index) == 1 && fieldIndex >= 0:
field.ReflectValueOf = func(ctx context.Context, v reflect.Value) reflect.Value {
v = reflect.Indirect(v)
if v.Type() != modelType {
return v.FieldByName(field.Name)
}
return v.Field(fieldIndex)
}
default:
field.ReflectValueOf = func(ctx context.Context, v reflect.Value) reflect.Value {
v = reflect.Indirect(v)
if v.Type() != modelType {
return v.FieldByName(field.Name)
}
for idx, fieldIdx := range field.StructField.Index {
if fieldIdx >= 0 {
v = v.Field(fieldIdx)

View File

@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"go/ast"
"path"
"reflect"
"strings"
"sync"
@ -247,7 +248,7 @@ func ParseWithSpecialTableName(dest interface{}, cacheStore *sync.Map, namer Nam
schema.FieldsByBindName[bindName] = field
}
field.setupValuerAndSetter()
field.setupValuerAndSetter(modelType)
}
prioritizedPrimaryField := schema.LookUpField("id")
@ -313,8 +314,14 @@ func ParseWithSpecialTableName(dest interface{}, cacheStore *sync.Map, namer Nam
for _, cbName := range callbackTypes {
if methodValue := callBackToMethodValue(modelValue, cbName); methodValue.IsValid() {
switch methodValue.Type().String() {
case "func(*gorm.DB) error": // TODO hack
reflect.Indirect(reflect.ValueOf(schema)).FieldByName(string(cbName)).SetBool(true)
case "func(*gorm.DB) error":
expectedPkgPath := path.Dir(reflect.TypeOf(schema).Elem().PkgPath())
if inVarPkg := methodValue.Type().In(0).Elem().PkgPath(); inVarPkg == expectedPkgPath {
reflect.Indirect(reflect.ValueOf(schema)).FieldByName(string(cbName)).SetBool(true)
} else {
logger.Default.Warn(context.Background(), "In model %v, the hook function `%v(*gorm.DB) error` has an incorrect parameter type. The expected parameter type is `%v`, but the provided type is `%v`.", schema, cbName, expectedPkgPath, inVarPkg)
// PASS
}
default:
logger.Default.Warn(context.Background(), "Model %v don't match %vInterface, should be `%v(*gorm.DB) error`. Please see https://gorm.io/docs/hooks.html", schema, cbName, cbName)
}

View File

@ -341,7 +341,9 @@ func (stmt *Statement) BuildCondition(query interface{}, args ...interface{}) []
if where, ok := cs.Expression.(clause.Where); ok {
if len(where.Exprs) == 1 {
if orConds, ok := where.Exprs[0].(clause.OrConditions); ok {
where.Exprs[0] = clause.AndConditions(orConds)
if len(orConds.Exprs) == 1 {
where.Exprs[0] = clause.AndConditions(orConds)
}
}
}
conds = append(conds, clause.And(where.Exprs...))
@ -362,6 +364,9 @@ func (stmt *Statement) BuildCondition(query interface{}, args ...interface{}) []
for _, key := range keys {
column := clause.Column{Name: key, Table: curTable}
if strings.Contains(key, ".") {
column = clause.Column{Name: key}
}
conds = append(conds, clause.Eq{Column: column, Value: v[key]})
}
case map[string]interface{}:
@ -374,6 +379,9 @@ func (stmt *Statement) BuildCondition(query interface{}, args ...interface{}) []
for _, key := range keys {
reflectValue := reflect.Indirect(reflect.ValueOf(v[key]))
column := clause.Column{Name: key, Table: curTable}
if strings.Contains(key, ".") {
column = clause.Column{Name: key}
}
switch reflectValue.Kind() {
case reflect.Slice, reflect.Array:
if _, ok := v[key].(driver.Valuer); ok {

View File

@ -0,0 +1,88 @@
package tests_test
import (
"fmt"
"strings"
"testing"
"gorm.io/gorm"
)
type Man struct {
ID int
Age int
Name string
Detail string
}
// Panic-safe BeforeUpdate hook that checks for Changed("age")
func (m *Man) BeforeUpdate(tx *gorm.DB) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("panic in BeforeUpdate: %v", r)
}
}()
if !tx.Statement.Changed("age") {
return nil
}
return nil
}
func (m *Man) update(data interface{}) error {
return DB.Set("data", data).Model(m).Where("id = ?", m.ID).Updates(data).Error
}
func TestBeforeUpdateStatementChanged(t *testing.T) {
DB.AutoMigrate(&Man{})
type TestCase struct {
BaseObjects Man
change interface{}
expectError bool
}
testCases := []TestCase{
{
BaseObjects: Man{ID: 1, Age: 18, Name: "random-name"},
change: struct {
Age int
}{Age: 20},
expectError: false,
},
{
BaseObjects: Man{ID: 2, Age: 18, Name: "random-name"},
change: struct {
Name string
}{Name: "name-only"},
expectError: true,
},
{
BaseObjects: Man{ID: 2, Age: 18, Name: "random-name"},
change: struct {
Name string
Age int
}{Name: "name-only", Age: 20},
expectError: false,
},
}
for _, test := range testCases {
DB.Create(&test.BaseObjects)
// below comment is stored for future reference
// err := DB.Set("data", test.change).Model(&test.BaseObjects).Where("id = ?", test.BaseObjects.ID).Updates(test.change).Error
err := test.BaseObjects.update(test.change)
if strings.Contains(fmt.Sprint(err), "panic in BeforeUpdate") {
if !test.expectError {
t.Errorf("unexpected panic in BeforeUpdate for input: %+v\nerror: %v", test.change, err)
}
} else {
if test.expectError {
t.Errorf("expected panic did not occur for input: %+v", test.change)
}
if err != nil {
t.Errorf("unexpected GORM error: %v", err)
}
}
}
}

View File

@ -1,6 +1,6 @@
services:
mysql:
image: 'mysql/mysql-server:latest'
image: 'mysql:latest'
ports:
- "127.0.0.1:9910:3306"
environment:

View File

@ -14,31 +14,48 @@ import (
)
func TestCreate(t *testing.T) {
user := *GetUser("create", Config{})
u1 := *GetUser("create", Config{})
if results := DB.Create(&user); results.Error != nil {
if results := DB.Create(&u1); results.Error != nil {
t.Fatalf("errors happened when create: %v", results.Error)
} else if results.RowsAffected != 1 {
t.Fatalf("rows affected expects: %v, got %v", 1, results.RowsAffected)
}
if user.ID == 0 {
t.Errorf("user's primary key should has value after create, got : %v", user.ID)
if u1.ID == 0 {
t.Errorf("user's primary key should has value after create, got : %v", u1.ID)
}
if user.CreatedAt.IsZero() {
if u1.CreatedAt.IsZero() {
t.Errorf("user's created at should be not zero")
}
if user.UpdatedAt.IsZero() {
if u1.UpdatedAt.IsZero() {
t.Errorf("user's updated at should be not zero")
}
var newUser User
if err := DB.Where("id = ?", user.ID).First(&newUser).Error; err != nil {
if err := DB.Where("id = ?", u1.ID).First(&newUser).Error; err != nil {
t.Fatalf("errors happened when query: %v", err)
} else {
CheckUser(t, newUser, user)
CheckUser(t, newUser, u1)
}
type user struct {
ID int `gorm:"primaryKey;->:false"`
Name string
Age int
}
var u2 user
if results := DB.Create(&u2); results.Error != nil {
t.Fatalf("errors happened when create: %v", results.Error)
} else if results.RowsAffected != 1 {
t.Fatalf("rows affected expects: %v, got %v", 1, results.RowsAffected)
}
if u2.ID != 0 {
t.Errorf("don't have the permission to read primary key from db, but got %v", u2.ID)
}
}

View File

@ -206,9 +206,9 @@ func TestDeleteSliceWithAssociations(t *testing.T) {
}
}
// only sqlite, postgres, sqlserver support returning
// only sqlite, postgres, gaussdb, sqlserver support returning
func TestSoftDeleteReturning(t *testing.T) {
if DB.Dialector.Name() != "sqlite" && DB.Dialector.Name() != "postgres" && DB.Dialector.Name() != "sqlserver" {
if DB.Dialector.Name() != "sqlite" && DB.Dialector.Name() != "postgres" && DB.Dialector.Name() != "gaussdb" && DB.Dialector.Name() != "sqlserver" {
return
}
@ -233,7 +233,7 @@ func TestSoftDeleteReturning(t *testing.T) {
}
func TestDeleteReturning(t *testing.T) {
if DB.Dialector.Name() != "sqlite" && DB.Dialector.Name() != "postgres" && DB.Dialector.Name() != "sqlserver" {
if DB.Dialector.Name() != "sqlite" && DB.Dialector.Name() != "postgres" && DB.Dialector.Name() != "gaussdb" && DB.Dialector.Name() != "sqlserver" {
return
}

View File

@ -39,7 +39,7 @@ func TestSupportedDialectorWithErrDuplicatedKey(t *testing.T) {
t.Fatalf("failed to connect database, got error %v", err)
}
dialectors := map[string]bool{"sqlite": true, "postgres": true, "mysql": true, "sqlserver": true}
dialectors := map[string]bool{"sqlite": true, "postgres": true, "gaussdb": true, "mysql": true, "sqlserver": true}
if supported, found := dialectors[db.Dialector.Name()]; !(found && supported) {
return
}
@ -81,7 +81,7 @@ func TestSupportedDialectorWithErrForeignKeyViolated(t *testing.T) {
t.Fatalf("failed to connect database, got error %v", err)
}
dialectors := map[string]bool{"sqlite": true, "postgres": true, "mysql": true, "sqlserver": true}
dialectors := map[string]bool{"sqlite": true, "postgres": true, "gaussdb": true, "mysql": true, "sqlserver": true}
if supported, found := dialectors[db.Dialector.Name()]; !(found && supported) {
return
}

248
tests/gaussdb_test.go Normal file
View File

@ -0,0 +1,248 @@
package tests_test
import (
"testing"
"time"
"github.com/google/uuid"
"github.com/lib/pq"
"gorm.io/gorm"
"gorm.io/gorm/clause"
. "gorm.io/gorm/utils/tests"
)
func TestGaussDBReturningIDWhichHasStringType(t *testing.T) {
t.Skipf("This test case skipped, because of gaussdb not support pgcrypto extension and gen_random_uuid() function")
if DB.Dialector.Name() != "gaussdb" {
t.Skip()
}
type Yasuo struct {
// TODO: function gen_random_uuid() does not exist
ID string `gorm:"default:gen_random_uuid()"`
Name string
CreatedAt time.Time `gorm:"type:TIMESTAMP WITHOUT TIME ZONE"`
UpdatedAt time.Time `gorm:"type:TIMESTAMP WITHOUT TIME ZONE;default:current_timestamp"`
}
if err := DB.Exec("CREATE EXTENSION IF NOT EXISTS pgcrypto;").Error; err != nil {
t.Errorf("Failed to create extension pgcrypto, got error %v", err)
}
DB.Migrator().DropTable(&Yasuo{})
if err := DB.AutoMigrate(&Yasuo{}); err != nil {
t.Fatalf("Failed to migrate for uuid default value, got error: %v", err)
}
yasuo := Yasuo{Name: "jinzhu"}
if err := DB.Create(&yasuo).Error; err != nil {
t.Fatalf("should be able to create data, but got %v", err)
}
if yasuo.ID == "" {
t.Fatal("should be able to has ID, but got zero value")
}
var result Yasuo
if err := DB.First(&result, "id = ?", yasuo.ID).Error; err != nil || yasuo.Name != "jinzhu" {
t.Errorf("No error should happen, but got %v", err)
}
if err := DB.Where("id = $1", yasuo.ID).First(&Yasuo{}).Error; err != nil || yasuo.Name != "jinzhu" {
t.Errorf("No error should happen, but got %v", err)
}
yasuo.Name = "jinzhu1"
if err := DB.Save(&yasuo).Error; err != nil {
t.Errorf("Failed to update date, got error %v", err)
}
if err := DB.First(&result, "id = ?", yasuo.ID).Error; err != nil || yasuo.Name != "jinzhu1" {
t.Errorf("No error should happen, but got %v", err)
}
}
func TestGaussDB(t *testing.T) {
t.Skipf("This test case skipped, because of gaussdb not support pgcrypto extension and gen_random_uuid() function")
if DB.Dialector.Name() != "gaussdb" {
t.Skip()
}
type Harumph struct {
gorm.Model
Name string `gorm:"check:name_checker,name <> ''"`
// TODO: function gen_random_uuid() does not exist
Test uuid.UUID `gorm:"type:uuid;not null;default:gen_random_uuid()"`
CreatedAt time.Time `gorm:"type:TIMESTAMP WITHOUT TIME ZONE"`
UpdatedAt time.Time `gorm:"type:TIMESTAMP WITHOUT TIME ZONE;default:current_timestamp"`
Things pq.StringArray `gorm:"type:text[]"`
}
if err := DB.Exec("CREATE EXTENSION IF NOT EXISTS pgcrypto;").Error; err != nil {
t.Errorf("Failed to create extension pgcrypto, got error %v", err)
}
DB.Migrator().DropTable(&Harumph{})
if err := DB.AutoMigrate(&Harumph{}); err != nil {
t.Fatalf("Failed to migrate for uuid default value, got error: %v", err)
}
harumph := Harumph{}
if err := DB.Create(&harumph).Error; err == nil {
t.Fatalf("should failed to create data, name can't be blank")
}
harumph = Harumph{Name: "jinzhu"}
if err := DB.Create(&harumph).Error; err != nil {
t.Fatalf("should be able to create data, but got %v", err)
}
var result Harumph
if err := DB.First(&result, "id = ?", harumph.ID).Error; err != nil || harumph.Name != "jinzhu" {
t.Errorf("No error should happen, but got %v", err)
}
if err := DB.Where("id = $1", harumph.ID).First(&Harumph{}).Error; err != nil || harumph.Name != "jinzhu" {
t.Errorf("No error should happen, but got %v", err)
}
harumph.Name = "jinzhu1"
if err := DB.Save(&harumph).Error; err != nil {
t.Errorf("Failed to update date, got error %v", err)
}
if err := DB.First(&result, "id = ?", harumph.ID).Error; err != nil || harumph.Name != "jinzhu1" {
t.Errorf("No error should happen, but got %v", err)
}
DB.Migrator().DropTable("log_usage")
if err := DB.Exec(`
CREATE TABLE public.log_usage (
log_id bigint NOT NULL
);
ALTER TABLE public.log_usage ALTER COLUMN log_id ADD GENERATED BY DEFAULT AS IDENTITY (
SEQUENCE NAME public.log_usage_log_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1
);
`).Error; err != nil {
t.Fatalf("failed to create table, got error %v", err)
}
columns, err := DB.Migrator().ColumnTypes("log_usage")
if err != nil {
t.Fatalf("failed to get columns, got error %v", err)
}
hasLogID := false
for _, column := range columns {
if column.Name() == "log_id" {
hasLogID = true
autoIncrement, ok := column.AutoIncrement()
if !ok || !autoIncrement {
t.Fatalf("column log_id should be auto incrementment")
}
}
}
if !hasLogID {
t.Fatalf("failed to found column log_id")
}
}
func TestGaussDBMany2ManyWithDefaultValueUUID(t *testing.T) {
t.Skipf("This test case skipped, because of gaussdb does not have 'uuid-ossp' extension")
if DB.Dialector.Name() != "gaussdb" {
t.Skip()
}
if err := DB.Exec(`create extension if not exists "uuid-ossp"`).Error; err != nil {
t.Fatalf("Failed to create 'uuid-ossp' extension, but got error %v", err)
}
DB.Migrator().DropTable(&Post{}, &Category{}, "post_categories")
DB.AutoMigrate(&Post{}, &Category{})
post := Post{
Title: "Hello World",
Categories: []*Category{
{Title: "Coding"},
{Title: "Golang"},
},
}
if err := DB.Create(&post).Error; err != nil {
t.Errorf("Failed, got error: %v", err)
}
}
func TestGaussDBOnConstraint(t *testing.T) {
t.Skipf("This test case skipped, because of gaussdb not support 'ON CONSTRAINT' statement")
if DB.Dialector.Name() != "gaussdb" {
t.Skip()
}
type Thing struct {
gorm.Model
SomeID string
OtherID string
Data string
}
DB.Migrator().DropTable(&Thing{})
DB.Migrator().CreateTable(&Thing{})
if err := DB.Exec("ALTER TABLE things ADD CONSTRAINT some_id_other_id_unique UNIQUE (some_id, other_id)").Error; err != nil {
t.Error(err)
}
thing := Thing{
SomeID: "1234",
OtherID: "1234",
Data: "something",
}
DB.Create(&thing)
thing2 := Thing{
SomeID: "1234",
OtherID: "1234",
Data: "something else",
}
result := DB.Clauses(clause.OnConflict{
OnConstraint: "some_id_other_id_unique",
UpdateAll: true,
}).Create(&thing2)
if result.Error != nil {
t.Errorf("creating second thing: %v", result.Error)
}
var things []Thing
if err := DB.Find(&things).Error; err != nil {
t.Errorf("Failed, got error: %v", err)
}
if len(things) > 1 {
t.Errorf("expected 1 thing got more")
}
}
func TestGaussDBAlterColumnDataType(t *testing.T) {
if DB.Dialector.Name() != "gaussdb" {
t.Skip()
}
DB.Migrator().DropTable(&Company{})
DB.AutoMigrate(Company{})
if err := DB.Table("companies").Migrator().AlterColumn(CompanyNew{}, "name"); err != nil {
t.Fatalf("failed to alter column from string to int, got error %v", err)
}
DB.AutoMigrate(Company{})
}

View File

@ -12,6 +12,7 @@ import (
"sync"
"testing"
"github.com/google/uuid"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/clause"
@ -850,3 +851,25 @@ func TestGenericsToSQL(t *testing.T) {
t.Errorf("ToSQL: got wrong sql with Generics API %v", sql)
}
}
func TestGenericsScanUUID(t *testing.T) {
ctx := context.Background()
users := []User{
{Name: uuid.NewString(), Age: 21},
{Name: uuid.NewString(), Age: 22},
{Name: uuid.NewString(), Age: 23},
}
if err := gorm.G[User](DB).CreateInBatches(ctx, &users, 2); err != nil {
t.Fatalf("CreateInBatches failed: %v", err)
}
userIds := []uuid.UUID{}
if err := gorm.G[User](DB).Select("name").Where("id in ?", []uint{users[0].ID, users[1].ID, users[2].ID}).Order("age").Scan(ctx, &userIds); err != nil || len(users) != 3 {
t.Fatalf("Scan failed: %v, userids %v", err, userIds)
}
if userIds[0].String() != users[0].Name || userIds[1].String() != users[1].Name || userIds[2].String() != users[2].Name {
t.Fatalf("wrong uuid scanned")
}
}

View File

@ -7,35 +7,34 @@ require (
github.com/jinzhu/now v1.1.5
github.com/lib/pq v1.10.9
github.com/stretchr/testify v1.10.0
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.5.4
gorm.io/gorm v1.26.1
gorm.io/driver/gaussdb v0.1.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/gorm v1.30.0
)
require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/HuaweiCloudDeveloper/gaussdb-go v1.0.0-rc1 // indirect
github.com/davecgh/go-spew v1.1.1 // 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/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/pgx/v5 v5.7.5 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/kr/text v0.2.0 // 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.9.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
golang.org/x/crypto v0.38.0 // indirect
golang.org/x/text v0.25.0 // indirect
github.com/tjfoc/gmsm v1.4.1 // indirect
golang.org/x/crypto v0.40.0 // indirect
golang.org/x/sync v0.16.0 // indirect
golang.org/x/text v0.27.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
replace gorm.io/gorm => ../
replace github.com/jackc/pgx/v5 => github.com/jackc/pgx/v5 v5.4.3
replace github.com/microsoft/go-mssqldb => github.com/microsoft/go-mssqldb v1.7.0

View File

@ -5,7 +5,6 @@ import (
"database/sql"
"fmt"
"math/rand"
"os"
"reflect"
"strconv"
"strings"
@ -13,11 +12,11 @@ import (
"time"
"github.com/stretchr/testify/assert"
"gorm.io/driver/gaussdb"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/logger"
"gorm.io/gorm/migrator"
"gorm.io/gorm/schema"
"gorm.io/gorm/utils"
@ -84,8 +83,8 @@ func TestMigrate(t *testing.T) {
}
}
func TestAutoMigrateInt8PG(t *testing.T) {
if DB.Dialector.Name() != "postgres" {
func TestAutoMigrateInt8PGAndGaussDB(t *testing.T) {
if DB.Dialector.Name() != "postgres" && DB.Dialector.Name() != "gaussdb" {
return
}
@ -184,7 +183,94 @@ func TestAutoMigrateNullable(t *testing.T) {
}
func TestSmartMigrateColumn(t *testing.T) {
fullSupported := map[string]bool{"mysql": true, "postgres": true}[DB.Dialector.Name()]
fullSupported := map[string]bool{"mysql": true, "postgres": true, "gaussdb": true}[DB.Dialector.Name()]
type UserMigrateColumn struct {
ID uint
Name string
Salary float64
Birthday time.Time `gorm:"precision:4"`
}
DB.Migrator().DropTable(&UserMigrateColumn{})
DB.AutoMigrate(&UserMigrateColumn{})
type UserMigrateColumn2 struct {
ID uint
Name string `gorm:"size:128"`
Salary float64 `gorm:"precision:2"`
Birthday time.Time `gorm:"precision:2"`
NameIgnoreMigration string `gorm:"size:100"`
}
if err := DB.Table("user_migrate_columns").AutoMigrate(&UserMigrateColumn2{}); err != nil {
t.Fatalf("failed to auto migrate, got error: %v", err)
}
columnTypes, err := DB.Table("user_migrate_columns").Migrator().ColumnTypes(&UserMigrateColumn{})
if err != nil {
t.Fatalf("failed to get column types, got error: %v", err)
}
for _, columnType := range columnTypes {
switch columnType.Name() {
case "name":
if length, _ := columnType.Length(); (fullSupported || length != 0) && length != 128 {
t.Fatalf("name's length should be 128, but got %v", length)
}
case "salary":
if precision, o, _ := columnType.DecimalSize(); (fullSupported || precision != 0) && precision != 2 {
t.Fatalf("salary's precision should be 2, but got %v %v", precision, o)
}
case "birthday":
if precision, _, _ := columnType.DecimalSize(); (fullSupported || precision != 0) && precision != 2 {
t.Fatalf("birthday's precision should be 2, but got %v", precision)
}
}
}
type UserMigrateColumn3 struct {
ID uint
Name string `gorm:"size:256"`
Salary float64 `gorm:"precision:3"`
Birthday time.Time `gorm:"precision:3"`
NameIgnoreMigration string `gorm:"size:128;-:migration"`
}
if err := DB.Table("user_migrate_columns").AutoMigrate(&UserMigrateColumn3{}); err != nil {
t.Fatalf("failed to auto migrate, got error: %v", err)
}
columnTypes, err = DB.Table("user_migrate_columns").Migrator().ColumnTypes(&UserMigrateColumn{})
if err != nil {
t.Fatalf("failed to get column types, got error: %v", err)
}
for _, columnType := range columnTypes {
switch columnType.Name() {
case "name":
if length, _ := columnType.Length(); (fullSupported || length != 0) && length != 256 {
t.Fatalf("name's length should be 128, but got %v", length)
}
case "salary":
if precision, _, _ := columnType.DecimalSize(); (fullSupported || precision != 0) && precision != 3 {
t.Fatalf("salary's precision should be 2, but got %v", precision)
}
case "birthday":
if precision, _, _ := columnType.DecimalSize(); (fullSupported || precision != 0) && precision != 3 {
t.Fatalf("birthday's precision should be 2, but got %v", precision)
}
case "name_ignore_migration":
if length, _ := columnType.Length(); (fullSupported || length != 0) && length != 100 {
t.Fatalf("name_ignore_migration's length should still be 100 but got %v", length)
}
}
}
}
func TestSmartMigrateColumnGaussDB(t *testing.T) {
fullSupported := map[string]bool{"mysql": true, "gaussdb": true}[DB.Dialector.Name()]
type UserMigrateColumn struct {
ID uint
@ -852,7 +938,7 @@ func TestMigrateColumnOrder(t *testing.T) {
// https://github.com/go-gorm/gorm/issues/5047
func TestMigrateSerialColumn(t *testing.T) {
if DB.Dialector.Name() != "postgres" {
if DB.Dialector.Name() != "postgres" && DB.Dialector.Name() != "gaussdb" {
return
}
@ -1011,6 +1097,42 @@ func TestPrimarykeyID(t *testing.T) {
}
}
func TestPrimarykeyIDGaussDB(t *testing.T) {
t.Skipf("This test case skipped, because of gaussdb not support uuid-ossp plugin (SQLSTATE 58P01)")
if DB.Dialector.Name() != "gaussdb" {
return
}
type MissPKLanguage struct {
ID string `gorm:"type:uuid;default:uuid_generate_v4()"`
Name string
}
type MissPKUser struct {
ID string `gorm:"type:uuid;default:uuid_generate_v4()"`
MissPKLanguages []MissPKLanguage `gorm:"many2many:miss_pk_user_languages;"`
}
var err error
err = DB.Migrator().DropTable(&MissPKUser{}, &MissPKLanguage{})
if err != nil {
t.Fatalf("DropTable err:%v", err)
}
// TODO: ERROR: could not open extension control file: No such file or directory (SQLSTATE 58P01)
DB.Exec(`CREATE EXTENSION IF NOT EXISTS "uuid-ossp";`)
err = DB.AutoMigrate(&MissPKUser{}, &MissPKLanguage{})
if err != nil {
t.Fatalf("AutoMigrate err:%v", err)
}
// patch
err = DB.AutoMigrate(&MissPKUser{}, &MissPKLanguage{})
if err != nil {
t.Fatalf("AutoMigrate err:%v", err)
}
}
func TestCurrentTimestamp(t *testing.T) {
if DB.Dialector.Name() != "mysql" {
return
@ -1211,35 +1333,24 @@ func TestInvalidCachedPlanSimpleProtocol(t *testing.T) {
}
}
func TestInvalidCachedPlanPrepareStmt(t *testing.T) {
if DB.Dialector.Name() != "postgres" {
// TODO: ERROR: must have at least one column (SQLSTATE 0A000)
func TestInvalidCachedPlanSimpleProtocolGaussDB(t *testing.T) {
t.Skipf("This test case skipped, because of gaussdb not support creaing empty table(SQLSTATE 0A000)")
if DB.Dialector.Name() != "gaussdb" {
return
}
db, err := gorm.Open(postgres.Open(postgresDSN), &gorm.Config{PrepareStmt: true})
db, err := gorm.Open(gaussdb.Open(gaussdbDSN), &gorm.Config{})
if err != nil {
t.Errorf("Open err:%v", err)
}
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)
}
type Object1 struct {
ID uint
}
type Object1 struct{}
type Object2 struct {
ID uint
Field1 int `gorm:"type:int8"`
Field1 string
}
type Object3 struct {
ID uint
Field1 int `gorm:"type:int4"`
}
type Object4 struct {
ID uint
Field2 int
Field2 string
}
db.Migrator().DropTable("objects")
@ -1247,63 +1358,16 @@ func TestInvalidCachedPlanPrepareStmt(t *testing.T) {
if err != nil {
t.Errorf("AutoMigrate err:%v", err)
}
err = db.Table("objects").Create(&Object1{}).Error
if err != nil {
t.Errorf("create err:%v", err)
}
// AddColumn
err = db.Table("objects").AutoMigrate(&Object2{})
if err != nil {
t.Errorf("AutoMigrate err:%v", err)
}
err = db.Table("objects").Take(&Object2{}).Error
if err != nil {
t.Errorf("take err:%v", err)
}
// AlterColumn
err = db.Table("objects").AutoMigrate(&Object3{})
if err != nil {
t.Errorf("AutoMigrate err:%v", err)
}
err = db.Table("objects").Take(&Object3{}).Error
if err != nil {
t.Errorf("take err:%v", err)
}
// AddColumn
err = db.Table("objects").AutoMigrate(&Object4{})
if err != nil {
t.Errorf("AutoMigrate err:%v", err)
}
err = db.Table("objects").Take(&Object4{}).Error
if err != nil {
t.Errorf("take err:%v", err)
}
db.Table("objects").Migrator().RenameColumn(&Object4{}, "field2", "field3")
if err != nil {
t.Errorf("RenameColumn err:%v", err)
}
err = db.Table("objects").Take(&Object4{}).Error
if err != nil {
t.Errorf("take err:%v", err)
}
db.Table("objects").Migrator().DropColumn(&Object4{}, "field3")
if err != nil {
t.Errorf("RenameColumn err:%v", err)
}
err = db.Table("objects").Take(&Object4{}).Error
if err != nil {
t.Errorf("take err:%v", err)
}
}
func TestDifferentTypeWithoutDeclaredLength(t *testing.T) {
@ -1346,7 +1410,7 @@ func TestDifferentTypeWithoutDeclaredLength(t *testing.T) {
}
func TestMigrateArrayTypeModel(t *testing.T) {
if DB.Dialector.Name() != "postgres" {
if DB.Dialector.Name() != "postgres" && DB.Dialector.Name() != "gaussdb" {
return
}
@ -1668,8 +1732,8 @@ func TestMigrateView(t *testing.T) {
}
}
func TestMigrateExistingBoolColumnPG(t *testing.T) {
if DB.Dialector.Name() != "postgres" {
func TestMigrateExistingBoolColumnPGAndGaussDB(t *testing.T) {
if DB.Dialector.Name() != "postgres" && DB.Dialector.Name() != "gaussdb" {
return
}
@ -1987,3 +2051,114 @@ func TestMigrateWithUniqueIndexAndUnique(t *testing.T) {
}
}
}
func testAutoMigrateDecimal(t *testing.T, model1, model2 any) []string {
tracer := Tracer{
Logger: DB.Config.Logger,
Test: func(ctx context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error) {
sql, _ := fc()
if strings.HasPrefix(sql, "ALTER TABLE ") {
t.Fatalf("shouldn't execute ALTER COLUMN TYPE if decimal is not change: sql: %s", sql)
}
},
}
session := DB.Session(&gorm.Session{Logger: tracer})
DB.Migrator().DropTable(model1)
var modifySql []string
if err := session.AutoMigrate(model1); err != nil {
t.Fatalf("failed to auto migrate, got error: %v", err)
}
if err := session.AutoMigrate(model1); err != nil {
t.Fatalf("failed to auto migrate, got error: %v", err)
}
tracer2 := Tracer{
Logger: DB.Config.Logger,
Test: func(ctx context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error) {
sql, _ := fc()
modifySql = append(modifySql, sql)
},
}
session2 := DB.Session(&gorm.Session{Logger: tracer2})
err := session2.Table("migrate_decimal_columns").Migrator().AutoMigrate(model2)
if err != nil {
t.Fatalf("failed to get column types, got error: %v", err)
}
return modifySql
}
func decimalColumnsTest[T, T2 any](t *testing.T, expectedSql []string) {
var t1 T
var t2 T2
modSql := testAutoMigrateDecimal(t, t1, t2)
var alterSQL []string
for _, sql := range modSql {
if strings.HasPrefix(sql, "ALTER TABLE ") {
alterSQL = append(alterSQL, sql)
}
}
if len(alterSQL) != 3 {
t.Fatalf("decimal changed error,expected: %+v,got: %+v.", expectedSql, alterSQL)
}
for i := range alterSQL {
if alterSQL[i] != expectedSql[i] {
t.Fatalf("decimal changed error,expected: %+v,got: %+v.", expectedSql, alterSQL)
}
}
}
func TestAutoMigrateDecimal(t *testing.T) {
if DB.Dialector.Name() == "sqlserver" { // database/sql will replace numeric to decimal. so only support decimal.
type MigrateDecimalColumn struct {
RecID1 int64 `gorm:"column:recid1;type:decimal(9,0);not null" json:"recid1"`
RecID2 int64 `gorm:"column:recid2;type:decimal(8);not null" json:"recid2"`
RecID3 int64 `gorm:"column:recid3;type:decimal(8,1);not null" json:"recid3"`
}
type MigrateDecimalColumn2 struct {
RecID1 int64 `gorm:"column:recid1;type:decimal(8);not null" json:"recid1"`
RecID2 int64 `gorm:"column:recid2;type:decimal(9,1);not null" json:"recid2"`
RecID3 int64 `gorm:"column:recid3;type:decimal(9,2);not null" json:"recid3"`
}
expectedSql := []string{
`ALTER TABLE "migrate_decimal_columns" ALTER COLUMN "recid1" decimal(8) NOT NULL`,
`ALTER TABLE "migrate_decimal_columns" ALTER COLUMN "recid2" decimal(9,1) NOT NULL`,
`ALTER TABLE "migrate_decimal_columns" ALTER COLUMN "recid3" decimal(9,2) NOT NULL`,
}
decimalColumnsTest[MigrateDecimalColumn, MigrateDecimalColumn2](t, expectedSql)
} else if DB.Dialector.Name() == "postgres" || DB.Dialector.Name() == "gaussdb" {
type MigrateDecimalColumn struct {
RecID1 int64 `gorm:"column:recid1;type:numeric(9,0);not null" json:"recid1"`
RecID2 int64 `gorm:"column:recid2;type:numeric(8);not null" json:"recid2"`
RecID3 int64 `gorm:"column:recid3;type:numeric(8,1);not null" json:"recid3"`
}
type MigrateDecimalColumn2 struct {
RecID1 int64 `gorm:"column:recid1;type:numeric(8);not null" json:"recid1"`
RecID2 int64 `gorm:"column:recid2;type:numeric(9,1);not null" json:"recid2"`
RecID3 int64 `gorm:"column:recid3;type:numeric(9,2);not null" json:"recid3"`
}
expectedSql := []string{
`ALTER TABLE "migrate_decimal_columns" ALTER COLUMN "recid1" TYPE numeric(8) USING "recid1"::numeric(8)`,
`ALTER TABLE "migrate_decimal_columns" ALTER COLUMN "recid2" TYPE numeric(9,1) USING "recid2"::numeric(9,1)`,
`ALTER TABLE "migrate_decimal_columns" ALTER COLUMN "recid3" TYPE numeric(9,2) USING "recid3"::numeric(9,2)`,
}
decimalColumnsTest[MigrateDecimalColumn, MigrateDecimalColumn2](t, expectedSql)
} else if DB.Dialector.Name() == "mysql" {
type MigrateDecimalColumn struct {
RecID1 int64 `gorm:"column:recid1;type:decimal(9,0);not null" json:"recid1"`
RecID2 int64 `gorm:"column:recid2;type:decimal(8);not null" json:"recid2"`
RecID3 int64 `gorm:"column:recid3;type:decimal(8,1);not null" json:"recid3"`
}
type MigrateDecimalColumn2 struct {
RecID1 int64 `gorm:"column:recid1;type:decimal(8);not null" json:"recid1"`
RecID2 int64 `gorm:"column:recid2;type:decimal(9,1);not null" json:"recid2"`
RecID3 int64 `gorm:"column:recid3;type:decimal(9,2);not null" json:"recid3"`
}
expectedSql := []string{
"ALTER TABLE `migrate_decimal_columns` MODIFY COLUMN `recid1` decimal(8) NOT NULL",
"ALTER TABLE `migrate_decimal_columns` MODIFY COLUMN `recid2` decimal(9,1) NOT NULL",
"ALTER TABLE `migrate_decimal_columns` MODIFY COLUMN `recid3` decimal(9,2) NOT NULL",
}
decimalColumnsTest[MigrateDecimalColumn, MigrateDecimalColumn2](t, expectedSql)
}
}

View File

@ -41,7 +41,7 @@ func TestManyToManyWithMultiPrimaryKeys(t *testing.T) {
t.Skip("skip sqlite, sqlserver due to it doesn't support multiple primary keys with auto increment")
}
if name := DB.Dialector.Name(); name == "postgres" {
if name := DB.Dialector.Name(); name == "postgres" || name == "mysql" || name == "gaussdb" {
stmt := gorm.Statement{DB: DB}
stmt.Parse(&Blog{})
stmt.Schema.LookUpField("ID").Unique = true
@ -142,6 +142,9 @@ func TestManyToManyWithCustomizedForeignKeys(t *testing.T) {
if name := DB.Dialector.Name(); name == "postgres" {
t.Skip("skip postgres due to it only allow unique constraint matching given keys")
}
if name := DB.Dialector.Name(); name == "gaussdb" {
t.Skip("skip gaussdb due to it only allow unique constraint matching given keys")
}
DB.Migrator().DropTable(&Blog{}, &Tag{}, "blog_tags", "locale_blog_tags", "shared_blog_tags")
if err := DB.AutoMigrate(&Blog{}, &Tag{}); err != nil {
@ -264,10 +267,14 @@ func TestManyToManyWithCustomizedForeignKeys2(t *testing.T) {
t.Skip("skip sqlite, sqlserver due to it doesn't support multiple primary keys with auto increment")
}
if name := DB.Dialector.Name(); name == "postgres" {
if name := DB.Dialector.Name(); name == "postgres" || name == "mysql" {
t.Skip("skip postgres due to it only allow unique constraint matching given keys")
}
if name := DB.Dialector.Name(); name == "gaussdb" {
t.Skip("skip gaussdb due to it only allow unique constraint matching given keys")
}
DB.Migrator().DropTable(&Blog{}, &Tag{}, "blog_tags", "locale_blog_tags", "shared_blog_tags")
if err := DB.AutoMigrate(&Blog{}, &Tag{}); err != nil {
t.Fatalf("Failed to auto migrate, got error: %v", err)
@ -332,7 +339,7 @@ func TestManyToManyWithCustomizedForeignKeys2(t *testing.T) {
DB.Model(&blog2).Association("LocaleTags").Find(&tags)
if !compareTags(tags, []string{"tag4"}) {
t.Fatalf("Should find 1 tags for EN Blog")
t.Fatalf("Should find 1 tags for EN Blog, but got %v", tags)
}
// Replace

View File

@ -696,6 +696,10 @@ func TestManyToManyPreloadWithMultiPrimaryKeys(t *testing.T) {
t.Skip("skip sqlite, sqlserver due to it doesn't support multiple primary keys with auto increment")
}
if name := DB.Dialector.Name(); name == "mysql" {
t.Skip("skip mysql due to it only allow unique constraint matching given keys")
}
type (
Level1 struct {
ID uint `gorm:"primary_key;"`

View File

@ -632,6 +632,21 @@ func TestOr(t *testing.T) {
t.Fatalf("Build OR condition, but got %v", result.Statement.SQL.String())
}
sub := dryDB.Clauses(clause.Where{
Exprs: []clause.Expression{
clause.OrConditions{
Exprs: []clause.Expression{
clause.Expr{SQL: "role = ?", Vars: []interface{}{"super_admin"}},
clause.Expr{SQL: "role = ?", Vars: []interface{}{"admin"}},
},
},
},
})
result = dryDB.Where(sub).Find(&User{})
if !regexp.MustCompile("SELECT \\* FROM .*users.* WHERE .*role.* = .+ OR .*role.* = .+").MatchString(result.Statement.SQL.String()) {
t.Fatalf("Build OR condition, but got %v", result.Statement.SQL.String())
}
result = dryDB.Where("role = ?", "admin").Or("role = ?", "super_admin").Find(&User{})
if !regexp.MustCompile("SELECT \\* FROM .*users.* WHERE .*role.* = .+ OR .*role.* = .+").MatchString(result.Statement.SQL.String()) {
t.Fatalf("Build OR condition, but got %v", result.Statement.SQL.String())
@ -1112,6 +1127,10 @@ func TestSearchWithMap(t *testing.T) {
DB.First(&user, map[string]interface{}{"name": users[0].Name})
CheckUser(t, user, users[0])
user = User{}
DB.First(&user, map[string]interface{}{"users.name": users[0].Name})
CheckUser(t, user, users[0])
user = User{}
DB.Where(map[string]interface{}{"name": users[1].Name}).First(&user)
CheckUser(t, user, users[1])

View File

@ -45,7 +45,7 @@ type SerializerPostgresStruct struct {
func (*SerializerPostgresStruct) TableName() string { return "serializer_structs" }
func adaptorSerializerModel(s *SerializerStruct) interface{} {
if DB.Dialector.Name() == "postgres" {
if DB.Dialector.Name() == "postgres" || DB.Dialector.Name() == "gaussdb" {
sps := SerializerPostgresStruct(*s)
return &sps
}

View File

@ -487,7 +487,7 @@ func replaceQuoteInSQL(sql string) string {
// convert dialect special quote into double quote
switch DB.Dialector.Name() {
case "postgres":
case "postgres", "gaussdb":
sql = strings.ReplaceAll(sql, `"`, `"`)
case "mysql", "sqlite":
sql = strings.ReplaceAll(sql, "`", `"`)

View File

@ -5,6 +5,7 @@ import (
"sync"
"testing"
"gorm.io/driver/gaussdb"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/schema"
@ -251,6 +252,82 @@ func TestPostgresTableWithIdentifierLength(t *testing.T) {
})
}
func TestGaussDBTableWithIdentifierLength(t *testing.T) {
if DB.Dialector.Name() != "gaussdb" {
return
}
type LongString struct {
ThisIsAVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString string `gorm:"unique"`
}
t.Run("default", func(t *testing.T) {
db, _ := gorm.Open(gaussdb.Open(gaussdbDSN), &gorm.Config{})
user, err := schema.Parse(&LongString{}, &sync.Map{}, db.Config.NamingStrategy)
if err != nil {
t.Fatalf("failed to parse user unique, got error %v", err)
}
constraints := user.ParseUniqueConstraints()
if len(constraints) != 1 {
t.Fatalf("failed to find unique constraint, got %v", constraints)
}
for key := range constraints {
if len(key) != 63 {
t.Errorf("failed to find unique constraint, got %v", constraints)
}
}
})
t.Run("naming strategy", func(t *testing.T) {
db, _ := gorm.Open(gaussdb.Open(gaussdbDSN), &gorm.Config{
NamingStrategy: schema.NamingStrategy{},
})
user, err := schema.Parse(&LongString{}, &sync.Map{}, db.Config.NamingStrategy)
if err != nil {
t.Fatalf("failed to parse user unique, got error %v", err)
}
constraints := user.ParseUniqueConstraints()
if len(constraints) != 1 {
t.Fatalf("failed to find unique constraint, got %v", constraints)
}
for key := range constraints {
if len(key) != 63 {
t.Errorf("failed to find unique constraint, got %v", constraints)
}
}
})
t.Run("namer", func(t *testing.T) {
uname := "custom_unique_name"
db, _ := gorm.Open(gaussdb.Open(gaussdbDSN), &gorm.Config{
NamingStrategy: mockUniqueNamingStrategy{
UName: uname,
},
})
user, err := schema.Parse(&LongString{}, &sync.Map{}, db.Config.NamingStrategy)
if err != nil {
t.Fatalf("failed to parse user unique, got error %v", err)
}
constraints := user.ParseUniqueConstraints()
if len(constraints) != 1 {
t.Fatalf("failed to find unique constraint, got %v", constraints)
}
for key := range constraints {
if key != uname {
t.Errorf("failed to find unique constraint, got %v", constraints)
}
}
})
}
type mockUniqueNamingStrategy struct {
UName string
schema.NamingStrategy

View File

@ -1,6 +1,6 @@
#!/bin/bash -e
dialects=("sqlite" "mysql" "postgres" "sqlserver" "tidb")
dialects=("sqlite" "mysql" "postgres" "gaussdb" "sqlserver" "tidb")
if [[ $(pwd) == *"gorm/tests"* ]]; then
cd ..

View File

@ -8,6 +8,7 @@ import (
"path/filepath"
"time"
"gorm.io/driver/gaussdb"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
@ -21,6 +22,7 @@ var DB *gorm.DB
var (
mysqlDSN = "gorm:gorm@tcp(localhost:9910)/gorm?charset=utf8&parseTime=True&loc=Local"
postgresDSN = "user=gorm password=gorm dbname=gorm host=localhost port=9920 sslmode=disable TimeZone=Asia/Shanghai"
gaussdbDSN = "user=gaussdb password=Gaussdb@123 dbname=gorm host=localhost port=9950 sslmode=disable TimeZone=Asia/Shanghai"
sqlserverDSN = "sqlserver://sa:LoremIpsum86@localhost:9930?database=master"
tidbDSN = "root:@tcp(localhost:9940)/test?charset=utf8&parseTime=True&loc=Local"
)
@ -65,6 +67,15 @@ func OpenTestConnection(cfg *gorm.Config) (db *gorm.DB, err error) {
DSN: dbDSN,
PreferSimpleProtocol: true,
}), cfg)
case "gaussdb":
log.Println("testing gaussdb...")
if dbDSN == "" {
dbDSN = gaussdbDSN
}
db, err = gorm.Open(gaussdb.New(gaussdb.Config{
DSN: dbDSN,
PreferSimpleProtocol: true,
}), cfg)
case "sqlserver":
// go install github.com/microsoft/go-sqlcmd/cmd/sqlcmd@latest
// SQLCMDPASSWORD=LoremIpsum86 sqlcmd -U sa -S localhost:9930

View File

@ -765,9 +765,9 @@ func TestSaveWithPrimaryValue(t *testing.T) {
}
}
// only sqlite, postgres, sqlserver support returning
// only sqlite, postgres, gaussdb, sqlserver support returning
func TestUpdateReturning(t *testing.T) {
if DB.Dialector.Name() != "sqlite" && DB.Dialector.Name() != "postgres" && DB.Dialector.Name() != "sqlserver" {
if DB.Dialector.Name() != "sqlite" && DB.Dialector.Name() != "postgres" && DB.Dialector.Name() != "gaussdb" && DB.Dialector.Name() != "sqlserver" {
return
}
@ -883,9 +883,9 @@ func TestSaveWithHooks(t *testing.T) {
}
}
// only postgres, sqlserver, sqlite support update from
// only postgres, gaussdb, sqlserver, sqlite support update from
func TestUpdateFrom(t *testing.T) {
if DB.Dialector.Name() != "postgres" && DB.Dialector.Name() != "sqlite" && DB.Dialector.Name() != "sqlserver" {
if DB.Dialector.Name() != "postgres" && DB.Dialector.Name() != "gaussdb" && DB.Dialector.Name() != "sqlite" && DB.Dialector.Name() != "sqlserver" {
return
}