Permalink
Browse files

Delete associated users and tokens when deleting instance

  • Loading branch information...
brycekahle committed Sep 4, 2017
1 parent 7f181fb commit 22dc87656bf71b3a3c4950b484ce37b5ce18aa41
Showing with 21 additions and 3 deletions.
  1. +0 −2 api/instance.go
  2. +21 −1 storage/sql/storage.go
View
@@ -107,8 +107,6 @@ func (a *API) DeleteInstance(w http.ResponseWriter, r *http.Request) error {
return internalServerError("Database error deleting instance").WithInternalError(err)
}
// TODO do we delete everything associated with an instance too?
w.WriteHeader(http.StatusNoContent)
return nil
}
View
@@ -3,6 +3,7 @@ package sql
import (
// this is where we do the connections
"fmt"
"net/url"
// import drivers we might need
@@ -303,7 +304,26 @@ func (conn *Connection) UpdateInstance(instance *models.Instance) error {
}
func (conn *Connection) DeleteInstance(instance *models.Instance) error {
return conn.db.Delete(instance).Error
tx := conn.db.Begin()
delModels := map[string]interface{}{
"user": models.User{},
"refresh token": models.RefreshToken{},
}
for name, dm := range delModels {
if result := tx.Delete(dm, "instance_id = ?", instance.ID); result.Error != nil {
tx.Rollback()
return errors.Wrap(result.Error, fmt.Sprintf("Error deleting %s records", name))
}
}
if result := tx.Delete(instance); result.Error != nil {
tx.Rollback()
return errors.Wrap(result.Error, "Error deleting instance record")
}
return tx.Commit().Error
}
// Dial will connect to that storage engine

0 comments on commit 22dc876

Please sign in to comment.