mirror of
https://github.com/grafana/grafana.git
synced 2025-02-13 00:55:47 -06:00
Annotations: Ignore unique constraint violations for tags (#65935)
This commit is contained in:
parent
9d69d3173f
commit
948131aac3
@ -14,16 +14,18 @@ type sqlStore struct {
|
||||
func (s *sqlStore) EnsureTagsExist(ctx context.Context, tags []*tag.Tag) ([]*tag.Tag, error) {
|
||||
err := s.db.WithDbSession(ctx, func(sess *db.Session) error {
|
||||
for _, tagElement := range tags {
|
||||
var existingTag tag.Tag
|
||||
exists, err := sess.Table("tag").Where("`key`=? AND `value`=?", tagElement.Key, tagElement.Value).Get(&existingTag)
|
||||
exists, err := s.innerGetTag(sess, tagElement)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if exists {
|
||||
tagElement.Id = existingTag.Id
|
||||
} else {
|
||||
if !exists {
|
||||
_, err := sess.Table("tag").Insert(tagElement)
|
||||
if err != nil {
|
||||
if s.db.GetDialect().IsUniqueConstraintViolation(err) {
|
||||
_, err := s.innerGetTag(sess, tagElement)
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
}
|
||||
@ -32,3 +34,17 @@ func (s *sqlStore) EnsureTagsExist(ctx context.Context, tags []*tag.Tag) ([]*tag
|
||||
})
|
||||
return tags, err
|
||||
}
|
||||
|
||||
func (s *sqlStore) innerGetTag(sess *db.Session, tagElement *tag.Tag) (bool, error) {
|
||||
var existingTag tag.Tag
|
||||
exists, err := sess.Table("tag").Where("`key`=? AND `value`=?", tagElement.Key, tagElement.Value).Get(&existingTag)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if !exists {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
tagElement.Id = existingTag.Id
|
||||
return true, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user