Stars: Add dashboard_uid and org_id to table (#96408)
This commit is contained in:
@@ -4,18 +4,30 @@ import (
|
||||
"context"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/db"
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore/migrations"
|
||||
"github.com/grafana/grafana/pkg/services/star"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
store store
|
||||
store store
|
||||
logger log.Logger
|
||||
}
|
||||
|
||||
func ProvideService(db db.DB) star.Service {
|
||||
starLogger := log.New("stars")
|
||||
// fill out dashboard_uid, org_id and updated columns for stars
|
||||
// need to run this at startup in case any downgrade happened after
|
||||
// the initial migration
|
||||
err := migrations.RunStarMigrations(db.GetEngine().NewSession(), db.GetDialect().DriverName())
|
||||
if err != nil {
|
||||
starLogger.Error("Failed to run star migrations", "err", err)
|
||||
}
|
||||
return &Service{
|
||||
store: &sqlStore{
|
||||
db: db,
|
||||
},
|
||||
logger: starLogger,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package starimpl
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
@@ -24,7 +25,7 @@ func testIntegrationUserStarsDataAccess(t *testing.T, fn getStore) {
|
||||
ss := db.InitTestDB(t)
|
||||
starStore := fn(ss)
|
||||
|
||||
t.Run("Given saved star", func(t *testing.T) {
|
||||
t.Run("Given saved star by dashboard id", func(t *testing.T) {
|
||||
cmd := star.StarDashboardCommand{
|
||||
DashboardID: 10,
|
||||
UserID: 12,
|
||||
@@ -64,22 +65,72 @@ func testIntegrationUserStarsDataAccess(t *testing.T, fn getStore) {
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("Given saved star by dashboard UID", func(t *testing.T) {
|
||||
cmd := star.StarDashboardCommand{
|
||||
DashboardUID: "test",
|
||||
OrgID: 1,
|
||||
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, DashboardUID: "test", OrgID: 1}
|
||||
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, DashboardUID: "testing", OrgID: 1}
|
||||
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{DashboardUID: "test", OrgID: 1, UserID: 12}
|
||||
err := starStore.Delete(context.Background(), &deleteQuery)
|
||||
require.NoError(t, err)
|
||||
getQuery := star.IsStarredByUserQuery{UserID: 12, DashboardUID: "test", OrgID: 1}
|
||||
isStarred, err := starStore.Get(context.Background(), &getQuery)
|
||||
require.NoError(t, err)
|
||||
require.False(t, isStarred)
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("DeleteByUser should remove the star for user", func(t *testing.T) {
|
||||
star1 := star.StarDashboardCommand{
|
||||
DashboardID: 10,
|
||||
UserID: 12,
|
||||
DashboardUID: "test",
|
||||
OrgID: 1,
|
||||
Updated: time.Now(),
|
||||
DashboardID: 10,
|
||||
UserID: 12,
|
||||
}
|
||||
err := starStore.Insert(context.Background(), &star1)
|
||||
require.NoError(t, err)
|
||||
star2 := star.StarDashboardCommand{
|
||||
DashboardID: 11,
|
||||
UserID: 12,
|
||||
DashboardUID: "test2",
|
||||
OrgID: 1,
|
||||
Updated: time.Now(),
|
||||
DashboardID: 11,
|
||||
UserID: 12,
|
||||
}
|
||||
err = starStore.Insert(context.Background(), &star2)
|
||||
require.NoError(t, err)
|
||||
star3 := star.StarDashboardCommand{
|
||||
DashboardID: 11,
|
||||
UserID: 11,
|
||||
DashboardUID: "test2",
|
||||
OrgID: 1,
|
||||
Updated: time.Now(),
|
||||
DashboardID: 11,
|
||||
UserID: 11,
|
||||
}
|
||||
err = starStore.Insert(context.Background(), &star3)
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -14,9 +14,22 @@ 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)
|
||||
|
||||
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
|
||||
}
|
||||
@@ -30,8 +43,12 @@ 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 {
|
||||
entity := star.Star{
|
||||
UserID: cmd.UserID,
|
||||
DashboardID: cmd.DashboardID,
|
||||
UserID: cmd.UserID,
|
||||
// nolint:staticcheck
|
||||
DashboardID: cmd.DashboardID,
|
||||
DashboardUID: cmd.DashboardUID,
|
||||
OrgID: cmd.OrgID,
|
||||
Updated: cmd.Updated,
|
||||
}
|
||||
|
||||
_, err := sess.Insert(&entity)
|
||||
@@ -45,7 +62,16 @@ 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)
|
||||
return err
|
||||
})
|
||||
@@ -60,12 +86,12 @@ func (s *sqlStore) DeleteByUser(ctx context.Context, userID int64) error {
|
||||
}
|
||||
|
||||
func (s *sqlStore) List(ctx context.Context, query *star.GetUserStarsQuery) (*star.GetUserStarsResult, error) {
|
||||
userStars := make(map[int64]bool)
|
||||
userStars := make(map[string]bool)
|
||||
err := s.db.WithDbSession(ctx, func(dbSession *db.Session) error {
|
||||
var stars = make([]star.Star, 0)
|
||||
err := dbSession.Where("user_id=?", query.UserID).Find(&stars)
|
||||
for _, star := range stars {
|
||||
userStars[star.DashboardID] = true
|
||||
userStars[star.DashboardUID] = true
|
||||
}
|
||||
return err
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user