Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove view from mysql backend #59

Merged
merged 1 commit into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions storage/mysql/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,20 +138,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 ?))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clever! I hadn't thought about using a bool value as a query param.

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