Commit e6d09b32 by Torkel Ödegaard

fix(db): remove stars and tags when removing user or dashboard, fixes #2016

parent 8fcaa499
...@@ -198,11 +198,28 @@ func GetDashboardTags(query *m.GetDashboardTagsQuery) error { ...@@ -198,11 +198,28 @@ func GetDashboardTags(query *m.GetDashboardTagsQuery) error {
} }
func DeleteDashboard(cmd *m.DeleteDashboardCommand) error { func DeleteDashboard(cmd *m.DeleteDashboardCommand) error {
sess := x.NewSession() return inTransaction2(func(sess *session) error {
defer sess.Close() dashboard := m.Dashboard{Slug: cmd.Slug, OrgId: cmd.OrgId}
has, err := x.Get(&dashboard)
if err != nil {
return err
} else if has == false {
return m.ErrDashboardNotFound
}
rawSql := "DELETE FROM dashboard WHERE org_id=? and slug=?" deletes := []string{
_, err := sess.Exec(rawSql, cmd.OrgId, cmd.Slug) "DELETE FROM dashboard_tag WHERE dashboard_id = ? ",
"DELETE FROM star WHERE dashboard_id = ? ",
"DELETE FROM dashboard WHERE id = ?",
}
return err for _, sql := range deletes {
_, err := sess.Exec(sql, dashboard.Id)
if err != nil {
return err
}
}
return nil
})
} }
...@@ -5,7 +5,6 @@ import ( ...@@ -5,7 +5,6 @@ import (
"github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/events" "github.com/grafana/grafana/pkg/events"
"github.com/grafana/grafana/pkg/log"
m "github.com/grafana/grafana/pkg/models" m "github.com/grafana/grafana/pkg/models"
) )
...@@ -123,8 +122,8 @@ func DeleteOrg(cmd *m.DeleteOrgCommand) error { ...@@ -123,8 +122,8 @@ func DeleteOrg(cmd *m.DeleteOrgCommand) error {
return inTransaction2(func(sess *session) error { return inTransaction2(func(sess *session) error {
deletes := []string{ deletes := []string{
"DELETE FROM star WHERE EXISTS (SELECT 1 FROM dashboard WHERE org_id = ?)", "DELETE FROM star WHERE EXISTS (SELECT 1 FROM dashboard WHERE org_id = ? AND star.dashboard_id = dashboard.id)",
"DELETE FROM dashboard_tag WHERE EXISTS (SELECT 1 FROM dashboard WHERE org_id = ?)", "DELETE FROM dashboard_tag WHERE EXISTS (SELECT 1 FROM dashboard WHERE org_id = ? AND dashboard_tag.dashboard_id = dashboard.id)",
"DELETE FROM dashboard WHERE org_id = ?", "DELETE FROM dashboard WHERE org_id = ?",
"DELETE FROM api_key WHERE org_id = ?", "DELETE FROM api_key WHERE org_id = ?",
"DELETE FROM data_source WHERE org_id = ?", "DELETE FROM data_source WHERE org_id = ?",
...@@ -134,7 +133,6 @@ func DeleteOrg(cmd *m.DeleteOrgCommand) error { ...@@ -134,7 +133,6 @@ func DeleteOrg(cmd *m.DeleteOrgCommand) error {
} }
for _, sql := range deletes { for _, sql := range deletes {
log.Trace(sql)
_, err := sess.Exec(sql, cmd.Id) _, err := sess.Exec(sql, cmd.Id)
if err != nil { if err != nil {
return err return err
......
package sqlstore package sqlstore
import ( import (
"fmt"
"strings" "strings"
"time" "time"
...@@ -309,9 +308,19 @@ func SearchUsers(query *m.SearchUsersQuery) error { ...@@ -309,9 +308,19 @@ func SearchUsers(query *m.SearchUsersQuery) error {
func DeleteUser(cmd *m.DeleteUserCommand) error { func DeleteUser(cmd *m.DeleteUserCommand) error {
return inTransaction(func(sess *xorm.Session) error { return inTransaction(func(sess *xorm.Session) error {
var rawSql = fmt.Sprintf("DELETE FROM %s WHERE id=?", x.Dialect().Quote("user")) deletes := []string{
_, err := sess.Exec(rawSql, cmd.UserId) "DELETE FROM star WHERE user_id = ?",
return err "DELETE FROM user WHERE id = ?",
}
for _, sql := range deletes {
_, err := sess.Exec(sql, cmd.UserId)
if err != nil {
return err
}
}
return nil
}) })
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment