Skip to content

Commit

Permalink
Order statuses in memory, consolidate indexing
Browse files Browse the repository at this point in the history
[:task_name, :status] is used on the index.
[:task_name, :status, created_at: :desc] is used on the show page.

Co-authored-by: Paulo Barros <[email protected]>
  • Loading branch information
etiennebarrie and Paulo Barros committed Jul 21, 2022
1 parent da9f7e8 commit b9c154f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/models/maintenance_tasks/run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Run < ApplicationRecord
serialize :backtrace
serialize :arguments, JSON

scope :active, -> { where(status: ACTIVE_STATUSES).order(status: :asc) }
scope :active, -> { where(status: ACTIVE_STATUSES) }
scope :completed, -> { where(status: COMPLETED_STATUSES) }

# Ensure ActiveStorage is in use before preloading the attachments
Expand Down
2 changes: 1 addition & 1 deletion app/models/maintenance_tasks/task_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def available_tasks
tasks << TaskData.new(task_name, last_run)
end

tasks.sort_by!(&:name)
tasks.sort_by! { |task| [task.name, task.status] }
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

class AddIndexOnTaskNameAndStatusToRuns < ActiveRecord::Migration[6.0]
def change
add_index(:maintenance_tasks_runs, [:task_name, :status],
order: { status: :asc })
remove_index(:maintenance_tasks_runs,
column: [:task_name, :created_at], order: { created_at: :desc },
name: :index_maintenance_tasks_runs_on_task_name_and_created_at)

add_index(:maintenance_tasks_runs, [:task_name, :status, :created_at],
name: :index_maintenance_tasks_runs,
order: { created_at: :desc })
end
end
4 changes: 2 additions & 2 deletions test/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2022_07_13_131925) do

create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
Expand Down Expand Up @@ -56,8 +57,7 @@
t.datetime "updated_at", precision: 6, null: false
t.text "arguments"
t.integer "lock_version", default: 0, null: false
t.index ["task_name", "created_at"], name: "index_maintenance_tasks_runs_on_task_name_and_created_at", order: { created_at: :desc }
t.index ["task_name", "status"], name: "index_maintenance_tasks_runs_on_task_name_and_status"
t.index ["task_name", "status", "created_at"], name: "index_maintenance_tasks_runs", order: { created_at: :desc }
end

create_table "posts", force: :cascade do |t|
Expand Down

0 comments on commit b9c154f

Please sign in to comment.