Split Delete expired dashboard version store (#49610)
* Split Delete expired dashboard version store * Add method to fakes * Fix lint * Fix lint 2 * Use split store method in cleanup * Add tests * Remove DeleteExpiredVersions from sqlstore * Fix lint * Fix integration tests
This commit is contained in:
@@ -2,24 +2,26 @@ package dashverimpl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
dashver "github.com/grafana/grafana/pkg/services/dashboardversion"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore/db"
|
||||
)
|
||||
|
||||
type store interface {
|
||||
Get(ctx context.Context, query *dashver.GetDashboardVersionQuery) (*dashver.DashboardVersion, error)
|
||||
Get(context.Context, *dashver.GetDashboardVersionQuery) (*dashver.DashboardVersion, error)
|
||||
GetBatch(context.Context, *dashver.DeleteExpiredVersionsCommand, int, int) ([]interface{}, error)
|
||||
DeleteBatch(context.Context, *dashver.DeleteExpiredVersionsCommand, []interface{}) (int64, error)
|
||||
}
|
||||
|
||||
type sqlStore struct {
|
||||
db db.DB
|
||||
}
|
||||
|
||||
func (s *sqlStore) Get(ctx context.Context, query *dashver.GetDashboardVersionQuery) (*dashver.DashboardVersion, error) {
|
||||
func (ss *sqlStore) Get(ctx context.Context, query *dashver.GetDashboardVersionQuery) (*dashver.DashboardVersion, error) {
|
||||
var version dashver.DashboardVersion
|
||||
err := s.db.WithDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
||||
err := ss.db.WithDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
||||
has, err := sess.Where("dashboard_version.dashboard_id=? AND dashboard_version.version=? AND dashboard.org_id=?", query.DashboardID, query.Version, query.OrgID).
|
||||
Join("LEFT", "dashboard", `dashboard.id = dashboard_version.dashboard_id`).
|
||||
Get(&version)
|
||||
@@ -29,7 +31,7 @@ func (s *sqlStore) Get(ctx context.Context, query *dashver.GetDashboardVersionQu
|
||||
}
|
||||
|
||||
if !has {
|
||||
return models.ErrDashboardVersionNotFound
|
||||
return dashver.ErrDashboardVersionNotFound
|
||||
}
|
||||
return nil
|
||||
})
|
||||
@@ -38,3 +40,38 @@ func (s *sqlStore) Get(ctx context.Context, query *dashver.GetDashboardVersionQu
|
||||
}
|
||||
return &version, nil
|
||||
}
|
||||
|
||||
func (ss *sqlStore) GetBatch(ctx context.Context, cmd *dashver.DeleteExpiredVersionsCommand, perBatch int, versionsToKeep int) ([]interface{}, error) {
|
||||
var versionIds []interface{}
|
||||
err := ss.db.WithTransactionalDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
||||
versionIdsToDeleteQuery := `SELECT id
|
||||
FROM dashboard_version, (
|
||||
SELECT dashboard_id, count(version) as count, min(version) as min
|
||||
FROM dashboard_version
|
||||
GROUP BY dashboard_id
|
||||
) AS vtd
|
||||
WHERE dashboard_version.dashboard_id=vtd.dashboard_id
|
||||
AND version < vtd.min + vtd.count - ?
|
||||
LIMIT ?`
|
||||
|
||||
err := sess.SQL(versionIdsToDeleteQuery, versionsToKeep, perBatch).Find(&versionIds)
|
||||
return err
|
||||
})
|
||||
return versionIds, err
|
||||
}
|
||||
|
||||
func (ss *sqlStore) DeleteBatch(ctx context.Context, cmd *dashver.DeleteExpiredVersionsCommand, versionIdsToDelete []interface{}) (int64, error) {
|
||||
var deleted int64
|
||||
err := ss.db.WithTransactionalDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
||||
deleteExpiredSQL := `DELETE FROM dashboard_version WHERE id IN (?` + strings.Repeat(",?", len(versionIdsToDelete)-1) + `)`
|
||||
sqlOrArgs := append([]interface{}{deleteExpiredSQL}, versionIdsToDelete...)
|
||||
expiredResponse, err := sess.Exec(sqlOrArgs...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
deleted, err = expiredResponse.RowsAffected()
|
||||
return err
|
||||
})
|
||||
return deleted, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user