Alerting: fix sqlstore.GetFolderByTitle to search for folder (#42898)

* a test to reproduce the bug
pull/43062/head
Yuriy Tseretyan 4 years ago committed by GitHub
parent 28eeb7662c
commit b63595b47f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      pkg/services/sqlstore/dashboard.go
  2. 20
      pkg/services/sqlstore/dashboard_folder_test.go

@ -6,9 +6,10 @@ import (
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/grafana/grafana/pkg/services/sqlstore/permissions"
"github.com/grafana/grafana/pkg/services/sqlstore/searchstore"
"github.com/prometheus/client_golang/prometheus"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/infra/metrics"
@ -214,18 +215,13 @@ func (ss *SQLStore) GetFolderByTitle(orgID int64, title string) (*models.Dashboa
// there is a unique constraint on org_id, folder_id, title
// there are no nested folders so the parent folder id is always 0
dashboard := models.Dashboard{OrgId: orgID, FolderId: 0, Title: title}
has, err := ss.engine.Get(&dashboard)
has, err := ss.engine.Table(&models.Dashboard{}).Where("is_folder = " + dialect.BooleanStr(true)).Where("folder_id=0").Get(&dashboard)
if err != nil {
return nil, err
} else if !has {
return nil, models.ErrDashboardNotFound
}
// if there is a dashboard instead of a folder with that title
if !dashboard.IsFolder {
if !has {
return nil, models.ErrDashboardNotFound
}
dashboard.SetId(dashboard.Id)
dashboard.SetUid(dashboard.Uid)
return &dashboard, nil

@ -7,10 +7,11 @@ import (
"context"
"testing"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/search"
"github.com/stretchr/testify/require"
)
func TestDashboardFolderDataAccess(t *testing.T) {
@ -507,6 +508,23 @@ func TestDashboardFolderDataAccess(t *testing.T) {
})
})
})
t.Run("Given dashboard and folder with the same title", func(t *testing.T) {
var orgId int64 = 1
title := "Very Unique Name"
var sqlStore *SQLStore
var folder1, folder2 *models.Dashboard
sqlStore = InitTestDB(t)
folder2 = insertTestDashboard(t, sqlStore, "TEST", orgId, 0, true, "prod")
_ = insertTestDashboard(t, sqlStore, title, orgId, folder2.Id, false, "prod")
folder1 = insertTestDashboard(t, sqlStore, title, orgId, 0, true, "prod")
t.Run("GetFolderByTitle should find the folder", func(t *testing.T) {
result, err := sqlStore.GetFolderByTitle(orgId, title)
require.NoError(t, err)
require.Equal(t, folder1.Id, result.Id)
})
})
})
}

Loading…
Cancel
Save