Annotations: Ignore unique constraint violations for tags (#65935)

This commit is contained in:
Emil Tullstedt 2023-04-18 14:21:38 +02:00 committed by GitHub
parent 9d69d3173f
commit 948131aac3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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
}