Refactor: Change sqlstore.inTransaction to SQLStore.WithTransactional… (#43772)

* Refactor: Change sqlstore.inTransaction to SQLStore.WithTransactionalDBSession in dashboard

* Fix: Fix failing lint and BE tests
This commit is contained in:
Katarina Yang 2022-01-07 11:50:59 -05:00 committed by GitHub
parent a29ddfd386
commit a30ca86084
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 16 deletions

View File

@ -45,7 +45,7 @@ func (s *Service) CreateDashboardSnapshot(ctx context.Context, cmd *models.Creat
cmd.DashboardEncrypted = encryptedDashboard
return s.SQLStore.CreateDashboardSnapshot(cmd)
return s.SQLStore.CreateDashboardSnapshot(ctx, cmd)
}
func (s *Service) GetDashboardSnapshot(ctx context.Context, query *models.GetDashboardSnapshotQuery) error {
@ -71,14 +71,14 @@ func (s *Service) GetDashboardSnapshot(ctx context.Context, query *models.GetDas
return err
}
func (s *Service) DeleteDashboardSnapshot(_ context.Context, cmd *models.DeleteDashboardSnapshotCommand) error {
return s.SQLStore.DeleteDashboardSnapshot(cmd)
func (s *Service) DeleteDashboardSnapshot(ctx context.Context, cmd *models.DeleteDashboardSnapshotCommand) error {
return s.SQLStore.DeleteDashboardSnapshot(ctx, cmd)
}
func (s *Service) SearchDashboardSnapshots(_ context.Context, query *models.GetDashboardSnapshotsQuery) error {
return s.SQLStore.SearchDashboardSnapshots(query)
}
func (s *Service) DeleteExpiredSnapshots(_ context.Context, cmd *models.DeleteExpiredSnapshotsCommand) error {
return s.SQLStore.DeleteExpiredSnapshots(cmd)
func (s *Service) DeleteExpiredSnapshots(ctx context.Context, cmd *models.DeleteExpiredSnapshotsCommand) error {
return s.SQLStore.DeleteExpiredSnapshots(ctx, cmd)
}

View File

@ -1,6 +1,7 @@
package sqlstore
import (
"context"
"time"
"github.com/grafana/grafana/pkg/components/simplejson"
@ -11,8 +12,8 @@ import (
// DeleteExpiredSnapshots removes snapshots with old expiry dates.
// SnapShotRemoveExpired is deprecated and should be removed in the future.
// Snapshot expiry is decided by the user when they share the snapshot.
func (ss *SQLStore) DeleteExpiredSnapshots(cmd *models.DeleteExpiredSnapshotsCommand) error {
return inTransaction(func(sess *DBSession) error {
func (ss *SQLStore) DeleteExpiredSnapshots(ctx context.Context, cmd *models.DeleteExpiredSnapshotsCommand) error {
return ss.WithTransactionalDbSession(ctx, func(sess *DBSession) error {
if !setting.SnapShotRemoveExpired {
sqlog.Warn("[Deprecated] The snapshot_remove_expired setting is outdated. Please remove from your config.")
return nil
@ -29,8 +30,8 @@ func (ss *SQLStore) DeleteExpiredSnapshots(cmd *models.DeleteExpiredSnapshotsCom
})
}
func (ss *SQLStore) CreateDashboardSnapshot(cmd *models.CreateDashboardSnapshotCommand) error {
return inTransaction(func(sess *DBSession) error {
func (ss *SQLStore) CreateDashboardSnapshot(ctx context.Context, cmd *models.CreateDashboardSnapshotCommand) error {
return ss.WithTransactionalDbSession(ctx, func(sess *DBSession) error {
// never
var expires = time.Now().Add(time.Hour * 24 * 365 * 50)
if cmd.Expires > 0 {
@ -59,8 +60,8 @@ func (ss *SQLStore) CreateDashboardSnapshot(cmd *models.CreateDashboardSnapshotC
})
}
func (ss *SQLStore) DeleteDashboardSnapshot(cmd *models.DeleteDashboardSnapshotCommand) error {
return inTransaction(func(sess *DBSession) error {
func (ss *SQLStore) DeleteDashboardSnapshot(ctx context.Context, cmd *models.DeleteDashboardSnapshotCommand) error {
return ss.WithTransactionalDbSession(ctx, func(sess *DBSession) error {
var rawSQL = "DELETE FROM dashboard_snapshot WHERE delete_key=?"
_, err := sess.Exec(rawSQL, cmd.DeleteKey)
return err

View File

@ -42,7 +42,7 @@ func TestDashboardSnapshotDBAccess(t *testing.T) {
OrgId: 1,
}
err = sqlstore.CreateDashboardSnapshot(&cmd)
err = sqlstore.CreateDashboardSnapshot(context.Background(), &cmd)
require.NoError(t, err)
t.Run("Should be able to get snapshot by key", func(t *testing.T) {
@ -116,7 +116,7 @@ func TestDashboardSnapshotDBAccess(t *testing.T) {
UserId: 0,
OrgId: 1,
}
err := sqlstore.CreateDashboardSnapshot(&cmd)
err := sqlstore.CreateDashboardSnapshot(context.Background(), &cmd)
require.NoError(t, err)
t.Run("Should not return any snapshots", func(t *testing.T) {
@ -154,7 +154,7 @@ func TestDeleteExpiredSnapshots(t *testing.T) {
createTestSnapshot(t, sqlstore, "key2", -1200)
createTestSnapshot(t, sqlstore, "key3", -1200)
err := sqlstore.DeleteExpiredSnapshots(&models.DeleteExpiredSnapshotsCommand{})
err := sqlstore.DeleteExpiredSnapshots(context.Background(), &models.DeleteExpiredSnapshotsCommand{})
require.NoError(t, err)
query := models.GetDashboardSnapshotsQuery{
@ -167,7 +167,7 @@ func TestDeleteExpiredSnapshots(t *testing.T) {
assert.Len(t, query.Result, 1)
assert.Equal(t, nonExpiredSnapshot.Key, query.Result[0].Key)
err = sqlstore.DeleteExpiredSnapshots(&models.DeleteExpiredSnapshotsCommand{})
err = sqlstore.DeleteExpiredSnapshots(context.Background(), &models.DeleteExpiredSnapshotsCommand{})
require.NoError(t, err)
query = models.GetDashboardSnapshotsQuery{
@ -193,7 +193,7 @@ func createTestSnapshot(t *testing.T, sqlstore *SQLStore, key string, expires in
OrgId: 1,
Expires: expires,
}
err := sqlstore.CreateDashboardSnapshot(&cmd)
err := sqlstore.CreateDashboardSnapshot(context.Background(), &cmd)
require.NoError(t, err)
// Set expiry date manually - to be able to create expired snapshots