mirror of
https://github.com/fleetdm/fleet
synced 2026-05-23 08:58:41 +00:00
replace soft deleted queries in NewQuery (#1159)
Uses the same strategy as #1002, #1016 to replace queries. Closes #1148
This commit is contained in:
parent
7297a87a74
commit
598b5c7cb8
1 changed files with 39 additions and 20 deletions
|
|
@ -3,8 +3,6 @@ package mysql
|
|||
import (
|
||||
"database/sql"
|
||||
|
||||
"github.com/VividCortex/mysqlerr"
|
||||
"github.com/go-sql-driver/mysql"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/kolide/kolide/server/kolide"
|
||||
"github.com/pkg/errors"
|
||||
|
|
@ -27,26 +25,47 @@ func (d *Datastore) QueryByName(name string) (*kolide.Query, bool, error) {
|
|||
return &query, true, nil
|
||||
}
|
||||
|
||||
// NewQuery creates a Query
|
||||
// NewQuery creates a New Query. If a query with the same name was soft-deleted,
|
||||
// NewQuery will replace the old one.
|
||||
func (d *Datastore) NewQuery(query *kolide.Query) (*kolide.Query, error) {
|
||||
sql := `
|
||||
INSERT INTO queries (
|
||||
name,
|
||||
description,
|
||||
query,
|
||||
saved,
|
||||
author_id
|
||||
) VALUES ( ?, ?, ?, ?, ? )
|
||||
`
|
||||
result, err := d.db.Exec(sql, query.Name, query.Description, query.Query, query.Saved, query.AuthorID)
|
||||
if driverErr, ok := err.(*mysql.MySQLError); ok {
|
||||
if driverErr.Number == mysqlerr.ER_DUP_ENTRY {
|
||||
// TODO: this shouldn't require an ID parameter
|
||||
return nil, alreadyExists("Query", 0)
|
||||
}
|
||||
var (
|
||||
deletedQuery kolide.Query
|
||||
sqlStatement string
|
||||
)
|
||||
err := d.db.Get(&deletedQuery,
|
||||
"SELECT * FROM queries WHERE name = ? AND deleted", query.Name)
|
||||
switch err {
|
||||
case nil:
|
||||
sqlStatement = `
|
||||
REPLACE INTO queries (
|
||||
name,
|
||||
description,
|
||||
query,
|
||||
saved,
|
||||
author_id,
|
||||
deleted
|
||||
) VALUES ( ?, ?, ?, ?, ?, ? )
|
||||
`
|
||||
case sql.ErrNoRows:
|
||||
sqlStatement = `
|
||||
INSERT INTO queries (
|
||||
name,
|
||||
description,
|
||||
query,
|
||||
saved,
|
||||
author_id,
|
||||
deleted
|
||||
) VALUES ( ?, ?, ?, ?, ?, ? )
|
||||
`
|
||||
default:
|
||||
return nil, errors.Wrap(err, "check for existing Query")
|
||||
}
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "inserting new query")
|
||||
deleted := false
|
||||
result, err := d.db.Exec(sqlStatement, query.Name, query.Description, query.Query, query.Saved, query.AuthorID, deleted)
|
||||
if err != nil && isDuplicate(err) {
|
||||
return nil, alreadyExists("Query", deletedQuery.ID)
|
||||
} else if err != nil {
|
||||
return nil, errors.Wrap(err, "creating new Query")
|
||||
}
|
||||
|
||||
id, _ := result.LastInsertId()
|
||||
|
|
|
|||
Loading…
Reference in a new issue