diff --git a/pkg/services/ngalert/CHANGELOG.md b/pkg/services/ngalert/CHANGELOG.md index 8ebb7c857fc..99a4536957b 100644 --- a/pkg/services/ngalert/CHANGELOG.md +++ b/pkg/services/ngalert/CHANGELOG.md @@ -54,6 +54,7 @@ Scopes must have an order to ensure consistency and ease of search, this helps u - [FEATURE] Indicate whether routes are provisioned when GETting Alertmanager configuration #47857 - [FEATURE] Indicate whether contact point is provisioned when GETting Alertmanager configuration #48323 - [FEATURE] Indicate whether alert rule is provisioned when GETting the rule #48458 +- [BUGFIX] Migration: ignore alerts that do not belong to any existing organization\dashboard #49192 ## 8.5.3 diff --git a/pkg/services/sqlstore/migrations/ualert/dash_alert.go b/pkg/services/sqlstore/migrations/ualert/dash_alert.go index 36158a1840a..f527744b31c 100644 --- a/pkg/services/sqlstore/migrations/ualert/dash_alert.go +++ b/pkg/services/sqlstore/migrations/ualert/dash_alert.go @@ -36,10 +36,12 @@ SELECT id, settings FROM alert +WHERE org_id IN (SELECT id from org) + AND dashboard_id IN (SELECT id from dashboard) ` // slurpDashAlerts loads all alerts from the alert database table into the -// the dashAlert type. +// the dashAlert type. If there are alerts that belong to either organization or dashboard that does not exist, those alerts will not be returned/ // Additionally it unmarshals the json settings for the alert into the // ParsedSettings property of the dash alert. func (m *migration) slurpDashAlerts() ([]dashAlert, error) { diff --git a/pkg/services/sqlstore/migrations/ualert/migration_test.go b/pkg/services/sqlstore/migrations/ualert/migration_test.go index 0c0a7d13bbb..2b441c510b2 100644 --- a/pkg/services/sqlstore/migrations/ualert/migration_test.go +++ b/pkg/services/sqlstore/migrations/ualert/migration_test.go @@ -568,9 +568,22 @@ func createDatasource(t *testing.T, id int64, orgId int64, uid string) *models.D } } +func createOrg(t *testing.T, id int64) *models.Org { + t.Helper() + return &models.Org{ + Id: id, + Version: 1, + Name: fmt.Sprintf("org_%d", id), + Created: time.Now(), + Updated: time.Now(), + } +} + // teardown cleans the input tables between test cases. func teardown(t *testing.T, x *xorm.Engine) { - _, err := x.Exec("DELETE from alert") + _, err := x.Exec("DELETE from org") + require.NoError(t, err) + _, err = x.Exec("DELETE from alert") require.NoError(t, err) _, err = x.Exec("DELETE from alert_notification") require.NoError(t, err) @@ -584,6 +597,11 @@ func teardown(t *testing.T, x *xorm.Engine) { func setupLegacyAlertsTables(t *testing.T, x *xorm.Engine, legacyChannels []*models.AlertNotification, alerts []*models.Alert) { t.Helper() + orgs := []models.Org{ + *createOrg(t, 1), + *createOrg(t, 2), + } + // Setup dashboards. dashboards := []models.Dashboard{ *createDashboard(t, 1, 1, "dash1-1"), @@ -601,6 +619,10 @@ func setupLegacyAlertsTables(t *testing.T, x *xorm.Engine, legacyChannels []*mod *createDatasource(t, 3, 2, "ds3-2"), *createDatasource(t, 4, 2, "ds4-2"), } + + _, errOrgs := x.Insert(orgs) + require.NoError(t, errOrgs) + _, errDataSourcess := x.Insert(dataSources) require.NoError(t, errDataSourcess)