Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

DropIndex and RenameIndex fail in postgres driver when TablePrefix is… #675

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
24 changes: 20 additions & 4 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 Down Expand Up @@ -49,13 +50,19 @@ func OpenTestConnection() (db *gorm.DB, err error) {
if dbDSN == "" {
dbDSN = "gorm:gorm@tcp(localhost:9910)/gorm?charset=utf8&parseTime=True&loc=Local"
}
db, err = gorm.Open(mysql.Open(dbDSN), &gorm.Config{})
db, err = gorm.Open(mysql.Open(dbDSN), &gorm.Config{
NamingStrategy: schema.NamingStrategy{
TablePrefix: "myschema.",
}})
case "postgres":
log.Println("testing postgres...")
if dbDSN == "" {
dbDSN = "user=gorm password=gorm host=localhost dbname=gorm port=9920 sslmode=disable TimeZone=Asia/Shanghai"
}
db, err = gorm.Open(postgres.Open(dbDSN), &gorm.Config{})
db, err = gorm.Open(postgres.Open(dbDSN), &gorm.Config{
NamingStrategy: schema.NamingStrategy{
TablePrefix: "myschema.",
}})
case "sqlserver":
// CREATE LOGIN gorm WITH PASSWORD = 'LoremIpsum86';
// CREATE DATABASE gorm;
Expand All @@ -66,10 +73,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: "myschema.",
}})
default:
log.Println("testing sqlite3...")
db, err = gorm.Open(sqlite.Open(filepath.Join(os.TempDir(), "gorm.db")), &gorm.Config{})
db, err = gorm.Open(sqlite.Open(filepath.Join(os.TempDir(), "gorm.db")), &gorm.Config{
NamingStrategy: schema.NamingStrategy{
TablePrefix: "myschema.",
},
})
}

if debug := os.Getenv("DEBUG"); debug == "true" {
Expand All @@ -87,6 +101,8 @@ func RunMigrations() {
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(allModels), func(i, j int) { allModels[i], allModels[j] = allModels[j], allModels[i] })

DB.Exec(`CREATE SCHEMA IF NOT EXISTS "myschema"`)

DB.Migrator().DropTable("user_friends", "user_speaks")

if err = DB.Migrator().DropTable(allModels...); err != nil {
Expand Down
17 changes: 10 additions & 7 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ import (
// TEST_DRIVERS: sqlite, mysql, postgres, sqlserver

func TestGORM(t *testing.T) {
user := User{Name: "jinzhu"}
// Drop index failure
// err := DB.Migrator().DropIndex(&User{}, "name_idx")
// if err != nil {
// t.Errorf("failed to drop index, got error %v\n", err)
// }

DB.Create(&user)

var result User
if err := DB.First(&result, user.ID).Error; err != nil {
t.Errorf("Failed, got error: %v", err)
}
// Rename index failure
// err := DB.Migrator().RenameIndex(&User{}, "name_idx", "name_idx2")
// if err != nil {
// t.Errorf("failed to rename index, got error %v\n", err)
// }
}
2 changes: 1 addition & 1 deletion models.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// His pet also has one Toy (has one - polymorphic)
type User struct {
gorm.Model
Name string
Name string `gorm:"index:name_idx"`
Age uint
Birthday *time.Time
Account Account
Expand Down
Loading