Skip to content

Commit

Permalink
Delete associated users and tokens when deleting instance
Browse files Browse the repository at this point in the history
  • Loading branch information
brycekahle committed Sep 4, 2017
1 parent 7f181fb commit 22dc876
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
2 changes: 0 additions & 2 deletions api/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
22 changes: 21 additions & 1 deletion storage/sql/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sql
import (
// this is where we do the connections

"fmt"
"net/url"

// import drivers we might need
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 22dc876

Please sign in to comment.