From b10b290213998aaff0554637c17b986dae075ba9 Mon Sep 17 00:00:00 2001 From: Sanghyuk Chris Lee Date: Thu, 11 May 2023 04:37:36 -0400 Subject: [PATCH] API: Fix status code when starring already starred dashboard (#63478) Fixes #63133 --- pkg/services/star/starimpl/sqlx_store.go | 5 +++++ pkg/services/star/starimpl/sqlx_store_test.go | 2 +- pkg/services/star/starimpl/star.go | 1 + pkg/services/star/starimpl/xorm_store.go | 4 ++++ 4 files changed, 11 insertions(+), 1 deletion(-) diff --git a/pkg/services/star/starimpl/sqlx_store.go b/pkg/services/star/starimpl/sqlx_store.go index c953eda7476..305229fe3e0 100644 --- a/pkg/services/star/starimpl/sqlx_store.go +++ b/pkg/services/star/starimpl/sqlx_store.go @@ -5,12 +5,14 @@ import ( "database/sql" "errors" + "github.com/grafana/grafana/pkg/infra/db" "github.com/grafana/grafana/pkg/services/sqlstore/session" "github.com/grafana/grafana/pkg/services/star" ) type sqlxStore struct { sess *session.SessionDB + db db.DB } func (s *sqlxStore) Get(ctx context.Context, query *star.IsStarredByUserQuery) (bool, error) { @@ -31,6 +33,9 @@ func (s *sqlxStore) Insert(ctx context.Context, cmd *star.StarDashboardCommand) DashboardID: cmd.DashboardID, } _, err := s.sess.NamedExec(ctx, `INSERT INTO star (user_id, dashboard_id) VALUES (:user_id, :dashboard_id)`, entity) + if s.db.GetDialect().IsUniqueConstraintViolation(err) { + return nil + } if err != nil { return err } diff --git a/pkg/services/star/starimpl/sqlx_store_test.go b/pkg/services/star/starimpl/sqlx_store_test.go index 3d9045e7475..edff42ea5f4 100644 --- a/pkg/services/star/starimpl/sqlx_store_test.go +++ b/pkg/services/star/starimpl/sqlx_store_test.go @@ -11,6 +11,6 @@ func TestIntegrationSQLxUserStarsDataAccess(t *testing.T) { t.Skip("skipping integration test") } testIntegrationUserStarsDataAccess(t, func(ss db.DB) store { - return &sqlxStore{sess: ss.GetSqlxSession()} + return &sqlxStore{sess: ss.GetSqlxSession(), db: ss} }) } diff --git a/pkg/services/star/starimpl/star.go b/pkg/services/star/starimpl/star.go index bc745b8511b..adc49002c47 100644 --- a/pkg/services/star/starimpl/star.go +++ b/pkg/services/star/starimpl/star.go @@ -18,6 +18,7 @@ func ProvideService(db db.DB, cfg *setting.Cfg) star.Service { return &Service{ store: &sqlxStore{ sess: db.GetSqlxSession(), + db: db, }, } } diff --git a/pkg/services/star/starimpl/xorm_store.go b/pkg/services/star/starimpl/xorm_store.go index ef2c20803f3..914bafa193d 100644 --- a/pkg/services/star/starimpl/xorm_store.go +++ b/pkg/services/star/starimpl/xorm_store.go @@ -35,6 +35,10 @@ func (s *sqlStore) Insert(ctx context.Context, cmd *star.StarDashboardCommand) e } _, err := sess.Insert(&entity) + if s.db.GetDialect().IsUniqueConstraintViolation(err) { + return nil + } + return err }) }