fleet/server/datastore/mysql/scheduled_queries.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

113 lines
2.9 KiB
Go

package mysql
import (
"database/sql"
"github.com/kolide/kolide/server/kolide"
"github.com/pkg/errors"
)
func (d *Datastore) NewScheduledQuery(sq *kolide.ScheduledQuery) (*kolide.ScheduledQuery, error) {
query := `
INSERT INTO scheduled_queries (
pack_id,
query_id,
snapshot,
removed,
` + "`interval`" + `,
platform,
version,
shard
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
`
result, err := d.db.Exec(query, sq.PackID, sq.QueryID, sq.Snapshot, sq.Removed, sq.Interval, sq.Platform, sq.Version, sq.Shard)
if err != nil {
return nil, errors.Wrap(err, "inserting scheduled query")
}
id, _ := result.LastInsertId()
sq.ID = uint(id)
query = `SELECT query, name FROM queries WHERE id = ? LIMIT 1`
metadata := []struct {
Query string
Name string
}{}
err = d.db.Select(&metadata, query, sq.QueryID)
if err != nil && err == sql.ErrNoRows {
return nil, notFound("Query").WithID(sq.QueryID)
} else if err != nil {
return nil, errors.Wrap(err, "select query by ID")
}
if len(metadata) != 1 {
return nil, errors.Wrap(err, "wrong number of results returned from database")
}
sq.Query = metadata[0].Query
sq.Name = metadata[0].Name
return sq, nil
}
func (d *Datastore) SaveScheduledQuery(sq *kolide.ScheduledQuery) (*kolide.ScheduledQuery, error) {
query := `
UPDATE scheduled_queries
SET pack_id = ?, query_id = ?, ` + "`interval`" + ` = ?, snapshot = ?, removed = ?, platform = ?, version = ?, shard = ?
WHERE id = ? AND NOT deleted
`
result, err := d.db.Exec(query, sq.PackID, sq.QueryID, sq.Interval, sq.Snapshot, sq.Removed, sq.Platform, sq.Version, sq.Shard, sq.ID)
if err != nil {
return nil, errors.Wrap(err, "saving a scheduled query")
}
rows, err := result.RowsAffected()
if err != nil {
return nil, errors.Wrap(err, "rows affected saving a scheduled query")
}
if rows == 0 {
return nil, notFound("ScheduledQueries").WithID(sq.ID)
}
return sq, nil
}
func (d *Datastore) DeleteScheduledQuery(id uint) error {
return d.deleteEntity("scheduled_queries", id)
}
func (d *Datastore) ScheduledQuery(id uint) (*kolide.ScheduledQuery, error) {
query := `
SELECT sq.*, q.query, q.name
FROM scheduled_queries sq
JOIN queries q
ON sq.query_id = q.id
WHERE sq.id = ?
AND NOT sq.deleted
`
sq := &kolide.ScheduledQuery{}
if err := d.db.Get(sq, query, id); err != nil {
return nil, errors.Wrap(err, "selecting a scheduled query")
}
return sq, nil
}
func (d *Datastore) ListScheduledQueriesInPack(id uint, opts kolide.ListOptions) ([]*kolide.ScheduledQuery, error) {
query := `
SELECT sq.*, q.query, q.name
FROM scheduled_queries sq
JOIN queries q
ON sq.query_id = q.id
WHERE sq.pack_id = ?
AND NOT sq.deleted
`
query = appendListOptionsToSQL(query, opts)
results := []*kolide.ScheduledQuery{}
if err := d.db.Select(&results, query, id); err != nil {
return nil, errors.Wrap(err, "listing scheduled queries")
}
return results, nil
}