Release the connection when discovering the column types in the migrator
When the migrator is used to discover the column types, such as when used with `AutoMigrate()`, it does not close the query result. This changes the migrator to close the query result and it also changes the query to use `LIMIT 1` to prevent additional work against the database when only discovering the schema. Fixes #3432.
This commit is contained in:
		
							parent
							
								
									c70c097e88
								
							
						
					
					
						commit
						222427c474
					
				| @ -388,9 +388,10 @@ func (m Migrator) MigrateColumn(value interface{}, field *schema.Field, columnTy | |||||||
| 
 | 
 | ||||||
| func (m Migrator) ColumnTypes(value interface{}) (columnTypes []*sql.ColumnType, err error) { | func (m Migrator) ColumnTypes(value interface{}) (columnTypes []*sql.ColumnType, err error) { | ||||||
| 	err = m.RunWithValue(value, func(stmt *gorm.Statement) error { | 	err = m.RunWithValue(value, func(stmt *gorm.Statement) error { | ||||||
| 		rows, err := m.DB.Session(&gorm.Session{}).Raw("select * from ?", clause.Table{Name: stmt.Table}).Rows() | 		rows, err := m.DB.Session(&gorm.Session{}).Raw("select * from ? limit 1", clause.Table{Name: stmt.Table}).Rows() | ||||||
| 		if err == nil { | 		if err == nil { | ||||||
| 			columnTypes, err = rows.ColumnTypes() | 			columnTypes, err = rows.ColumnTypes() | ||||||
|  | 			_ = rows.Close() | ||||||
| 		} | 		} | ||||||
| 		return err | 		return err | ||||||
| 	}) | 	}) | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Jonathan A. Sternberg
						Jonathan A. Sternberg