Alerting: Fix GetAlertRulesForScheduling to use folder table and join by org_id (#80330)

This commit is contained in:
Yuri Tseretyan 2024-01-11 09:21:03 -05:00 committed by GitHub
parent e1aa8a95d9
commit 77db6a9ca4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 3 deletions

View File

@ -566,9 +566,8 @@ func (st DBstore) GetAlertRulesForScheduling(ctx context.Context, query *ngmodel
query.ResultRules = rules
if query.PopulateFolders {
foldersSql := sess.Table("dashboard").Alias("d").Select("d.uid, d.title").
Where("is_folder = ?", st.SQLStore.GetDialect().BooleanStr(true)).
And(`EXISTS (SELECT 1 FROM alert_rule a WHERE d.uid = a.namespace_uid)`)
foldersSql := sess.Table("folder").Alias("d").Select("d.uid, d.title").
Where(`EXISTS (SELECT 1 FROM alert_rule a WHERE d.uid = a.namespace_uid AND d.org_id = a.org_id)`)
if len(disabledOrgs) > 0 {
foldersSql.NotIn("org_id", disabledOrgs)
}

View File

@ -342,6 +342,8 @@ func TestIntegration_GetAlertRulesForScheduling(t *testing.T) {
createFolder(t, store, rule1.NamespaceUID, rule1.Title, rule1.OrgID)
createFolder(t, store, rule2.NamespaceUID, rule2.Title, rule2.OrgID)
createFolder(t, store, rule2.NamespaceUID, "same UID folder", generator().OrgID) // create a folder with the same UID but in the different org
tc := []struct {
name string
rules []string