Skip to content

Commit

Permalink
Merge pull request go-gorm#4773 from xwjdsh/master
Browse files Browse the repository at this point in the history
fix: automigrate error caused by indexes while using dynamic table name
  • Loading branch information
huacnlee authored Oct 19, 2021
2 parents 9a5ba37 + a3bd9c3 commit 38e55f1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
24 changes: 19 additions & 5 deletions schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ type Tabler interface {

// Parse get data type from dialector
func Parse(dest interface{}, cacheStore *sync.Map, namer Namer) (*Schema, error) {
return parse(dest, cacheStore, namer, "")
}

// ParseWithSchemaTable get data type from dialector with extra schema table
func ParseWithSchemaTable(dest interface{}, cacheStore *sync.Map, namer Namer, schemaTable string) (*Schema, error) {
return parse(dest, cacheStore, namer, schemaTable)
}

func parse(dest interface{}, cacheStore *sync.Map, namer Namer, schemaTable string) (*Schema, error) {
if dest == nil {
return nil, fmt.Errorf("%w: %+v", ErrUnsupportedDataType, dest)
}
Expand Down Expand Up @@ -107,6 +116,9 @@ func Parse(dest interface{}, cacheStore *sync.Map, namer Namer) (*Schema, error)

modelValue := reflect.New(modelType)
tableName := namer.TableName(modelType.Name())
if schemaTable != "" {
tableName = schemaTable
}
if tabler, ok := modelValue.Interface().(Tabler); ok {
tableName = tabler.TableName()
}
Expand Down Expand Up @@ -235,11 +247,13 @@ func Parse(dest interface{}, cacheStore *sync.Map, namer Namer) (*Schema, error)
}
}

if v, loaded := cacheStore.LoadOrStore(modelType, schema); loaded {
s := v.(*Schema)
// Wait for the initialization of other goroutines to complete
<-s.initialized
return s, s.err
if schemaTable == "" {
if v, loaded := cacheStore.LoadOrStore(modelType, schema); loaded {
s := v.(*Schema)
// Wait for the initialization of other goroutines to complete
<-s.initialized
return s, s.err
}
}

defer func() {
Expand Down
2 changes: 1 addition & 1 deletion statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ func (stmt *Statement) Build(clauses ...string) {
}

func (stmt *Statement) Parse(value interface{}) (err error) {
if stmt.Schema, err = schema.Parse(value, stmt.DB.cacheStore, stmt.DB.NamingStrategy); err == nil && stmt.Table == "" {
if stmt.Schema, err = schema.ParseWithSchemaTable(value, stmt.DB.cacheStore, stmt.DB.NamingStrategy, stmt.DB.Statement.Table); err == nil && stmt.Table == "" {
if tables := strings.Split(stmt.Schema.Table, "."); len(tables) == 2 {
stmt.TableExpr = &clause.Expr{SQL: stmt.Quote(stmt.Schema.Table)}
stmt.Table = tables[1]
Expand Down
30 changes: 30 additions & 0 deletions tests/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,3 +381,33 @@ func TestMigrateConstraint(t *testing.T) {
}
}
}

type MigrateUser struct {
gorm.Model
Name string `gorm:"index"`
}

// https://github.com/go-gorm/gorm/issues/4752
func TestMigrateIndexesWithDynamicTableName(t *testing.T) {
tableNameSuffixes := []string{"01", "02", "03"}
for _, v := range tableNameSuffixes {
tableName := "migrate_user_" + v
m := DB.Scopes(func(db *gorm.DB) *gorm.DB {
return db.Table(tableName)
}).Migrator()

if err := m.AutoMigrate(&MigrateUser{}); err != nil {
t.Fatalf("Failed to create table for %#v", tableName)
}

if !m.HasTable(tableName) {
t.Fatalf("Failed to create table for %#v", tableName)
}
if !m.HasIndex(&MigrateUser{}, "Name") {
t.Fatalf("Should find index for %s's name after AutoMigrate", tableName)
}
if !m.HasIndex(&MigrateUser{}, "DeletedAt") {
t.Fatalf("Should find index for %s's deleted_at after AutoMigrate", tableName)
}
}
}

0 comments on commit 38e55f1

Please sign in to comment.