From 222427c474a3146bf79cb782fe50fae7d80aae69 Mon Sep 17 00:00:00 2001 From: "Jonathan A. Sternberg" Date: Tue, 8 Sep 2020 18:12:14 -0500 Subject: [PATCH] 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. --- migrator/migrator.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/migrator/migrator.go b/migrator/migrator.go index 98e92c96..c0e22ae0 100644 --- a/migrator/migrator.go +++ b/migrator/migrator.go @@ -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) { 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 { columnTypes, err = rows.ColumnTypes() + _ = rows.Close() } return err })