Nested folder: Fix for PostgreSQL (#59405)

* Nested Folders: Fix PostgreSQL LastInsertId failure

* Fix children sorting

* Fix store tests
This commit is contained in:
Sofia Papagiannaki
2022-11-28 17:48:44 +02:00
committed by GitHub
parent b3b7cba0fe
commit 8ab7ca45cd
3 changed files with 52 additions and 25 deletions
+24
View File
@@ -3,12 +3,14 @@ package sqlstore
import (
"context"
"errors"
"fmt"
"reflect"
"time"
"xorm.io/xorm"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/services/sqlstore/migrator"
"github.com/grafana/grafana/pkg/util/errutil"
"github.com/grafana/grafana/pkg/util/retryer"
"github.com/mattn/go-sqlite3"
@@ -131,6 +133,28 @@ func (sess *DBSession) InsertId(bean interface{}) (int64, error) {
return id, nil
}
func (sess *DBSession) WithReturningID(driverName string, query string, args []interface{}) (int64, error) {
supported := driverName != migrator.Postgres
var id int64
if !supported {
query = fmt.Sprintf("%s RETURNING id", query)
if _, err := sess.SQL(query, args...).Get(&id); err != nil {
return id, err
}
} else {
sqlOrArgs := append([]interface{}{query}, args...)
res, err := sess.Exec(sqlOrArgs...)
if err != nil {
return id, err
}
id, err = res.LastInsertId()
if err != nil {
return id, err
}
}
return id, nil
}
func getTypeName(bean interface{}) (res string) {
t := reflect.TypeOf(bean)
for t.Kind() == reflect.Ptr {