mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Refactor: Change sqlstore.inTransaction to .WithTransactionalDBSession (#43245)
* Refactor: Change sqlstore.inTransaction(...) to SQLStore.WithTransactionalDBSession(...) in alert_notification.go * Chore: Fix BE lint err * fix: fix failing sqlstore nil error * chore: remove unecessary setup() * fix: use appropriate ctx
This commit is contained in:
parent
846c6ce758
commit
7f8daa0eae
@ -14,7 +14,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (ss *SQLStore) DeleteAlertNotification(ctx context.Context, cmd *models.DeleteAlertNotificationCommand) error {
|
func (ss *SQLStore) DeleteAlertNotification(ctx context.Context, cmd *models.DeleteAlertNotificationCommand) error {
|
||||||
return inTransaction(func(sess *DBSession) error {
|
return ss.WithTransactionalDbSession(ctx, func(sess *DBSession) error {
|
||||||
sql := "DELETE FROM alert_notification WHERE alert_notification.org_id = ? AND alert_notification.id = ?"
|
sql := "DELETE FROM alert_notification WHERE alert_notification.org_id = ? AND alert_notification.id = ?"
|
||||||
res, err := sess.Exec(sql, cmd.OrgId, cmd.Id)
|
res, err := sess.Exec(sql, cmd.OrgId, cmd.Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -39,7 +39,7 @@ func (ss *SQLStore) DeleteAlertNotification(ctx context.Context, cmd *models.Del
|
|||||||
|
|
||||||
func (ss *SQLStore) DeleteAlertNotificationWithUid(ctx context.Context, cmd *models.DeleteAlertNotificationWithUidCommand) error {
|
func (ss *SQLStore) DeleteAlertNotificationWithUid(ctx context.Context, cmd *models.DeleteAlertNotificationWithUidCommand) error {
|
||||||
existingNotification := &models.GetAlertNotificationsWithUidQuery{OrgId: cmd.OrgId, Uid: cmd.Uid}
|
existingNotification := &models.GetAlertNotificationsWithUidQuery{OrgId: cmd.OrgId, Uid: cmd.Uid}
|
||||||
if err := getAlertNotificationWithUidInternal(ctx, existingNotification, newSession(context.Background())); err != nil {
|
if err := getAlertNotificationWithUidInternal(ctx, existingNotification, newSession(ctx)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,7 +60,7 @@ func (ss *SQLStore) DeleteAlertNotificationWithUid(ctx context.Context, cmd *mod
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ss *SQLStore) GetAlertNotifications(ctx context.Context, query *models.GetAlertNotificationsQuery) error {
|
func (ss *SQLStore) GetAlertNotifications(ctx context.Context, query *models.GetAlertNotificationsQuery) error {
|
||||||
return getAlertNotificationInternal(ctx, query, newSession(context.Background()))
|
return getAlertNotificationInternal(ctx, query, newSession(ctx))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ss *SQLStore) addAlertNotificationUidByIdHandler() {
|
func (ss *SQLStore) addAlertNotificationUidByIdHandler() {
|
||||||
@ -75,7 +75,7 @@ func (ss *SQLStore) GetAlertNotificationUidWithId(ctx context.Context, query *mo
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
err := getAlertNotificationUidInternal(ctx, query, newSession(context.Background()))
|
err := getAlertNotificationUidInternal(ctx, query, newSession(ctx))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -90,20 +90,23 @@ func newAlertNotificationUidCacheKey(orgID, notificationId int64) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ss *SQLStore) GetAlertNotificationsWithUid(ctx context.Context, query *models.GetAlertNotificationsWithUidQuery) error {
|
func (ss *SQLStore) GetAlertNotificationsWithUid(ctx context.Context, query *models.GetAlertNotificationsWithUidQuery) error {
|
||||||
return getAlertNotificationWithUidInternal(ctx, query, newSession(context.Background()))
|
return getAlertNotificationWithUidInternal(ctx, query, newSession(ctx))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ss *SQLStore) GetAllAlertNotifications(ctx context.Context, query *models.GetAllAlertNotificationsQuery) error {
|
func (ss *SQLStore) GetAllAlertNotifications(ctx context.Context, query *models.GetAllAlertNotificationsQuery) error {
|
||||||
|
return ss.WithDbSession(ctx, func(sess *DBSession) error {
|
||||||
results := make([]*models.AlertNotification, 0)
|
results := make([]*models.AlertNotification, 0)
|
||||||
if err := x.Where("org_id = ?", query.OrgId).Asc("name").Find(&results); err != nil {
|
if err := sess.Where("org_id = ?", query.OrgId).Asc("name").Find(&results); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
query.Result = results
|
query.Result = results
|
||||||
return nil
|
return nil
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ss *SQLStore) GetAlertNotificationsWithUidToSend(ctx context.Context, query *models.GetAlertNotificationsWithUidToSendQuery) error {
|
func (ss *SQLStore) GetAlertNotificationsWithUidToSend(ctx context.Context, query *models.GetAlertNotificationsWithUidToSendQuery) error {
|
||||||
|
return ss.WithDbSession(ctx, func(sess *DBSession) error {
|
||||||
var sql bytes.Buffer
|
var sql bytes.Buffer
|
||||||
params := make([]interface{}, 0)
|
params := make([]interface{}, 0)
|
||||||
|
|
||||||
@ -139,12 +142,13 @@ func (ss *SQLStore) GetAlertNotificationsWithUidToSend(ctx context.Context, quer
|
|||||||
sql.WriteString(`)`)
|
sql.WriteString(`)`)
|
||||||
|
|
||||||
results := make([]*models.AlertNotification, 0)
|
results := make([]*models.AlertNotification, 0)
|
||||||
if err := x.SQL(sql.String(), params...).Find(&results); err != nil {
|
if err := sess.SQL(sql.String(), params...).Find(&results); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
query.Result = results
|
query.Result = results
|
||||||
return nil
|
return nil
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func getAlertNotificationUidInternal(ctx context.Context, query *models.GetAlertNotificationUidQuery, sess *DBSession) error {
|
func getAlertNotificationUidInternal(ctx context.Context, query *models.GetAlertNotificationUidQuery, sess *DBSession) error {
|
||||||
@ -265,7 +269,7 @@ func getAlertNotificationWithUidInternal(ctx context.Context, query *models.GetA
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ss *SQLStore) CreateAlertNotificationCommand(ctx context.Context, cmd *models.CreateAlertNotificationCommand) error {
|
func (ss *SQLStore) CreateAlertNotificationCommand(ctx context.Context, cmd *models.CreateAlertNotificationCommand) error {
|
||||||
return inTransaction(func(sess *DBSession) error {
|
return ss.WithTransactionalDbSession(ctx, func(sess *DBSession) error {
|
||||||
if cmd.Uid == "" {
|
if cmd.Uid == "" {
|
||||||
uid, uidGenerationErr := generateNewAlertNotificationUid(ctx, sess, cmd.OrgId)
|
uid, uidGenerationErr := generateNewAlertNotificationUid(ctx, sess, cmd.OrgId)
|
||||||
if uidGenerationErr != nil {
|
if uidGenerationErr != nil {
|
||||||
@ -355,7 +359,7 @@ func generateNewAlertNotificationUid(ctx context.Context, sess *DBSession, orgId
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ss *SQLStore) UpdateAlertNotification(ctx context.Context, cmd *models.UpdateAlertNotificationCommand) error {
|
func (ss *SQLStore) UpdateAlertNotification(ctx context.Context, cmd *models.UpdateAlertNotificationCommand) error {
|
||||||
return inTransaction(func(sess *DBSession) (err error) {
|
return ss.WithTransactionalDbSession(ctx, func(sess *DBSession) (err error) {
|
||||||
current := models.AlertNotification{}
|
current := models.AlertNotification{}
|
||||||
|
|
||||||
if _, err = sess.ID(cmd.Id).Get(¤t); err != nil {
|
if _, err = sess.ID(cmd.Id).Get(¤t); err != nil {
|
||||||
@ -425,7 +429,7 @@ func (ss *SQLStore) UpdateAlertNotification(ctx context.Context, cmd *models.Upd
|
|||||||
func (ss *SQLStore) UpdateAlertNotificationWithUid(ctx context.Context, cmd *models.UpdateAlertNotificationWithUidCommand) error {
|
func (ss *SQLStore) UpdateAlertNotificationWithUid(ctx context.Context, cmd *models.UpdateAlertNotificationWithUidCommand) error {
|
||||||
getAlertNotificationWithUidQuery := &models.GetAlertNotificationsWithUidQuery{OrgId: cmd.OrgId, Uid: cmd.Uid}
|
getAlertNotificationWithUidQuery := &models.GetAlertNotificationsWithUidQuery{OrgId: cmd.OrgId, Uid: cmd.Uid}
|
||||||
|
|
||||||
if err := getAlertNotificationWithUidInternal(ctx, getAlertNotificationWithUidQuery, newSession(context.Background())); err != nil {
|
if err := getAlertNotificationWithUidInternal(ctx, getAlertNotificationWithUidQuery, newSession(ctx)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,8 +20,7 @@ import (
|
|||||||
func TestAlertNotificationSQLAccess(t *testing.T) {
|
func TestAlertNotificationSQLAccess(t *testing.T) {
|
||||||
var sqlStore *SQLStore
|
var sqlStore *SQLStore
|
||||||
setup := func() {
|
setup := func() {
|
||||||
sqlStore := InitTestDB(t)
|
sqlStore = InitTestDB(t)
|
||||||
|
|
||||||
// Set up bus handlers
|
// Set up bus handlers
|
||||||
bus.AddHandler("deleteAlertNotification", func(ctx context.Context, cmd *models.DeleteAlertNotificationCommand) error {
|
bus.AddHandler("deleteAlertNotification", func(ctx context.Context, cmd *models.DeleteAlertNotificationCommand) error {
|
||||||
return sqlStore.DeleteAlertNotification(ctx, cmd)
|
return sqlStore.DeleteAlertNotification(ctx, cmd)
|
||||||
@ -181,7 +180,6 @@ func TestAlertNotificationSQLAccess(t *testing.T) {
|
|||||||
|
|
||||||
t.Run("invalid frequency", func(t *testing.T) {
|
t.Run("invalid frequency", func(t *testing.T) {
|
||||||
cmd.Frequency = "invalid duration"
|
cmd.Frequency = "invalid duration"
|
||||||
|
|
||||||
err := sqlStore.CreateAlertNotificationCommand(context.Background(), cmd)
|
err := sqlStore.CreateAlertNotificationCommand(context.Background(), cmd)
|
||||||
require.True(t, regexp.MustCompile(`^time: invalid duration "?invalid duration"?$`).MatchString(
|
require.True(t, regexp.MustCompile(`^time: invalid duration "?invalid duration"?$`).MatchString(
|
||||||
err.Error()))
|
err.Error()))
|
||||||
|
Loading…
Reference in New Issue
Block a user