mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Nested Folder: Modify store Update() (#58183)
* Nested Folder: Modify store Update() * fixup
This commit is contained in:
parent
3dfa49b376
commit
d131733f55
@ -96,23 +96,41 @@ func (ss *sqlStore) Update(ctx context.Context, cmd folder.UpdateFolderCommand)
|
||||
}
|
||||
|
||||
cmd.Folder.Updated = time.Now()
|
||||
existingUID := cmd.Folder.UID
|
||||
err := ss.db.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
description := cmd.Folder.Description
|
||||
sql := strings.Builder{}
|
||||
sql.Write([]byte("UPDATE folder SET "))
|
||||
columnsToUpdate := []string{"updated = ?"}
|
||||
args := []interface{}{cmd.Folder.Updated}
|
||||
if cmd.NewDescription != nil {
|
||||
description = *cmd.NewDescription
|
||||
columnsToUpdate = append(columnsToUpdate, "description = ?")
|
||||
cmd.Folder.Description = *cmd.NewDescription
|
||||
args = append(args, cmd.Folder.Description)
|
||||
}
|
||||
|
||||
title := cmd.Folder.Title
|
||||
if cmd.NewTitle != nil {
|
||||
title = *cmd.NewTitle
|
||||
columnsToUpdate = append(columnsToUpdate, "title = ?")
|
||||
cmd.Folder.Title = *cmd.NewTitle
|
||||
args = append(args, cmd.Folder.Title)
|
||||
}
|
||||
|
||||
uid := cmd.Folder.UID
|
||||
if cmd.NewUID != nil {
|
||||
uid = *cmd.NewUID
|
||||
columnsToUpdate = append(columnsToUpdate, "uid = ?")
|
||||
cmd.Folder.UID = *cmd.NewUID
|
||||
args = append(args, cmd.Folder.UID)
|
||||
}
|
||||
|
||||
res, err := sess.Exec("UPDATE folder SET description = ?, title = ?, uid = ?, updated = ? WHERE uid = ? AND org_id = ?", description, title, uid, cmd.Folder.Updated, cmd.Folder.UID, cmd.Folder.OrgID)
|
||||
if len(columnsToUpdate) == 0 {
|
||||
return folder.ErrBadRequest.Errorf("no columns to update")
|
||||
}
|
||||
|
||||
sql.Write([]byte(strings.Join(columnsToUpdate, ", ")))
|
||||
sql.Write([]byte(" WHERE uid = ? AND org_id = ?"))
|
||||
args = append(args, existingUID, cmd.Folder.OrgID)
|
||||
|
||||
args = append([]interface{}{sql.String()}, args...)
|
||||
|
||||
res, err := sess.Exec(args...)
|
||||
if err != nil {
|
||||
return folder.ErrDatabaseError.Errorf("failed to update folder: %w", err)
|
||||
}
|
||||
@ -124,10 +142,6 @@ func (ss *sqlStore) Update(ctx context.Context, cmd folder.UpdateFolderCommand)
|
||||
if affected == 0 {
|
||||
return folder.ErrInternal.Errorf("no folders are updated")
|
||||
}
|
||||
|
||||
cmd.Folder.Description = description
|
||||
cmd.Folder.Title = title
|
||||
cmd.Folder.UID = uid
|
||||
return nil
|
||||
})
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user