diff --git a/pkg/services/sqlstore/alert.go b/pkg/services/sqlstore/alert.go index 3fccba6267e..bc589f89c14 100644 --- a/pkg/services/sqlstore/alert.go +++ b/pkg/services/sqlstore/alert.go @@ -6,7 +6,6 @@ import ( "strings" "time" - "github.com/go-xorm/xorm" "github.com/grafana/grafana/pkg/bus" m "github.com/grafana/grafana/pkg/models" ) @@ -48,7 +47,7 @@ func GetAllAlertQueryHandler(query *m.GetAllAlertsQuery) error { return nil } -func deleteAlertByIdInternal(alertId int64, reason string, sess *xorm.Session) error { +func deleteAlertByIdInternal(alertId int64, reason string, sess *DBSession) error { sqlog.Debug("Deleting alert", "id", alertId, "reason", reason) if _, err := sess.Exec("DELETE FROM alert WHERE id = ?", alertId); err != nil { @@ -63,7 +62,7 @@ func deleteAlertByIdInternal(alertId int64, reason string, sess *xorm.Session) e } func DeleteAlertById(cmd *m.DeleteAlertCommand) error { - return inTransaction(func(sess *xorm.Session) error { + return inTransaction(func(sess *DBSession) error { return deleteAlertByIdInternal(cmd.AlertId, "DeleteAlertCommand", sess) }) } @@ -123,7 +122,7 @@ func HandleAlertsQuery(query *m.GetAlertsQuery) error { return nil } -func DeleteAlertDefinition(dashboardId int64, sess *xorm.Session) error { +func DeleteAlertDefinition(dashboardId int64, sess *DBSession) error { alerts := make([]*m.Alert, 0) sess.Where("dashboard_id = ?", dashboardId).Find(&alerts) @@ -135,7 +134,7 @@ func DeleteAlertDefinition(dashboardId int64, sess *xorm.Session) error { } func SaveAlerts(cmd *m.SaveAlertsCommand) error { - return inTransaction(func(sess *xorm.Session) error { + return inTransaction(func(sess *DBSession) error { existingAlerts, err := GetAlertsByDashboardId2(cmd.DashboardId, sess) if err != nil { return err @@ -153,7 +152,7 @@ func SaveAlerts(cmd *m.SaveAlertsCommand) error { }) } -func upsertAlerts(existingAlerts []*m.Alert, cmd *m.SaveAlertsCommand, sess *xorm.Session) error { +func upsertAlerts(existingAlerts []*m.Alert, cmd *m.SaveAlertsCommand, sess *DBSession) error { for _, alert := range cmd.Alerts { update := false var alertToUpdate *m.Alert @@ -197,7 +196,7 @@ func upsertAlerts(existingAlerts []*m.Alert, cmd *m.SaveAlertsCommand, sess *xor return nil } -func deleteMissingAlerts(alerts []*m.Alert, cmd *m.SaveAlertsCommand, sess *xorm.Session) error { +func deleteMissingAlerts(alerts []*m.Alert, cmd *m.SaveAlertsCommand, sess *DBSession) error { for _, missingAlert := range alerts { missing := true @@ -216,7 +215,7 @@ func deleteMissingAlerts(alerts []*m.Alert, cmd *m.SaveAlertsCommand, sess *xorm return nil } -func GetAlertsByDashboardId2(dashboardId int64, sess *xorm.Session) ([]*m.Alert, error) { +func GetAlertsByDashboardId2(dashboardId int64, sess *DBSession) ([]*m.Alert, error) { alerts := make([]*m.Alert, 0) err := sess.Where("dashboard_id = ?", dashboardId).Find(&alerts) @@ -228,7 +227,7 @@ func GetAlertsByDashboardId2(dashboardId int64, sess *xorm.Session) ([]*m.Alert, } func SetAlertState(cmd *m.SetAlertStateCommand) error { - return inTransaction(func(sess *xorm.Session) error { + return inTransaction(func(sess *DBSession) error { alert := m.Alert{} if has, err := sess.Id(cmd.AlertId).Get(&alert); err != nil { @@ -262,7 +261,7 @@ func SetAlertState(cmd *m.SetAlertStateCommand) error { } func PauseAlert(cmd *m.PauseAlertCommand) error { - return inTransaction(func(sess *xorm.Session) error { + return inTransaction(func(sess *DBSession) error { if len(cmd.AlertIds) == 0 { return fmt.Errorf("command contains no alertids") } @@ -292,7 +291,7 @@ func PauseAlert(cmd *m.PauseAlertCommand) error { } func PauseAllAlerts(cmd *m.PauseAllAlertCommand) error { - return inTransaction(func(sess *xorm.Session) error { + return inTransaction(func(sess *DBSession) error { var newState string if cmd.Paused { newState = string(m.AlertStatePaused) diff --git a/pkg/services/sqlstore/alert_notification.go b/pkg/services/sqlstore/alert_notification.go index 5acb53c3c09..5e66627f194 100644 --- a/pkg/services/sqlstore/alert_notification.go +++ b/pkg/services/sqlstore/alert_notification.go @@ -6,7 +6,6 @@ import ( "strings" "time" - "github.com/go-xorm/xorm" "github.com/grafana/grafana/pkg/bus" m "github.com/grafana/grafana/pkg/models" ) @@ -21,7 +20,7 @@ func init() { } func DeleteAlertNotification(cmd *m.DeleteAlertNotificationCommand) error { - return inTransaction(func(sess *xorm.Session) error { + return inTransaction(func(sess *DBSession) error { sql := "DELETE FROM alert_notification WHERE alert_notification.org_id = ? AND alert_notification.id = ?" _, err := sess.Exec(sql, cmd.OrgId, cmd.Id) @@ -34,7 +33,7 @@ func DeleteAlertNotification(cmd *m.DeleteAlertNotificationCommand) error { } func GetAlertNotifications(query *m.GetAlertNotificationsQuery) error { - return getAlertNotificationInternal(query, x.NewSession()) + return getAlertNotificationInternal(query, newSession()) } func GetAllAlertNotifications(query *m.GetAllAlertNotificationsQuery) error { @@ -85,7 +84,7 @@ func GetAlertNotificationsToSend(query *m.GetAlertNotificationsToSendQuery) erro return nil } -func getAlertNotificationInternal(query *m.GetAlertNotificationsQuery, sess *xorm.Session) error { +func getAlertNotificationInternal(query *m.GetAlertNotificationsQuery, sess *DBSession) error { var sql bytes.Buffer params := make([]interface{}, 0) @@ -131,7 +130,7 @@ func getAlertNotificationInternal(query *m.GetAlertNotificationsQuery, sess *xor } func CreateAlertNotificationCommand(cmd *m.CreateAlertNotificationCommand) error { - return inTransaction(func(sess *xorm.Session) error { + return inTransaction(func(sess *DBSession) error { existingQuery := &m.GetAlertNotificationsQuery{OrgId: cmd.OrgId, Name: cmd.Name} err := getAlertNotificationInternal(existingQuery, sess) @@ -163,7 +162,7 @@ func CreateAlertNotificationCommand(cmd *m.CreateAlertNotificationCommand) error } func UpdateAlertNotification(cmd *m.UpdateAlertNotificationCommand) error { - return inTransaction(func(sess *xorm.Session) (err error) { + return inTransaction(func(sess *DBSession) (err error) { current := m.AlertNotification{} if _, err = sess.Id(cmd.Id).Get(¤t); err != nil { diff --git a/pkg/services/sqlstore/annotation.go b/pkg/services/sqlstore/annotation.go index 62a10ee2106..ffad5bf2cad 100644 --- a/pkg/services/sqlstore/annotation.go +++ b/pkg/services/sqlstore/annotation.go @@ -5,7 +5,6 @@ import ( "fmt" "strings" - "github.com/go-xorm/xorm" "github.com/grafana/grafana/pkg/services/annotations" ) @@ -13,7 +12,7 @@ type SqlAnnotationRepo struct { } func (r *SqlAnnotationRepo) Save(item *annotations.Item) error { - return inTransaction(func(sess *xorm.Session) error { + return inTransaction(func(sess *DBSession) error { if _, err := sess.Table("annotation").Insert(item); err != nil { return err @@ -24,7 +23,7 @@ func (r *SqlAnnotationRepo) Save(item *annotations.Item) error { } func (r *SqlAnnotationRepo) Update(item *annotations.Item) error { - return inTransaction(func(sess *xorm.Session) error { + return inTransaction(func(sess *DBSession) error { if _, err := sess.Table("annotation").Id(item.Id).Update(item); err != nil { return err @@ -97,7 +96,7 @@ func (r *SqlAnnotationRepo) Find(query *annotations.ItemQuery) ([]*annotations.I } func (r *SqlAnnotationRepo) Delete(params *annotations.DeleteParams) error { - return inTransaction(func(sess *xorm.Session) error { + return inTransaction(func(sess *DBSession) error { sql := "DELETE FROM annotation WHERE dashboard_id = ? AND panel_id = ?" diff --git a/pkg/services/sqlstore/apikey.go b/pkg/services/sqlstore/apikey.go index 1cca7b5e40b..0532f636625 100644 --- a/pkg/services/sqlstore/apikey.go +++ b/pkg/services/sqlstore/apikey.go @@ -3,7 +3,6 @@ package sqlstore import ( "time" - "github.com/go-xorm/xorm" "github.com/grafana/grafana/pkg/bus" m "github.com/grafana/grafana/pkg/models" ) @@ -24,7 +23,7 @@ func GetApiKeys(query *m.GetApiKeysQuery) error { } func DeleteApiKey(cmd *m.DeleteApiKeyCommand) error { - return inTransaction(func(sess *xorm.Session) error { + return inTransaction(func(sess *DBSession) error { var rawSql = "DELETE FROM api_key WHERE id=? and org_id=?" _, err := sess.Exec(rawSql, cmd.Id, cmd.OrgId) return err @@ -32,7 +31,7 @@ func DeleteApiKey(cmd *m.DeleteApiKeyCommand) error { } func AddApiKey(cmd *m.AddApiKeyCommand) error { - return inTransaction(func(sess *xorm.Session) error { + return inTransaction(func(sess *DBSession) error { t := m.ApiKey{ OrgId: cmd.OrgId, Name: cmd.Name, diff --git a/pkg/services/sqlstore/dashboard.go b/pkg/services/sqlstore/dashboard.go index 7bd65ac4da8..d8e9ec34890 100644 --- a/pkg/services/sqlstore/dashboard.go +++ b/pkg/services/sqlstore/dashboard.go @@ -4,7 +4,6 @@ import ( "bytes" "fmt" - "github.com/go-xorm/xorm" "github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/metrics" m "github.com/grafana/grafana/pkg/models" @@ -23,7 +22,7 @@ func init() { } func SaveDashboard(cmd *m.SaveDashboardCommand) error { - return inTransaction(func(sess *xorm.Session) error { + return inTransaction(func(sess *DBSession) error { dash := cmd.GetDashboardModel() // try get existing dashboard @@ -220,7 +219,7 @@ func GetDashboardTags(query *m.GetDashboardTagsQuery) error { } func DeleteDashboard(cmd *m.DeleteDashboardCommand) error { - return inTransaction2(func(sess *session) error { + return inTransaction(func(sess *DBSession) error { dashboard := m.Dashboard{Slug: cmd.Slug, OrgId: cmd.OrgId} has, err := sess.Get(&dashboard) if err != nil { @@ -243,7 +242,7 @@ func DeleteDashboard(cmd *m.DeleteDashboardCommand) error { } } - if err := DeleteAlertDefinition(dashboard.Id, sess.Session); err != nil { + if err := DeleteAlertDefinition(dashboard.Id, sess); err != nil { return nil } diff --git a/pkg/services/sqlstore/dashboard_snapshot.go b/pkg/services/sqlstore/dashboard_snapshot.go index bb3a4f8f57e..810189b3246 100644 --- a/pkg/services/sqlstore/dashboard_snapshot.go +++ b/pkg/services/sqlstore/dashboard_snapshot.go @@ -3,7 +3,6 @@ package sqlstore import ( "time" - "github.com/go-xorm/xorm" "github.com/grafana/grafana/pkg/bus" m "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/setting" @@ -18,7 +17,7 @@ func init() { } func DeleteExpiredSnapshots(cmd *m.DeleteExpiredSnapshotsCommand) error { - return inTransaction(func(sess *xorm.Session) error { + return inTransaction(func(sess *DBSession) error { var expiredCount int64 = 0 if setting.SnapShotRemoveExpired { @@ -36,7 +35,7 @@ func DeleteExpiredSnapshots(cmd *m.DeleteExpiredSnapshotsCommand) error { } func CreateDashboardSnapshot(cmd *m.CreateDashboardSnapshotCommand) error { - return inTransaction(func(sess *xorm.Session) error { + return inTransaction(func(sess *DBSession) error { // never var expires = time.Now().Add(time.Hour * 24 * 365 * 50) @@ -65,7 +64,7 @@ func CreateDashboardSnapshot(cmd *m.CreateDashboardSnapshotCommand) error { } func DeleteDashboardSnapshot(cmd *m.DeleteDashboardSnapshotCommand) error { - return inTransaction(func(sess *xorm.Session) error { + return inTransaction(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/datasource.go b/pkg/services/sqlstore/datasource.go index 625b53469e9..831bead2360 100644 --- a/pkg/services/sqlstore/datasource.go +++ b/pkg/services/sqlstore/datasource.go @@ -6,8 +6,6 @@ import ( "github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/components/securejsondata" m "github.com/grafana/grafana/pkg/models" - - "github.com/go-xorm/xorm" ) func init() { @@ -52,7 +50,7 @@ func GetDataSources(query *m.GetDataSourcesQuery) error { } func DeleteDataSourceById(cmd *m.DeleteDataSourceByIdCommand) error { - return inTransaction(func(sess *xorm.Session) error { + return inTransaction(func(sess *DBSession) error { var rawSql = "DELETE FROM data_source WHERE id=? and org_id=?" _, err := sess.Exec(rawSql, cmd.Id, cmd.OrgId) return err @@ -60,7 +58,7 @@ func DeleteDataSourceById(cmd *m.DeleteDataSourceByIdCommand) error { } func DeleteDataSourceByName(cmd *m.DeleteDataSourceByNameCommand) error { - return inTransaction(func(sess *xorm.Session) error { + return inTransaction(func(sess *DBSession) error { var rawSql = "DELETE FROM data_source WHERE name=? and org_id=?" _, err := sess.Exec(rawSql, cmd.Name, cmd.OrgId) return err @@ -69,7 +67,7 @@ func DeleteDataSourceByName(cmd *m.DeleteDataSourceByNameCommand) error { func AddDataSource(cmd *m.AddDataSourceCommand) error { - return inTransaction(func(sess *xorm.Session) error { + return inTransaction(func(sess *DBSession) error { existing := m.DataSource{OrgId: cmd.OrgId, Name: cmd.Name} has, _ := sess.Get(&existing) @@ -109,7 +107,7 @@ func AddDataSource(cmd *m.AddDataSourceCommand) error { }) } -func updateIsDefaultFlag(ds *m.DataSource, sess *xorm.Session) error { +func updateIsDefaultFlag(ds *m.DataSource, sess *DBSession) error { // Handle is default flag if ds.IsDefault { rawSql := "UPDATE data_source SET is_default=? WHERE org_id=? AND id <> ?" @@ -122,7 +120,7 @@ func updateIsDefaultFlag(ds *m.DataSource, sess *xorm.Session) error { func UpdateDataSource(cmd *m.UpdateDataSourceCommand) error { - return inTransaction(func(sess *xorm.Session) error { + return inTransaction(func(sess *DBSession) error { ds := &m.DataSource{ Id: cmd.Id, OrgId: cmd.OrgId, diff --git a/pkg/services/sqlstore/org.go b/pkg/services/sqlstore/org.go index 919bb6fd026..8931f1cf0f5 100644 --- a/pkg/services/sqlstore/org.go +++ b/pkg/services/sqlstore/org.go @@ -63,7 +63,7 @@ func GetOrgByName(query *m.GetOrgByNameQuery) error { return nil } -func isOrgNameTaken(name string, existingId int64, sess *session) (bool, error) { +func isOrgNameTaken(name string, existingId int64, sess *DBSession) (bool, error) { // check if org name is taken var org m.Org exists, err := sess.Where("name=?", name).Get(&org) @@ -80,7 +80,7 @@ func isOrgNameTaken(name string, existingId int64, sess *session) (bool, error) } func CreateOrg(cmd *m.CreateOrgCommand) error { - return inTransaction2(func(sess *session) error { + return inTransaction(func(sess *DBSession) error { if isNameTaken, err := isOrgNameTaken(cmd.Name, 0, sess); err != nil { return err @@ -120,7 +120,7 @@ func CreateOrg(cmd *m.CreateOrgCommand) error { } func UpdateOrg(cmd *m.UpdateOrgCommand) error { - return inTransaction2(func(sess *session) error { + return inTransaction(func(sess *DBSession) error { if isNameTaken, err := isOrgNameTaken(cmd.Name, cmd.OrgId, sess); err != nil { return err @@ -154,7 +154,7 @@ func UpdateOrg(cmd *m.UpdateOrgCommand) error { } func UpdateOrgAddress(cmd *m.UpdateOrgAddressCommand) error { - return inTransaction2(func(sess *session) error { + return inTransaction(func(sess *DBSession) error { org := m.Org{ Address1: cmd.Address1, Address2: cmd.Address2, @@ -181,7 +181,7 @@ func UpdateOrgAddress(cmd *m.UpdateOrgAddressCommand) error { } func DeleteOrg(cmd *m.DeleteOrgCommand) error { - return inTransaction2(func(sess *session) error { + return inTransaction(func(sess *DBSession) error { if res, err := sess.Query("SELECT 1 from org WHERE id=?", cmd.Id); err != nil { return err } else if len(res) != 1 { diff --git a/pkg/services/sqlstore/org_users.go b/pkg/services/sqlstore/org_users.go index 11ea558b0ce..e1b9dcc1da7 100644 --- a/pkg/services/sqlstore/org_users.go +++ b/pkg/services/sqlstore/org_users.go @@ -4,8 +4,6 @@ import ( "fmt" "time" - "github.com/go-xorm/xorm" - "github.com/grafana/grafana/pkg/bus" m "github.com/grafana/grafana/pkg/models" ) @@ -18,7 +16,7 @@ func init() { } func AddOrgUser(cmd *m.AddOrgUserCommand) error { - return inTransaction(func(sess *xorm.Session) error { + return inTransaction(func(sess *DBSession) error { // check if user exists if res, err := sess.Query("SELECT 1 from org_user WHERE org_id=? and user_id=?", cmd.OrgId, cmd.UserId); err != nil { return err @@ -46,7 +44,7 @@ func AddOrgUser(cmd *m.AddOrgUserCommand) error { } func UpdateOrgUser(cmd *m.UpdateOrgUserCommand) error { - return inTransaction(func(sess *xorm.Session) error { + return inTransaction(func(sess *DBSession) error { var orgUser m.OrgUser exists, err := sess.Where("org_id=? AND user_id=?", cmd.OrgId, cmd.UserId).Get(&orgUser) if err != nil { @@ -81,7 +79,7 @@ func GetOrgUsers(query *m.GetOrgUsersQuery) error { } func RemoveOrgUser(cmd *m.RemoveOrgUserCommand) error { - return inTransaction(func(sess *xorm.Session) error { + return inTransaction(func(sess *DBSession) error { var rawSql = "DELETE FROM org_user WHERE org_id=? and user_id=?" _, err := sess.Exec(rawSql, cmd.OrgId, cmd.UserId) if err != nil { @@ -92,7 +90,7 @@ func RemoveOrgUser(cmd *m.RemoveOrgUserCommand) error { }) } -func validateOneAdminLeftInOrg(orgId int64, sess *xorm.Session) error { +func validateOneAdminLeftInOrg(orgId int64, sess *DBSession) error { // validate that there is an admin user left res, err := sess.Query("SELECT 1 from org_user WHERE org_id=? and role='Admin'", orgId) if err != nil { diff --git a/pkg/services/sqlstore/playlist.go b/pkg/services/sqlstore/playlist.go index 56fae9d3feb..ad9656c5228 100644 --- a/pkg/services/sqlstore/playlist.go +++ b/pkg/services/sqlstore/playlist.go @@ -3,8 +3,6 @@ package sqlstore import ( "fmt" - "github.com/go-xorm/xorm" - "github.com/grafana/grafana/pkg/bus" m "github.com/grafana/grafana/pkg/models" ) @@ -118,7 +116,7 @@ func DeletePlaylist(cmd *m.DeletePlaylistCommand) error { return m.ErrCommandValidationFailed } - return inTransaction(func(sess *xorm.Session) error { + return inTransaction(func(sess *DBSession) error { var rawPlaylistSql = "DELETE FROM playlist WHERE id = ? and org_id = ?" _, err := sess.Exec(rawPlaylistSql, cmd.Id, cmd.OrgId) diff --git a/pkg/services/sqlstore/plugin_setting.go b/pkg/services/sqlstore/plugin_setting.go index f52d65cdc94..172995872eb 100644 --- a/pkg/services/sqlstore/plugin_setting.go +++ b/pkg/services/sqlstore/plugin_setting.go @@ -44,7 +44,7 @@ func GetPluginSettingById(query *m.GetPluginSettingByIdQuery) error { } func UpdatePluginSetting(cmd *m.UpdatePluginSettingCmd) error { - return inTransaction2(func(sess *session) error { + return inTransaction(func(sess *DBSession) error { var pluginSetting m.PluginSetting exists, err := sess.Where("org_id=? and plugin_id=?", cmd.OrgId, cmd.PluginId).Get(&pluginSetting) @@ -104,7 +104,7 @@ func UpdatePluginSetting(cmd *m.UpdatePluginSettingCmd) error { } func UpdatePluginSettingVersion(cmd *m.UpdatePluginSettingVersionCmd) error { - return inTransaction2(func(sess *session) error { + return inTransaction(func(sess *DBSession) error { _, err := sess.Exec("UPDATE plugin_setting SET plugin_version=? WHERE org_id=? AND plugin_id=?", cmd.PluginVersion, cmd.OrgId, cmd.PluginId) return err diff --git a/pkg/services/sqlstore/preferences.go b/pkg/services/sqlstore/preferences.go index 65609a9c57c..399b23f3ffa 100644 --- a/pkg/services/sqlstore/preferences.go +++ b/pkg/services/sqlstore/preferences.go @@ -68,7 +68,7 @@ func GetPreferences(query *m.GetPreferencesQuery) error { } func SavePreferences(cmd *m.SavePreferencesCommand) error { - return inTransaction2(func(sess *session) error { + return inTransaction(func(sess *DBSession) error { var prefs m.Preferences exists, err := sess.Where("org_id=? AND user_id=?", cmd.OrgId, cmd.UserId).Get(&prefs) diff --git a/pkg/services/sqlstore/quota.go b/pkg/services/sqlstore/quota.go index 53ea8889c56..0a857efce40 100644 --- a/pkg/services/sqlstore/quota.go +++ b/pkg/services/sqlstore/quota.go @@ -2,6 +2,7 @@ package sqlstore import ( "fmt" + "github.com/grafana/grafana/pkg/bus" m "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/setting" @@ -94,7 +95,7 @@ func GetOrgQuotas(query *m.GetOrgQuotasQuery) error { } func UpdateOrgQuota(cmd *m.UpdateOrgQuotaCmd) error { - return inTransaction2(func(sess *session) error { + return inTransaction(func(sess *DBSession) error { //Check if quota is already defined in the DB quota := m.Quota{ Target: cmd.Target, @@ -194,7 +195,7 @@ func GetUserQuotas(query *m.GetUserQuotasQuery) error { } func UpdateUserQuota(cmd *m.UpdateUserQuotaCmd) error { - return inTransaction2(func(sess *session) error { + return inTransaction(func(sess *DBSession) error { //Check if quota is already defined in the DB quota := m.Quota{ Target: cmd.Target, diff --git a/pkg/services/sqlstore/shared.go b/pkg/services/sqlstore/shared.go index 72566a93583..0f4aeb969c6 100644 --- a/pkg/services/sqlstore/shared.go +++ b/pkg/services/sqlstore/shared.go @@ -9,18 +9,21 @@ import ( sqlite3 "github.com/mattn/go-sqlite3" ) -type dbTransactionFunc func(sess *xorm.Session) error -type dbTransactionFunc2 func(sess *session) error - -type session struct { +type DBSession struct { *xorm.Session events []interface{} } -func (sess *session) publishAfterCommit(msg interface{}) { +type dbTransactionFunc func(sess *DBSession) error + +func (sess *DBSession) publishAfterCommit(msg interface{}) { sess.events = append(sess.events, msg) } +func newSession() *DBSession { + return &DBSession{Session: x.NewSession()} +} + func inTransaction(callback dbTransactionFunc) error { return inTransactionWithRetry(callback, 0) } @@ -28,7 +31,7 @@ func inTransaction(callback dbTransactionFunc) error { func inTransactionWithRetry(callback dbTransactionFunc, retry int) error { var err error - sess := x.NewSession() + sess := newSession() defer sess.Close() if err = sess.Begin(); err != nil { @@ -54,28 +57,6 @@ func inTransactionWithRetry(callback dbTransactionFunc, retry int) error { return err } - return nil -} - -func inTransaction2(callback dbTransactionFunc2) error { - var err error - - sess := session{Session: x.NewSession()} - - defer sess.Close() - if err = sess.Begin(); err != nil { - return err - } - - err = callback(&sess) - - if err != nil { - sess.Rollback() - return err - } else if err = sess.Commit(); err != nil { - return err - } - if len(sess.events) > 0 { for _, e := range sess.events { if err = bus.Publish(e); err != nil { diff --git a/pkg/services/sqlstore/sql_test_data.go b/pkg/services/sqlstore/sql_test_data.go index ffb3f0fc997..37740dc8373 100644 --- a/pkg/services/sqlstore/sql_test_data.go +++ b/pkg/services/sqlstore/sql_test_data.go @@ -12,7 +12,7 @@ func init() { bus.AddHandler("sql", InsertSqlTestData) } -func sqlRandomWalk(m1 string, m2 string, intWalker int64, floatWalker float64, sess *session) error { +func sqlRandomWalk(m1 string, m2 string, intWalker int64, floatWalker float64, sess *DBSession) error { timeWalker := time.Now().UTC().Add(time.Hour * -200) now := time.Now().UTC() @@ -45,7 +45,7 @@ func sqlRandomWalk(m1 string, m2 string, intWalker int64, floatWalker float64, s } func InsertSqlTestData(cmd *m.InsertSqlTestDataCommand) error { - return inTransaction2(func(sess *session) error { + return inTransaction(func(sess *DBSession) error { var err error sqlog.Info("SQL TestData: Clearing previous test data") diff --git a/pkg/services/sqlstore/star.go b/pkg/services/sqlstore/star.go index 09dcb8ed939..7c136c04fbc 100644 --- a/pkg/services/sqlstore/star.go +++ b/pkg/services/sqlstore/star.go @@ -1,8 +1,6 @@ package sqlstore import ( - "github.com/go-xorm/xorm" - "github.com/grafana/grafana/pkg/bus" m "github.com/grafana/grafana/pkg/models" ) @@ -36,7 +34,7 @@ func StarDashboard(cmd *m.StarDashboardCommand) error { return m.ErrCommandValidationFailed } - return inTransaction(func(sess *xorm.Session) error { + return inTransaction(func(sess *DBSession) error { entity := m.Star{ UserId: cmd.UserId, @@ -53,7 +51,7 @@ func UnstarDashboard(cmd *m.UnstarDashboardCommand) error { return m.ErrCommandValidationFailed } - return inTransaction(func(sess *xorm.Session) error { + return inTransaction(func(sess *DBSession) error { var rawSql = "DELETE FROM star WHERE user_id=? and dashboard_id=?" _, err := sess.Exec(rawSql, cmd.UserId, cmd.DashboardId) return err diff --git a/pkg/services/sqlstore/temp_user.go b/pkg/services/sqlstore/temp_user.go index 0fe5c9612f5..8864d5fb02a 100644 --- a/pkg/services/sqlstore/temp_user.go +++ b/pkg/services/sqlstore/temp_user.go @@ -3,7 +3,6 @@ package sqlstore import ( "time" - "github.com/go-xorm/xorm" "github.com/grafana/grafana/pkg/bus" m "github.com/grafana/grafana/pkg/models" ) @@ -16,7 +15,7 @@ func init() { } func UpdateTempUserStatus(cmd *m.UpdateTempUserStatusCommand) error { - return inTransaction(func(sess *xorm.Session) error { + return inTransaction(func(sess *DBSession) error { var rawSql = "UPDATE temp_user SET status=? WHERE code=?" _, err := sess.Exec(rawSql, string(cmd.Status), cmd.Code) return err @@ -24,7 +23,7 @@ func UpdateTempUserStatus(cmd *m.UpdateTempUserStatusCommand) error { } func CreateTempUser(cmd *m.CreateTempUserCommand) error { - return inTransaction2(func(sess *session) error { + return inTransaction(func(sess *DBSession) error { // create user user := &m.TempUser{ diff --git a/pkg/services/sqlstore/user.go b/pkg/services/sqlstore/user.go index 71b29bd3355..177f465dc96 100644 --- a/pkg/services/sqlstore/user.go +++ b/pkg/services/sqlstore/user.go @@ -5,8 +5,6 @@ import ( "strings" "time" - "github.com/go-xorm/xorm" - "fmt" "github.com/grafana/grafana/pkg/bus" @@ -34,7 +32,7 @@ func init() { bus.AddHandler("sql", SetUserHelpFlag) } -func getOrgIdForNewUser(cmd *m.CreateUserCommand, sess *session) (int64, error) { +func getOrgIdForNewUser(cmd *m.CreateUserCommand, sess *DBSession) (int64, error) { if cmd.SkipOrgSetup { return -1, nil } @@ -77,7 +75,7 @@ func getOrgIdForNewUser(cmd *m.CreateUserCommand, sess *session) (int64, error) } func CreateUser(cmd *m.CreateUserCommand) error { - return inTransaction2(func(sess *session) error { + return inTransaction(func(sess *DBSession) error { orgId, err := getOrgIdForNewUser(cmd, sess) if err != nil { return err @@ -220,7 +218,7 @@ func GetUserByEmail(query *m.GetUserByEmailQuery) error { } func UpdateUser(cmd *m.UpdateUserCommand) error { - return inTransaction2(func(sess *session) error { + return inTransaction(func(sess *DBSession) error { user := m.User{ Name: cmd.Name, @@ -247,7 +245,7 @@ func UpdateUser(cmd *m.UpdateUserCommand) error { } func ChangeUserPassword(cmd *m.ChangeUserPasswordCommand) error { - return inTransaction2(func(sess *session) error { + return inTransaction(func(sess *DBSession) error { user := m.User{ Password: cmd.NewPassword, @@ -277,7 +275,7 @@ func SetUsingOrg(cmd *m.SetUsingOrgCommand) error { return fmt.Errorf("user does not belong to org") } - return inTransaction(func(sess *xorm.Session) error { + return inTransaction(func(sess *DBSession) error { user := m.User{} sess.Id(cmd.UserId).Get(&user) @@ -394,7 +392,7 @@ func SearchUsers(query *m.SearchUsersQuery) error { } func DeleteUser(cmd *m.DeleteUserCommand) error { - return inTransaction(func(sess *xorm.Session) error { + return inTransaction(func(sess *DBSession) error { deletes := []string{ "DELETE FROM star WHERE user_id = ?", "DELETE FROM " + dialect.Quote("user") + " WHERE id = ?", @@ -412,7 +410,7 @@ func DeleteUser(cmd *m.DeleteUserCommand) error { } func UpdateUserPermissions(cmd *m.UpdateUserPermissionsCommand) error { - return inTransaction(func(sess *xorm.Session) error { + return inTransaction(func(sess *DBSession) error { user := m.User{} sess.Id(cmd.UserId).Get(&user) @@ -424,7 +422,7 @@ func UpdateUserPermissions(cmd *m.UpdateUserPermissionsCommand) error { } func SetUserHelpFlag(cmd *m.SetUserHelpFlagCommand) error { - return inTransaction2(func(sess *session) error { + return inTransaction(func(sess *DBSession) error { user := m.User{ Id: cmd.UserId,