From efe856ae4e9c26c6d50dd0abb6bcb7610d86a5ad Mon Sep 17 00:00:00 2001 From: Kyle Brandt Date: Fri, 30 Apr 2021 15:08:01 -0400 Subject: [PATCH] AlertingMigration: Create alert_rule_version entry (#33585) Create the alert rule version entry during the migration so it is consistent with rules created via api. for https://github.com/grafana/alerting-squad/issues/123 --- .../sqlstore/migrations/ualert/alert_rule.go | 53 ++++++++++++++++++- .../sqlstore/migrations/ualert/ualert.go | 6 +++ 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/pkg/services/sqlstore/migrations/ualert/alert_rule.go b/pkg/services/sqlstore/migrations/ualert/alert_rule.go index 2c5d86926f7..df500ce83ee 100644 --- a/pkg/services/sqlstore/migrations/ualert/alert_rule.go +++ b/pkg/services/sqlstore/migrations/ualert/alert_rule.go @@ -14,6 +14,7 @@ type alertRule struct { Condition string Data []alertQuery IntervalSeconds int64 + Version int64 Uid string NamespaceUid string RuleGroup string @@ -22,7 +23,53 @@ type alertRule struct { For duration Updated time.Time Annotations map[string]string - // Labels map[string]string (Labels are not Created in the migration) + Labels map[string]string // (Labels are not Created in the migration) +} + +type alertRuleVersion struct { + RuleOrgID int64 `xorm:"rule_org_id"` + RuleUID string `xorm:"rule_uid"` + RuleNamespaceUID string `xorm:"rule_namespace_uid"` + RuleGroup string + ParentVersion int64 + RestoredFrom int64 + Version int64 + + Created time.Time + Title string + Condition string + Data []alertQuery + IntervalSeconds int64 + NoDataState string + ExecErrState string + // ideally this field should have been apimodels.ApiDuration + // but this is currently not possible because of circular dependencies + For duration + Annotations map[string]string + Labels map[string]string +} + +func (a *alertRule) makeVersion() *alertRuleVersion { + return &alertRuleVersion{ + RuleOrgID: a.OrgId, + RuleUID: a.Uid, + RuleNamespaceUID: a.NamespaceUid, + RuleGroup: a.RuleGroup, + ParentVersion: 0, + RestoredFrom: 0, + Version: 1, + + Created: time.Now().UTC(), + Title: a.Title, + Condition: a.Condition, + Data: a.Data, + IntervalSeconds: a.IntervalSeconds, + NoDataState: a.NoDataState, + ExecErrState: a.ExecErrState, + For: a.For, + Annotations: a.Annotations, + Labels: map[string]string{}, + } } func getMigrationInfo(da dashAlert) string { @@ -41,15 +88,17 @@ func (m *migration) makeAlertRule(cond condition, da dashAlert, folderUID string ar := &alertRule{ OrgId: da.OrgId, Title: da.Name, // TODO: Make sure all names are unique, make new name on constraint insert error. + Uid: util.GenerateShortUID(), Condition: cond.Condition, Data: cond.Data, IntervalSeconds: ruleAdjustInterval(da.Frequency), + Version: 1, NamespaceUid: folderUID, // Folder already created, comes from env var. RuleGroup: da.Name, For: duration(da.For), Updated: time.Now().UTC(), Annotations: annotations, - Uid: util.GenerateShortUID(), + Labels: map[string]string{}, } var err error diff --git a/pkg/services/sqlstore/migrations/ualert/ualert.go b/pkg/services/sqlstore/migrations/ualert/ualert.go index 147482401b9..3ffb5dddd60 100644 --- a/pkg/services/sqlstore/migrations/ualert/ualert.go +++ b/pkg/services/sqlstore/migrations/ualert/ualert.go @@ -174,6 +174,12 @@ func (m *migration) Exec(sess *xorm.Session, mg *migrator.Migrator) error { return err } } + + // create entry in alert_rule_version + _, err = m.sess.Insert(rule.makeVersion()) + if err != nil { + return err + } } return nil