Stars: Remove sqlx storage implementation (#77766)

This commit is contained in:
Ryan McKinley 2023-11-07 06:55:24 -08:00 committed by GitHub
parent 71dd335bf8
commit 229fb2c261
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 1 additions and 94 deletions

View File

@ -1,67 +0,0 @@
package starimpl
import (
"context"
"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) {
var star_res star.Star
err := s.sess.Get(ctx, &star_res, "SELECT * from star where user_id=? and dashboard_id=?", query.UserID, query.DashboardID)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return false, nil
}
return false, err
}
return true, nil
}
func (s *sqlxStore) Insert(ctx context.Context, cmd *star.StarDashboardCommand) error {
entity := star.Star{
UserID: cmd.UserID,
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
}
return err
}
func (s *sqlxStore) Delete(ctx context.Context, cmd *star.UnstarDashboardCommand) error {
_, err := s.sess.Exec(ctx, "DELETE FROM star WHERE user_id=? and dashboard_id=?", cmd.UserID, cmd.DashboardID)
return err
}
func (s *sqlxStore) DeleteByUser(ctx context.Context, userID int64) error {
_, err := s.sess.Exec(ctx, "DELETE FROM star WHERE user_id = ?", userID)
return err
}
func (s *sqlxStore) List(ctx context.Context, query *star.GetUserStarsQuery) (*star.GetUserStarsResult, error) {
userStars := make(map[int64]bool)
var stars = make([]star.Star, 0)
err := s.sess.Select(ctx, &stars, "SELECT * FROM star WHERE user_id=?", query.UserID)
if err != nil {
return nil, err
}
for _, star := range stars {
userStars[star.DashboardID] = true
}
return &star.GetUserStarsResult{UserStars: userStars}, err
}

View File

@ -1,16 +0,0 @@
package starimpl
import (
"testing"
"github.com/grafana/grafana/pkg/infra/db"
)
func TestIntegrationSQLxUserStarsDataAccess(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
testIntegrationUserStarsDataAccess(t, func(ss db.DB) store {
return &sqlxStore{sess: ss.GetSqlxSession(), db: ss}
})
}

View File

@ -4,24 +4,14 @@ import (
"context"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/services/star"
"github.com/grafana/grafana/pkg/setting"
)
type Service struct {
store store
}
func ProvideService(db db.DB, cfg *setting.Cfg) star.Service {
if cfg.IsFeatureToggleEnabled(featuremgmt.FlagNewDBLibrary) {
return &Service{
store: &sqlxStore{
sess: db.GetSqlxSession(),
db: db,
},
}
}
func ProvideService(db db.DB) star.Service {
return &Service{
store: &sqlStore{
db: db,