From a30ca8608493061ff5b334c52bd6d1c17e4e1746 Mon Sep 17 00:00:00 2001 From: Katarina Yang <69819079+yangkb09@users.noreply.github.com> Date: Fri, 7 Jan 2022 11:50:59 -0500 Subject: [PATCH] =?UTF-8?q?Refactor:=20Change=20sqlstore.inTransaction=20t?= =?UTF-8?q?o=20SQLStore.WithTransactional=E2=80=A6=20(#43772)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Refactor: Change sqlstore.inTransaction to SQLStore.WithTransactionalDBSession in dashboard * Fix: Fix failing lint and BE tests --- .../dashboardsnapshots/dashboardsnapshots.go | 10 +++++----- pkg/services/sqlstore/dashboard_snapshot.go | 13 +++++++------ pkg/services/sqlstore/dashboard_snapshot_test.go | 10 +++++----- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/pkg/services/dashboardsnapshots/dashboardsnapshots.go b/pkg/services/dashboardsnapshots/dashboardsnapshots.go index f150ab4ab54..48dfb5ab2f1 100644 --- a/pkg/services/dashboardsnapshots/dashboardsnapshots.go +++ b/pkg/services/dashboardsnapshots/dashboardsnapshots.go @@ -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) } diff --git a/pkg/services/sqlstore/dashboard_snapshot.go b/pkg/services/sqlstore/dashboard_snapshot.go index e1e9d21d6bf..38584e26a4e 100644 --- a/pkg/services/sqlstore/dashboard_snapshot.go +++ b/pkg/services/sqlstore/dashboard_snapshot.go @@ -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 diff --git a/pkg/services/sqlstore/dashboard_snapshot_test.go b/pkg/services/sqlstore/dashboard_snapshot_test.go index 059954c625d..ebaf1091ebb 100644 --- a/pkg/services/sqlstore/dashboard_snapshot_test.go +++ b/pkg/services/sqlstore/dashboard_snapshot_test.go @@ -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