mirror of
https://github.com/grafana/grafana.git
synced 2025-09-20 01:39:53 +08:00
Annotations: Ignore unique constraint violations for tags (#65935)
This commit is contained in:
@ -14,16 +14,18 @@ type sqlStore struct {
|
|||||||
func (s *sqlStore) EnsureTagsExist(ctx context.Context, tags []*tag.Tag) ([]*tag.Tag, error) {
|
func (s *sqlStore) EnsureTagsExist(ctx context.Context, tags []*tag.Tag) ([]*tag.Tag, error) {
|
||||||
err := s.db.WithDbSession(ctx, func(sess *db.Session) error {
|
err := s.db.WithDbSession(ctx, func(sess *db.Session) error {
|
||||||
for _, tagElement := range tags {
|
for _, tagElement := range tags {
|
||||||
var existingTag tag.Tag
|
exists, err := s.innerGetTag(sess, tagElement)
|
||||||
exists, err := sess.Table("tag").Where("`key`=? AND `value`=?", tagElement.Key, tagElement.Value).Get(&existingTag)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if exists {
|
if !exists {
|
||||||
tagElement.Id = existingTag.Id
|
|
||||||
} else {
|
|
||||||
_, err := sess.Table("tag").Insert(tagElement)
|
_, err := sess.Table("tag").Insert(tagElement)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if s.db.GetDialect().IsUniqueConstraintViolation(err) {
|
||||||
|
_, err := s.innerGetTag(sess, tagElement)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -32,3 +34,17 @@ func (s *sqlStore) EnsureTagsExist(ctx context.Context, tags []*tag.Tag) ([]*tag
|
|||||||
})
|
})
|
||||||
return tags, err
|
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
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user