mirror of
https://github.com/empayre/fleet.git
synced 2024-11-07 01:15:22 +00:00
0b7747bef0
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
44 lines
1.1 KiB
Go
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
|
|
}
|