Refactor ParseWithSchemaTable method and improve test.
This commit is contained in:
		
							parent
							
								
									38e55f1117
								
							
						
					
					
						commit
						09483e8928
					
				@ -115,16 +115,19 @@ func parse(dest interface{}, cacheStore *sync.Map, namer Namer, schemaTable stri
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	modelValue := reflect.New(modelType)
 | 
			
		||||
	tableName := namer.TableName(modelType.Name())
 | 
			
		||||
	if schemaTable != "" {
 | 
			
		||||
		tableName = schemaTable
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// schemaTable for assignment table name directly
 | 
			
		||||
	tableName := schemaTable
 | 
			
		||||
	if schemaTable == "" {
 | 
			
		||||
		tableName = namer.TableName(modelType.Name())
 | 
			
		||||
 | 
			
		||||
		if tabler, ok := modelValue.Interface().(Tabler); ok {
 | 
			
		||||
			tableName = tabler.TableName()
 | 
			
		||||
		}
 | 
			
		||||
		if en, ok := namer.(embeddedNamer); ok {
 | 
			
		||||
			tableName = en.Table
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	schema := &Schema{
 | 
			
		||||
		Name:           modelType.Name(),
 | 
			
		||||
 | 
			
		||||
@ -382,32 +382,41 @@ func TestMigrateConstraint(t *testing.T) {
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type MigrateUser struct {
 | 
			
		||||
type DynamicUser struct {
 | 
			
		||||
	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
 | 
			
		||||
func TestMigrateIndexesWithDynamicTableName(t *testing.T) {
 | 
			
		||||
	tableNameSuffixes := []string{"01", "02", "03"}
 | 
			
		||||
	for _, v := range tableNameSuffixes {
 | 
			
		||||
		tableName := "migrate_user_" + v
 | 
			
		||||
	// Create primary table
 | 
			
		||||
	if err := DB.AutoMigrate(&DynamicUser{}); err != nil {
 | 
			
		||||
		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 {
 | 
			
		||||
			return db.Table(tableName)
 | 
			
		||||
		}).Migrator()
 | 
			
		||||
 | 
			
		||||
		if err := m.AutoMigrate(&MigrateUser{}); err != nil {
 | 
			
		||||
			t.Fatalf("Failed to create table for %#v", tableName)
 | 
			
		||||
		if err := m.AutoMigrate(&DynamicUser{}); err != nil {
 | 
			
		||||
			t.Fatalf("AutoMigrate create table error: %#v", err)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		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