Alerting: Update migration to migrate only alerts that belong to existing org\dashboard (#49192)

* Update migration to migrate only alerts that belong to existing org\dashboard
pull/49204/head
Yuriy Tseretyan 3 years ago committed by GitHub
parent 859cc92da7
commit d87fdc1037
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      pkg/services/ngalert/CHANGELOG.md
  2. 4
      pkg/services/sqlstore/migrations/ualert/dash_alert.go
  3. 24
      pkg/services/sqlstore/migrations/ualert/migration_test.go

@ -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 routes are provisioned when GETting Alertmanager configuration #47857
- [FEATURE] Indicate whether contact point is provisioned when GETting Alertmanager configuration #48323 - [FEATURE] Indicate whether contact point is provisioned when GETting Alertmanager configuration #48323
- [FEATURE] Indicate whether alert rule is provisioned when GETting the rule #48458 - [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 ## 8.5.3

@ -36,10 +36,12 @@ SELECT id,
settings settings
FROM FROM
alert 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 // 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 // Additionally it unmarshals the json settings for the alert into the
// ParsedSettings property of the dash alert. // ParsedSettings property of the dash alert.
func (m *migration) slurpDashAlerts() ([]dashAlert, error) { func (m *migration) slurpDashAlerts() ([]dashAlert, error) {

@ -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. // teardown cleans the input tables between test cases.
func teardown(t *testing.T, x *xorm.Engine) { 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) require.NoError(t, err)
_, err = x.Exec("DELETE from alert_notification") _, err = x.Exec("DELETE from alert_notification")
require.NoError(t, err) 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) { func setupLegacyAlertsTables(t *testing.T, x *xorm.Engine, legacyChannels []*models.AlertNotification, alerts []*models.Alert) {
t.Helper() t.Helper()
orgs := []models.Org{
*createOrg(t, 1),
*createOrg(t, 2),
}
// Setup dashboards. // Setup dashboards.
dashboards := []models.Dashboard{ dashboards := []models.Dashboard{
*createDashboard(t, 1, 1, "dash1-1"), *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, 3, 2, "ds3-2"),
*createDatasource(t, 4, 2, "ds4-2"), *createDatasource(t, 4, 2, "ds4-2"),
} }
_, errOrgs := x.Insert(orgs)
require.NoError(t, errOrgs)
_, errDataSourcess := x.Insert(dataSources) _, errDataSourcess := x.Insert(dataSources)
require.NoError(t, errDataSourcess) require.NoError(t, errDataSourcess)

Loading…
Cancel
Save