mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
SQLStore: Remove call to deleted store.NewSession()
(#56025)
This commit is contained in:
parent
6bc09a6390
commit
6099e55e62
@ -278,59 +278,63 @@ func (r *ConflictResolver) MergeConflictingUsers(ctx context.Context) error {
|
|||||||
|
|
||||||
// creating a session for each block of users
|
// creating a session for each block of users
|
||||||
// we want to rollback incase something happens during update / delete
|
// we want to rollback incase something happens during update / delete
|
||||||
sess := r.Store.NewSession(ctx)
|
if err := r.Store.WithDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
||||||
defer sess.Close()
|
err := sess.Begin()
|
||||||
err := sess.Begin()
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("could not open a db session: %w", err)
|
|
||||||
}
|
|
||||||
for _, u := range users {
|
|
||||||
if u.Direction == "+" {
|
|
||||||
id, err := strconv.ParseInt(u.ID, 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("could not convert id in +")
|
|
||||||
}
|
|
||||||
intoUserId = id
|
|
||||||
} else if u.Direction == "-" {
|
|
||||||
id, err := strconv.ParseInt(u.ID, 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("could not convert id in -")
|
|
||||||
}
|
|
||||||
fromUserIds = append(fromUserIds, id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if _, err := sess.ID(intoUserId).Where(sqlstore.NotServiceAccountFilter(r.Store)).Get(&intoUser); err != nil {
|
|
||||||
return fmt.Errorf("could not find intoUser: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, fromUserId := range fromUserIds {
|
|
||||||
var fromUser user.User
|
|
||||||
exists, err := sess.ID(fromUserId).Where(sqlstore.NotServiceAccountFilter(r.Store)).Get(&fromUser)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not find fromUser: %w", err)
|
return fmt.Errorf("could not open a db session: %w", err)
|
||||||
}
|
}
|
||||||
if !exists {
|
for _, u := range users {
|
||||||
fmt.Printf("user with id %d does not exist, skipping\n", fromUserId)
|
if u.Direction == "+" {
|
||||||
|
id, err := strconv.ParseInt(u.ID, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("could not convert id in +")
|
||||||
|
}
|
||||||
|
intoUserId = id
|
||||||
|
} else if u.Direction == "-" {
|
||||||
|
id, err := strconv.ParseInt(u.ID, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("could not convert id in -")
|
||||||
|
}
|
||||||
|
fromUserIds = append(fromUserIds, id)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// // delete the user
|
if _, err := sess.ID(intoUserId).Where(sqlstore.NotServiceAccountFilter(r.Store)).Get(&intoUser); err != nil {
|
||||||
delErr := r.Store.DeleteUserInSession(ctx, sess, &models.DeleteUserCommand{UserId: fromUserId})
|
return fmt.Errorf("could not find intoUser: %w", err)
|
||||||
if delErr != nil {
|
|
||||||
return fmt.Errorf("error during deletion of user: %w", delErr)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
commitErr := sess.Commit()
|
for _, fromUserId := range fromUserIds {
|
||||||
if commitErr != nil {
|
var fromUser user.User
|
||||||
return fmt.Errorf("could not commit operation for useridentification %s: %w", block, commitErr)
|
exists, err := sess.ID(fromUserId).Where(sqlstore.NotServiceAccountFilter(r.Store)).Get(&fromUser)
|
||||||
}
|
if err != nil {
|
||||||
userStore := userimpl.ProvideStore(r.Store, setting.NewCfg())
|
return fmt.Errorf("could not find fromUser: %w", err)
|
||||||
updateMainCommand := &user.UpdateUserCommand{
|
}
|
||||||
UserID: intoUser.ID,
|
if !exists {
|
||||||
Login: strings.ToLower(intoUser.Login),
|
fmt.Printf("user with id %d does not exist, skipping\n", fromUserId)
|
||||||
Email: strings.ToLower(intoUser.Email),
|
}
|
||||||
}
|
// // delete the user
|
||||||
updateErr := userStore.Update(ctx, updateMainCommand)
|
delErr := r.Store.DeleteUserInSession(ctx, sess, &models.DeleteUserCommand{UserId: fromUserId})
|
||||||
if updateErr != nil {
|
if delErr != nil {
|
||||||
return fmt.Errorf("could not update user: %w", updateErr)
|
return fmt.Errorf("error during deletion of user: %w", delErr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
commitErr := sess.Commit()
|
||||||
|
if commitErr != nil {
|
||||||
|
return fmt.Errorf("could not commit operation for useridentification %s: %w", block, commitErr)
|
||||||
|
}
|
||||||
|
userStore := userimpl.ProvideStore(r.Store, setting.NewCfg())
|
||||||
|
updateMainCommand := &user.UpdateUserCommand{
|
||||||
|
UserID: intoUser.ID,
|
||||||
|
Login: strings.ToLower(intoUser.Login),
|
||||||
|
Email: strings.ToLower(intoUser.Email),
|
||||||
|
}
|
||||||
|
updateErr := userStore.Update(ctx, updateMainCommand)
|
||||||
|
if updateErr != nil {
|
||||||
|
return fmt.Errorf("could not update user: %w", updateErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}); err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
Loading…
Reference in New Issue
Block a user