Merge branch 'master' into alerting_reminder
* master: (95 commits) registry: adds more comments registry: adds comments to interfaces changelog: update changelog: update changelog: add notes about closing #12438 alerting: only log when screenshot been uploaded fixes typos changelog: add notes about closing #12444 Revert "auth proxy: use real ip when validating white listed ip's" changelog: adds note for #11892 changelog: add notes about closing #12430 fix footer css issue Karma to Jest: 3 test files (#12414) fix: log close/flush was done too early, before server shutdown log message was called, fixes #12438 Karma to Jest: value_select_dropdown (#12435) support passing api token in Basic auth password (#12416) Add disabled styles for checked checkbox (#12422) changelog: add notes about closing #11920 changelog: add notes about closing #11920 changelog: update ...
This commit is contained in:
@@ -238,18 +238,19 @@ func (r *SqlAnnotationRepo) Delete(params *annotations.DeleteParams) error {
|
||||
queryParams []interface{}
|
||||
)
|
||||
|
||||
sqlog.Info("delete", "orgId", params.OrgId)
|
||||
if params.RegionId != 0 {
|
||||
annoTagSql = "DELETE FROM annotation_tag WHERE annotation_id IN (SELECT id FROM annotation WHERE region_id = ?)"
|
||||
sql = "DELETE FROM annotation WHERE region_id = ?"
|
||||
queryParams = []interface{}{params.RegionId}
|
||||
annoTagSql = "DELETE FROM annotation_tag WHERE annotation_id IN (SELECT id FROM annotation WHERE region_id = ? AND org_id = ?)"
|
||||
sql = "DELETE FROM annotation WHERE region_id = ? AND org_id = ?"
|
||||
queryParams = []interface{}{params.RegionId, params.OrgId}
|
||||
} else if params.Id != 0 {
|
||||
annoTagSql = "DELETE FROM annotation_tag WHERE annotation_id IN (SELECT id FROM annotation WHERE id = ?)"
|
||||
sql = "DELETE FROM annotation WHERE id = ?"
|
||||
queryParams = []interface{}{params.Id}
|
||||
annoTagSql = "DELETE FROM annotation_tag WHERE annotation_id IN (SELECT id FROM annotation WHERE id = ? AND org_id = ?)"
|
||||
sql = "DELETE FROM annotation WHERE id = ? AND org_id = ?"
|
||||
queryParams = []interface{}{params.Id, params.OrgId}
|
||||
} else {
|
||||
annoTagSql = "DELETE FROM annotation_tag WHERE annotation_id IN (SELECT id FROM annotation WHERE dashboard_id = ? AND panel_id = ?)"
|
||||
sql = "DELETE FROM annotation WHERE dashboard_id = ? AND panel_id = ?"
|
||||
queryParams = []interface{}{params.DashboardId, params.PanelId}
|
||||
annoTagSql = "DELETE FROM annotation_tag WHERE annotation_id IN (SELECT id FROM annotation WHERE dashboard_id = ? AND panel_id = ? AND org_id = ?)"
|
||||
sql = "DELETE FROM annotation WHERE dashboard_id = ? AND panel_id = ? AND org_id = ?"
|
||||
queryParams = []interface{}{params.DashboardId, params.PanelId, params.OrgId}
|
||||
}
|
||||
|
||||
if _, err := sess.Exec(annoTagSql, queryParams...); err != nil {
|
||||
|
||||
@@ -268,7 +268,7 @@ func TestAnnotations(t *testing.T) {
|
||||
|
||||
annotationId := items[0].Id
|
||||
|
||||
err = repo.Delete(&annotations.DeleteParams{Id: annotationId})
|
||||
err = repo.Delete(&annotations.DeleteParams{Id: annotationId, OrgId: 1})
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
items, err = repo.Find(query)
|
||||
|
||||
@@ -27,18 +27,18 @@ func startSession(ctx context.Context, engine *xorm.Engine, beginTran bool) (*DB
|
||||
var sess *DBSession
|
||||
sess, ok := value.(*DBSession)
|
||||
|
||||
if !ok {
|
||||
newSess := &DBSession{Session: engine.NewSession()}
|
||||
if beginTran {
|
||||
err := newSess.Begin()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return newSess, nil
|
||||
if ok {
|
||||
return sess, nil
|
||||
}
|
||||
|
||||
return sess, nil
|
||||
newSess := &DBSession{Session: engine.NewSession()}
|
||||
if beginTran {
|
||||
err := newSess.Begin()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return newSess, nil
|
||||
}
|
||||
|
||||
func withDbSession(ctx context.Context, callback dbTransactionFunc) error {
|
||||
|
||||
@@ -26,7 +26,7 @@ import (
|
||||
|
||||
_ "github.com/grafana/grafana/pkg/tsdb/mssql"
|
||||
_ "github.com/lib/pq"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
sqlite3 "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -56,6 +56,64 @@ type SqlStore struct {
|
||||
skipEnsureAdmin bool
|
||||
}
|
||||
|
||||
// NewSession returns a new DBSession
|
||||
func (ss *SqlStore) NewSession() *DBSession {
|
||||
return &DBSession{Session: ss.engine.NewSession()}
|
||||
}
|
||||
|
||||
// WithDbSession calls the callback with an session attached to the context.
|
||||
func (ss *SqlStore) WithDbSession(ctx context.Context, callback dbTransactionFunc) error {
|
||||
sess, err := startSession(ctx, ss.engine, false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return callback(sess)
|
||||
}
|
||||
|
||||
// WithTransactionalDbSession calls the callback with an session within a transaction
|
||||
func (ss *SqlStore) WithTransactionalDbSession(ctx context.Context, callback dbTransactionFunc) error {
|
||||
return ss.inTransactionWithRetryCtx(ctx, callback, 0)
|
||||
}
|
||||
|
||||
func (ss *SqlStore) inTransactionWithRetryCtx(ctx context.Context, callback dbTransactionFunc, retry int) error {
|
||||
sess, err := startSession(ctx, ss.engine, true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer sess.Close()
|
||||
|
||||
err = callback(sess)
|
||||
|
||||
// special handling of database locked errors for sqlite, then we can retry 3 times
|
||||
if sqlError, ok := err.(sqlite3.Error); ok && retry < 5 {
|
||||
if sqlError.Code == sqlite3.ErrLocked {
|
||||
sess.Rollback()
|
||||
time.Sleep(time.Millisecond * time.Duration(10))
|
||||
sqlog.Info("Database table locked, sleeping then retrying", "retry", retry)
|
||||
return ss.inTransactionWithRetryCtx(ctx, callback, retry+1)
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
log.Error(3, "Failed to publish event after commit", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ss *SqlStore) Init() error {
|
||||
ss.log = log.New("sqlstore")
|
||||
ss.readConfig()
|
||||
|
||||
Reference in New Issue
Block a user