Skip to content

Commit

Permalink
Make the view optional (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
Calvin Lee authored Feb 27, 2023
1 parent 9aa68a9 commit c771e92
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
28 changes: 18 additions & 10 deletions storage/mysql/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,20 +146,28 @@ UPDATE
}

func (s *MySQLStorage) RetrieveNextCommand(r *mdm.Request, skipNotNow bool) (*mdm.Command, error) {
statusWhere := "status IS NULL"
if !skipNotNow {
statusWhere = `(` + statusWhere + ` OR status = 'NotNow')`
}
command := new(mdm.Command)
err := s.db.QueryRowContext(
r.Context,
`SELECT command_uuid, request_type, command FROM view_queue WHERE id = ? AND active = 1 AND `+statusWhere+` LIMIT 1;`,
r.ID,
r.Context, `
SELECT c.command_uuid, c.request_type, c.command
FROM enrollment_queue AS q
INNER JOIN commands AS c
ON q.command_uuid = c.command_uuid
LEFT JOIN command_results r
ON r.command_uuid = q.command_uuid AND r.id = q.id
WHERE q.id = ?
AND q.active = 1
AND (r.status IS NULL OR (r.status = 'NotNow' AND NOT ?))
ORDER BY
q.priority DESC,
q.created_at
LIMIT 1;`,
r.ID, skipNotNow,
).Scan(&command.CommandUUID, &command.Command.RequestType, &command.Raw)
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
}
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
}
return nil, err
}
return command, nil
Expand Down
1 change: 1 addition & 0 deletions storage/mysql/schema.00009.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE enrollment_queue ADD INDEX (priority DESC, created_at);
2 changes: 2 additions & 0 deletions storage/mysql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ CREATE TABLE enrollment_queue (

PRIMARY KEY (id, command_uuid),

INDEX (priority DESC, created_at),

FOREIGN KEY (id)
REFERENCES enrollments (id)
ON DELETE CASCADE ON UPDATE CASCADE,
Expand Down

0 comments on commit c771e92

Please sign in to comment.