Refactor ParseWithSchemaTable method and improve test.
This commit is contained in:
		
							parent
							
								
									38e55f1117
								
							
						
					
					
						commit
						09483e8928
					
				@ -115,15 +115,18 @@ func parse(dest interface{}, cacheStore *sync.Map, namer Namer, schemaTable stri
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	modelValue := reflect.New(modelType)
 | 
						modelValue := reflect.New(modelType)
 | 
				
			||||||
	tableName := namer.TableName(modelType.Name())
 | 
					
 | 
				
			||||||
	if schemaTable != "" {
 | 
						// schemaTable for assignment table name directly
 | 
				
			||||||
		tableName = schemaTable
 | 
						tableName := schemaTable
 | 
				
			||||||
	}
 | 
						if schemaTable == "" {
 | 
				
			||||||
	if tabler, ok := modelValue.Interface().(Tabler); ok {
 | 
							tableName = namer.TableName(modelType.Name())
 | 
				
			||||||
		tableName = tabler.TableName()
 | 
					
 | 
				
			||||||
	}
 | 
							if tabler, ok := modelValue.Interface().(Tabler); ok {
 | 
				
			||||||
	if en, ok := namer.(embeddedNamer); ok {
 | 
								tableName = tabler.TableName()
 | 
				
			||||||
		tableName = en.Table
 | 
							}
 | 
				
			||||||
 | 
							if en, ok := namer.(embeddedNamer); ok {
 | 
				
			||||||
 | 
								tableName = en.Table
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	schema := &Schema{
 | 
						schema := &Schema{
 | 
				
			||||||
 | 
				
			|||||||
@ -382,32 +382,41 @@ func TestMigrateConstraint(t *testing.T) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type MigrateUser struct {
 | 
					type DynamicUser struct {
 | 
				
			||||||
	gorm.Model
 | 
						gorm.Model
 | 
				
			||||||
	Name string `gorm:"index"`
 | 
						Name      string
 | 
				
			||||||
 | 
						CompanyID string `gorm:"index"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// To test auto migrate crate indexes for dynamic table name
 | 
				
			||||||
// https://github.com/go-gorm/gorm/issues/4752
 | 
					// https://github.com/go-gorm/gorm/issues/4752
 | 
				
			||||||
func TestMigrateIndexesWithDynamicTableName(t *testing.T) {
 | 
					func TestMigrateIndexesWithDynamicTableName(t *testing.T) {
 | 
				
			||||||
	tableNameSuffixes := []string{"01", "02", "03"}
 | 
						// Create primary table
 | 
				
			||||||
	for _, v := range tableNameSuffixes {
 | 
						if err := DB.AutoMigrate(&DynamicUser{}); err != nil {
 | 
				
			||||||
		tableName := "migrate_user_" + v
 | 
							t.Fatalf("AutoMigrate create table error: %#v", err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Create sub tables
 | 
				
			||||||
 | 
						for _, v := range []string{"01", "02", "03"} {
 | 
				
			||||||
 | 
							tableName := "users_" + v
 | 
				
			||||||
		m := DB.Scopes(func(db *gorm.DB) *gorm.DB {
 | 
							m := DB.Scopes(func(db *gorm.DB) *gorm.DB {
 | 
				
			||||||
			return db.Table(tableName)
 | 
								return db.Table(tableName)
 | 
				
			||||||
		}).Migrator()
 | 
							}).Migrator()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if err := m.AutoMigrate(&MigrateUser{}); err != nil {
 | 
							if err := m.AutoMigrate(&DynamicUser{}); err != nil {
 | 
				
			||||||
			t.Fatalf("Failed to create table for %#v", tableName)
 | 
								t.Fatalf("AutoMigrate create table error: %#v", err)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if !m.HasTable(tableName) {
 | 
							if !m.HasTable(tableName) {
 | 
				
			||||||
			t.Fatalf("Failed to create table for %#v", tableName)
 | 
								t.Fatalf("AutoMigrate expected %#v exist, but not.", tableName)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		if !m.HasIndex(&MigrateUser{}, "Name") {
 | 
					
 | 
				
			||||||
			t.Fatalf("Should find index for %s's name after AutoMigrate", tableName)
 | 
							if !m.HasIndex(&DynamicUser{}, "CompanyID") {
 | 
				
			||||||
 | 
								t.Fatalf("Should have index on %s", "CompanyI.")
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		if !m.HasIndex(&MigrateUser{}, "DeletedAt") {
 | 
					
 | 
				
			||||||
			t.Fatalf("Should find index for %s's deleted_at after AutoMigrate", tableName)
 | 
							if !m.HasIndex(&DynamicUser{}, "DeletedAt") {
 | 
				
			||||||
 | 
								t.Fatalf("Should have index on deleted_at.")
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user