Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(GetRows): skip prepare stmt #137

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 21 additions & 7 deletions migrator.go
Expand Up @@ -528,14 +528,28 @@ func (m Migrator) GetRows(currentSchema interface{}, table interface{}) (*sql.Ro
name = fmt.Sprintf("%v.%v", currentSchema, table)
}

return m.DB.Session(&gorm.Session{}).Table(name).Limit(1).Scopes(func(d *gorm.DB) *gorm.DB {
dialector, _ := m.Dialector.(Dialector)
// use simple protocol
if !m.DB.PrepareStmt && (dialector.Config != nil && (dialector.Config.DriverName == "" || dialector.Config.DriverName == "pgx")) {
d.Statement.Vars = append(d.Statement.Vars, pgx.QuerySimpleProtocol(true))
var res []interface{}
sql := m.DB.ToSQL(func(tx *gorm.DB) *gorm.DB {
return tx.Table(name).Limit(1).Find(&res)
})
dialector, _ := m.Dialector.(Dialector)

// skip the prepared stmt
if m.DB.PrepareStmt {
if pdb, ok := m.DB.ConnPool.(*gorm.PreparedStmtDB); ok {
pdb.Mux.Lock()
if stmt, ok := pdb.Stmts[sql]; ok {
go stmt.Close()
delete(pdb.Stmts, sql)
}
pdb.Mux.Unlock()
}
return d
}).Rows()
} else if dialector.Config != nil && (dialector.Config.DriverName == "" || dialector.Config.DriverName == "pgx") {
// use simple protocol for pgx
return m.DB.Raw(sql, pgx.QuerySimpleProtocol(true)).Rows()
}

return m.DB.Raw(sql).Rows()
}

func (m Migrator) CurrentSchema(stmt *gorm.Statement, table string) (interface{}, interface{}) {
Expand Down