Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

reproduce error #386

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
18 changes: 7 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@ module gorm.io/playground
go 1.16

require (
github.com/denisenkom/go-mssqldb v0.10.0 // indirect
github.com/jackc/pgproto3/v2 v2.0.7 // indirect
github.com/jackc/pgx/v4 v4.11.0 // indirect
github.com/mattn/go-sqlite3 v1.14.7 // indirect
golang.org/x/crypto v0.0.0-20210505212654-3497b51f5e64 // indirect
golang.org/x/text v0.3.6 // indirect
github.com/denisenkom/go-mssqldb v0.11.0 // indirect
github.com/go-kit/kit v0.10.0 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
gorm.io/driver/mysql v1.0.6
gorm.io/driver/postgres v1.1.0
gorm.io/driver/sqlite v1.1.4
gorm.io/driver/sqlserver v1.0.7
gorm.io/gorm v1.21.9
gorm.io/driver/mysql v1.1.2
gorm.io/driver/postgres v1.1.2
gorm.io/driver/sqlite v1.1.5
gorm.io/driver/sqlserver v1.0.9
gorm.io/gorm v1.21.15
)

replace gorm.io/gorm => ./gorm
40 changes: 34 additions & 6 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,48 @@
package main

import (
"fmt"
"strings"
"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"}
type NewUser struct {
gorm.Model
Name string `gorm:"index"`
City string `gorm:"index"`
Table string `gorm:"-"`
}

DB.Create(&user)
/*
NOTE: https://gorm.io/docs/v2_release_note.html#Breaking-Changes
*/
func UserTable(u *NewUser) func(*gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
return db.Table(fmt.Sprintf("newuser_%s", strings.ToLower(u.Table)))
}
}

// TableName which is cached
// func (u *NewUser) TableName() string {
// return fmt.Sprintf("newuser_%s", u.Table)
// }

var result User
if err := DB.First(&result, user.ID).Error; err != nil {
t.Errorf("Failed, got error: %v", err)
func TestGORM(t *testing.T) {
tableName := []string{"a", "b", "c"}
for _, v := range tableName {
nu := &NewUser{
Table: v,
}
// DB.Scopes(UserTable(nu)).Migrator().AutoMigrate(&nu)
// DB.Migrator().AutoMigrate(&nu)
if err := DB.Scopes(UserTable(nu)).Migrator().AutoMigrate(&nu); err != nil {
t.Errorf("Failed, got error: %v", err)
}
}
}