Stars: Remove deprecated internal ID apis (#110499)

This commit is contained in:
Ryan McKinley
2025-09-04 14:45:01 -05:00
committed by GitHub
parent 3d6d632686
commit 1dadf2cad9
10 changed files with 20 additions and 382 deletions
-43
View File
@@ -27,46 +27,6 @@ func testIntegrationUserStarsDataAccess(t *testing.T, fn getStore) {
ss := db.InitTestDB(t)
starStore := fn(ss)
t.Run("Given saved star by dashboard id", func(t *testing.T) {
cmd := star.StarDashboardCommand{
DashboardID: 10,
UserID: 12,
}
err := starStore.Insert(context.Background(), &cmd)
require.NoError(t, err)
t.Run("Get should return true when starred", func(t *testing.T) {
query := star.IsStarredByUserQuery{UserID: 12, DashboardID: 10}
isStarred, err := starStore.Get(context.Background(), &query)
require.NoError(t, err)
require.True(t, isStarred)
})
t.Run("Get should return false when not starred", func(t *testing.T) {
query := star.IsStarredByUserQuery{UserID: 12, DashboardID: 12}
isStarred, err := starStore.Get(context.Background(), &query)
require.NoError(t, err)
require.False(t, isStarred)
})
t.Run("List should return a list of size 1", func(t *testing.T) {
query := star.GetUserStarsQuery{UserID: 12}
result, err := starStore.List(context.Background(), &query)
require.NoError(t, err)
require.Equal(t, 1, len(result.UserStars))
})
t.Run("Delete should remove the star", func(t *testing.T) {
deleteQuery := star.UnstarDashboardCommand{DashboardID: 10, UserID: 12}
err := starStore.Delete(context.Background(), &deleteQuery)
require.NoError(t, err)
getQuery := star.IsStarredByUserQuery{UserID: 12, DashboardID: 10}
isStarred, err := starStore.Get(context.Background(), &getQuery)
require.NoError(t, err)
require.False(t, isStarred)
})
})
t.Run("Given saved star by dashboard UID", func(t *testing.T) {
cmd := star.StarDashboardCommand{
DashboardUID: "test",
@@ -113,7 +73,6 @@ func testIntegrationUserStarsDataAccess(t *testing.T, fn getStore) {
DashboardUID: "test",
OrgID: 1,
Updated: time.Now(),
DashboardID: 10,
UserID: 12,
}
err := starStore.Insert(context.Background(), &star1)
@@ -122,7 +81,6 @@ func testIntegrationUserStarsDataAccess(t *testing.T, fn getStore) {
DashboardUID: "test2",
OrgID: 1,
Updated: time.Now(),
DashboardID: 11,
UserID: 12,
}
err = starStore.Insert(context.Background(), &star2)
@@ -131,7 +89,6 @@ func testIntegrationUserStarsDataAccess(t *testing.T, fn getStore) {
DashboardUID: "test2",
OrgID: 1,
Updated: time.Now(),
DashboardID: 11,
UserID: 11,
}
err = starStore.Insert(context.Background(), &star3)
+11 -26
View File
@@ -2,6 +2,8 @@ package starimpl
import (
"context"
"math/rand"
"time"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/services/star"
@@ -14,22 +16,9 @@ type sqlStore struct {
func (s *sqlStore) Get(ctx context.Context, query *star.IsStarredByUserQuery) (bool, error) {
var isStarred bool
err := s.db.WithDbSession(ctx, func(sess *db.Session) error {
if query.DashboardUID != "" && query.OrgID != 0 {
rawSQL := "SELECT 1 from star where user_id=? and dashboard_uid=? and org_id=?"
results, err := sess.Query(rawSQL, query.UserID, query.DashboardUID, query.OrgID)
rawSQL := "SELECT 1 from star where user_id=? and dashboard_uid=? and org_id=?"
results, err := sess.Query(rawSQL, query.UserID, query.DashboardUID, query.OrgID)
if err != nil {
return err
}
isStarred = len(results) != 0
return nil
}
// TODO: Remove this block after all dashboards have a UID
// && the deprecated endpoints have been removed
rawSQL := "SELECT 1 from star where user_id=? and dashboard_id=?"
results, err := sess.Query(rawSQL, query.UserID, query.DashboardID)
if err != nil {
return err
}
@@ -42,6 +31,11 @@ func (s *sqlStore) Get(ctx context.Context, query *star.IsStarredByUserQuery) (b
func (s *sqlStore) Insert(ctx context.Context, cmd *star.StarDashboardCommand) error {
return s.db.WithTransactionalDbSession(ctx, func(sess *db.Session) error {
// nolint:staticcheck
if cmd.DashboardID == 0 {
cmd.DashboardID = time.Now().UnixMicro() + rand.Int63n(5000) // random unique value
}
entity := star.Star{
UserID: cmd.UserID,
// nolint:staticcheck
@@ -62,17 +56,8 @@ func (s *sqlStore) Insert(ctx context.Context, cmd *star.StarDashboardCommand) e
func (s *sqlStore) Delete(ctx context.Context, cmd *star.UnstarDashboardCommand) error {
return s.db.WithTransactionalDbSession(ctx, func(sess *db.Session) error {
if cmd.DashboardUID != "" && cmd.OrgID != 0 {
var rawSQL = "DELETE FROM star WHERE user_id=? and dashboard_uid=? and org_id=?"
_, err := sess.Exec(rawSQL, cmd.UserID, cmd.DashboardUID, cmd.OrgID)
return err
}
// TODO: Remove this block after all dashboards have a UID
// && the deprecated endpoints have been removed
var rawSQL = "DELETE FROM star WHERE user_id=? and dashboard_id=?"
// nolint:staticcheck
_, err := sess.Exec(rawSQL, cmd.UserID, cmd.DashboardID)
var rawSQL = "DELETE FROM star WHERE user_id=? and dashboard_uid=? and org_id=?"
_, err := sess.Exec(rawSQL, cmd.UserID, cmd.DashboardUID, cmd.OrgID)
return err
})
}