fleet/server/datastore/mysql/delete.go
John Murphy f4bee00b01 Fix Issue where saving same option value errs. (#1433)
Closes issue #1390

There were quite a few places where UPDATES could fail silently because we weren't checking target rows where actually found where we expect them to be. In order to address this problem clientFoundRows was set in the sql driver configuration and checks for UPDATES were added to determine if matched rows were found where we expect them to be.
2017-03-30 17:03:48 -05:00

25 lines
494 B
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
}