mirror of
https://github.com/grafana/grafana.git
synced 2025-08-02 17:52:24 +08:00
Folders: Switch order of the columns in folder table indexes so that org_id becomes first (#82454)
* Folders: Switch order of the columns in folder table so that org_id becomes first
This commit is contained in:

committed by
GitHub

parent
c6f8462a06
commit
38e8c62972
@ -88,6 +88,7 @@ func (ss *sqlStore) Delete(ctx context.Context, UIDs []string, orgID int64) erro
|
||||
return nil
|
||||
}
|
||||
return ss.db.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
// covered by UQE_folder_org_id_uid
|
||||
s := fmt.Sprintf("DELETE FROM folder WHERE org_id=? AND uid IN (%s)", strings.Repeat("?, ", len(UIDs)-1)+"?")
|
||||
sqlArgs := make([]any, 0, len(UIDs)+2)
|
||||
sqlArgs = append(sqlArgs, s, orgID)
|
||||
@ -140,6 +141,7 @@ func (ss *sqlStore) Update(ctx context.Context, cmd folder.UpdateFolderCommand)
|
||||
}
|
||||
|
||||
sql.WriteString(strings.Join(columnsToUpdate, ", "))
|
||||
// covered by UQE_folder_org_id_uid
|
||||
sql.WriteString(" WHERE uid = ? AND org_id = ?")
|
||||
args = append(args, cmd.UID, cmd.OrgID)
|
||||
|
||||
@ -205,14 +207,17 @@ func (ss *sqlStore) Get(ctx context.Context, q folder.GetFolderQuery) (*folder.F
|
||||
}
|
||||
switch {
|
||||
case q.UID != nil:
|
||||
// covered UQE_folder_uid_org_id
|
||||
s.WriteString(" WHERE f0.uid = ? AND f0.org_id = ?")
|
||||
exists, err = sess.SQL(s.String(), q.UID, q.OrgID).Get(foldr)
|
||||
// nolint:staticcheck
|
||||
case q.ID != nil:
|
||||
s.WriteString(" WHERE f0.id = ?")
|
||||
metrics.MFolderIDsServiceCount.WithLabelValues(metrics.Folder).Inc()
|
||||
// covered by primary key
|
||||
exists, err = sess.SQL(s.String(), q.ID).Get(foldr)
|
||||
case q.Title != nil:
|
||||
// covered by UQE_folder_org_id_parent_uid_title
|
||||
s.WriteString(" WHERE f0.title = ? AND f0.org_id = ?")
|
||||
args := []any{*q.Title, q.OrgID}
|
||||
if q.ParentUID != nil {
|
||||
@ -245,6 +250,7 @@ func (ss *sqlStore) GetParents(ctx context.Context, q folder.GetParentsQuery) ([
|
||||
}
|
||||
var folders []*folder.Folder
|
||||
|
||||
// covered by UQE_folder_org_id_uid
|
||||
recQuery := `
|
||||
WITH RECURSIVE RecQry AS (
|
||||
SELECT * FROM folder WHERE uid = ? AND org_id = ?
|
||||
@ -295,6 +301,7 @@ func (ss *sqlStore) GetChildren(ctx context.Context, q folder.GetChildrenQuery)
|
||||
err := ss.db.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
sql := strings.Builder{}
|
||||
args := make([]any, 0, 2)
|
||||
// covered by UQE_folder_org_id_parent_uid_title
|
||||
if q.UID == "" {
|
||||
sql.WriteString("SELECT * FROM folder WHERE parent_uid IS NULL AND org_id=?")
|
||||
args = append(args, q.OrgID)
|
||||
@ -342,6 +349,7 @@ func (ss *sqlStore) GetChildren(ctx context.Context, q folder.GetChildrenQuery)
|
||||
func (ss *sqlStore) getParentsMySQL(ctx context.Context, q folder.GetParentsQuery) (folders []*folder.Folder, err error) {
|
||||
err = ss.db.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
uid := ""
|
||||
// covered by UQE_folder_org_id_uid
|
||||
ok, err := sess.SQL("SELECT parent_uid FROM folder WHERE org_id=? AND uid=?", q.OrgID, q.UID).Get(&uid)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -351,6 +359,7 @@ func (ss *sqlStore) getParentsMySQL(ctx context.Context, q folder.GetParentsQuer
|
||||
}
|
||||
for {
|
||||
f := &folder.Folder{}
|
||||
// covered by UQE_folder_org_id_uid
|
||||
ok, err := sess.SQL("SELECT * FROM folder WHERE org_id=? AND uid=?", q.OrgID, uid).Get(f)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -454,6 +463,7 @@ func (ss *sqlStore) GetFolders(ctx context.Context, q getFoldersQuery) ([]*folde
|
||||
if q.WithFullpath || q.WithFullpathUIDs || len(q.ancestorUIDs) > 0 {
|
||||
s.WriteString(getFullpathJoinsSQL())
|
||||
}
|
||||
// covered by UQE_folder_org_id_uid
|
||||
s.WriteString(` WHERE f0.org_id=?`)
|
||||
args := []any{q.OrgID}
|
||||
if len(partialUIDs) > 0 {
|
||||
@ -509,6 +519,7 @@ func (ss *sqlStore) GetDescendants(ctx context.Context, orgID int64, ancestor_ui
|
||||
}
|
||||
switch recursiveQueriesAreSupported {
|
||||
case true:
|
||||
// covered by UQE_folder_org_id_parent_uid_title
|
||||
recQuery := `
|
||||
WITH RECURSIVE RecQry AS (
|
||||
SELECT * FROM folder WHERE parent_uid = ? AND org_id = ?
|
||||
@ -532,6 +543,7 @@ func (ss *sqlStore) GetDescendants(ctx context.Context, orgID int64, ancestor_ui
|
||||
s := strings.Builder{}
|
||||
args := make([]any, 0, 1+folder.MaxNestedFolderDepth)
|
||||
args = append(args, orgID)
|
||||
// covered by UQE_folder_org_id_uid
|
||||
s.WriteString(`SELECT f0.id, f0.org_id, f0.uid, f0.parent_uid, f0.title, f0.description, f0.created, f0.updated`)
|
||||
s.WriteString(` FROM folder f0`)
|
||||
s.WriteString(getFullpathJoinsSQL())
|
||||
@ -585,6 +597,7 @@ func getFullapathUIDsSQL(dialect migrator.Dialect) string {
|
||||
func getFullpathJoinsSQL() string {
|
||||
joins := make([]string, 0, folder.MaxNestedFolderDepth)
|
||||
for i := 1; i <= folder.MaxNestedFolderDepth; i++ {
|
||||
// covered by UQE_folder_org_id_uid
|
||||
joins = append(joins, fmt.Sprintf(` LEFT JOIN folder f%d ON f%d.org_id = f%d.org_id AND f%d.uid = f%d.parent_uid`, i, i, i-1, i, i-1))
|
||||
}
|
||||
return strings.Join(joins, "\n")
|
||||
|
Reference in New Issue
Block a user