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:
Jonathan A. Sternberg 2020-09-08 18:12:14 -05:00
parent c70c097e88
commit 222427c474
No known key found for this signature in database
GPG Key ID: 5526FD110785B45C
1 changed files with 2 additions and 1 deletions

View File

@ -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
}) })