fleet/server/datastore/mysql/delete.go
Zachary Wasserman 0b7747bef0
Fix pack and query UI issues in Fleet 2.0 (#1829)
Replaces (and appropriately refactors) a number of endpoints that were removed long ago when we decided to kill the UI with the fleetctl release. We turned out not to do this, and now need to restore these missing endpoints.

This is not a straight up replacement of the existing code because of refactoring to the DB schemas that was also done in the migration.

Most of the replaced code was removed in #1670 and #1686.

Fixes #1811, fixes #1810
2018-06-15 10:13:11 -04:00

44 lines
1.1 KiB
Go

package mysql
import (
"fmt"
"github.com/pkg/errors"
)
func (d *Datastore) deleteEntity(dbTable string, id uint) error {
deleteStmt := fmt.Sprintf(
`
UPDATE %s SET deleted_at = ?, deleted = TRUE
WHERE id = ? AND NOT deleted
`, dbTable)
result, err := d.db.Exec(deleteStmt, d.clock.Now(), id)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("delete %s", dbTable))
}
rows, _ := result.RowsAffected()
if rows != 1 {
return notFound(dbTable).WithID(id)
}
return nil
}
// deleteEntityByName hard deletes an entity with the given name from the given
// DB table, returning a notFound error if appropriate.
// Note: deleteEntity uses soft deletion, but we are moving off this pattern.
func (d *Datastore) deleteEntityByName(dbTable string, name string) error {
deleteStmt := fmt.Sprintf("DELETE FROM %s WHERE name = ?", dbTable)
result, err := d.db.Exec(deleteStmt, name)
if err != nil {
if isMySQLForeignKey(err) {
return foreignKey(dbTable, name)
}
return errors.Wrap(err, fmt.Sprintf("delete %s", dbTable))
}
rows, _ := result.RowsAffected()
if rows != 1 {
return notFound(dbTable).WithName(name)
}
return nil
}