From f437aecf00eb60962081ab47fa068038951f7a97 Mon Sep 17 00:00:00 2001 From: Jorge Manrubia Date: Fri, 4 Apr 2025 15:34:18 +0200 Subject: [PATCH] Sort completed bubbles by most recently completed first --- app/controllers/bubbles_controller.rb | 2 +- app/models/bubble/poppable.rb | 1 + ...33310_add_index_to_sort_pops_by_recency.rb | 5 ++ db/schema.rb | 5 +- db/schema_cache.yml | 71 ++++++++++++------- 5 files changed, 54 insertions(+), 30 deletions(-) create mode 100644 db/migrate/20250404133310_add_index_to_sort_pops_by_recency.rb diff --git a/app/controllers/bubbles_controller.rb b/app/controllers/bubbles_controller.rb index 49a86bc13..3033ca473 100644 --- a/app/controllers/bubbles_controller.rb +++ b/app/controllers/bubbles_controller.rb @@ -14,7 +14,7 @@ class BubblesController < ApplicationController def index @considering_bubbles = @filter.bubbles.considering.load_async @doing_bubbles = @filter.bubbles.doing.load_async - @popped_bubbles = @filter.with(indexed_by: "popped").bubbles.limit(RECENTLY_POPPED_LIMIT).load_async + @popped_bubbles = @filter.with(indexed_by: "popped").bubbles.recently_popped_first.limit(RECENTLY_POPPED_LIMIT).load_async end def create diff --git a/app/models/bubble/poppable.rb b/app/models/bubble/poppable.rb index 3e97943c2..098bda924 100644 --- a/app/models/bubble/poppable.rb +++ b/app/models/bubble/poppable.rb @@ -9,6 +9,7 @@ module Bubble::Poppable scope :popped, -> { joins(:pop) } scope :active, -> { where.missing(:pop) } + scope :recently_popped_first, -> { popped.order("pops.created_at": :desc) } scope :due_to_be_popped, -> { considering.where(last_active_at: ..AUTO_POP_AFTER.ago) } end diff --git a/db/migrate/20250404133310_add_index_to_sort_pops_by_recency.rb b/db/migrate/20250404133310_add_index_to_sort_pops_by_recency.rb new file mode 100644 index 000000000..c3c9f0dc0 --- /dev/null +++ b/db/migrate/20250404133310_add_index_to_sort_pops_by_recency.rb @@ -0,0 +1,5 @@ +class AddIndexToSortPopsByRecency < ActiveRecord::Migration[8.1] + def change + add_index :pops, %i[ bubble_id created_at ] + end +end diff --git a/db/schema.rb b/db/schema.rb index 41314f524..69418c7d5 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.1].define(version: 2025_04_04_083727) do +ActiveRecord::Schema[8.1].define(version: 2025_04_04_133310) do create_table "accesses", force: :cascade do |t| t.integer "bucket_id", null: false t.datetime "created_at", null: false @@ -113,7 +113,7 @@ ActiveRecord::Schema[8.1].define(version: 2025_04_04_083727) do t.datetime "created_at", null: false t.integer "creator_id", null: false t.date "due_on" - t.datetime "last_active_at", null: false + t.datetime "last_active_at" t.integer "stage_id" t.text "status", default: "creating", null: false t.string "title" @@ -249,6 +249,7 @@ ActiveRecord::Schema[8.1].define(version: 2025_04_04_083727) do t.string "reason", null: false t.datetime "updated_at", null: false t.integer "user_id" + t.index ["bubble_id", "created_at"], name: "index_pops_on_bubble_id_and_created_at" t.index ["bubble_id"], name: "index_pops_on_bubble_id", unique: true t.index ["user_id"], name: "index_pops_on_user_id" end diff --git a/db/schema_cache.yml b/db/schema_cache.yml index d29ed5494..6e2c00371 100644 --- a/db/schema_cache.yml +++ b/db/schema_cache.yml @@ -468,6 +468,16 @@ columns: default_function: collation: comment: + - !ruby/object:ActiveRecord::ConnectionAdapters::SQLite3::Column + auto_increment: + name: last_active_at + cast_type: *3 + sql_type_metadata: *4 + 'null': true + default: + default_function: + collation: + comment: - !ruby/object:ActiveRecord::ConnectionAdapters::SQLite3::Column auto_increment: name: stage_id @@ -499,16 +509,6 @@ columns: collation: comment: - *9 - - !ruby/object:ActiveRecord::ConnectionAdapters::SQLite3::Column - auto_increment: - name: last_active_at - cast_type: *3 - sql_type_metadata: *4 - 'null': false - default: - default_function: - collation: - comment: buckets: - *5 - &32 !ruby/object:ActiveRecord::ConnectionAdapters::SQLite3::Column @@ -760,6 +760,7 @@ columns: default_function: collation: comment: + - *6 - !ruby/object:ActiveRecord::ConnectionAdapters::SQLite3::Column auto_increment: name: label @@ -770,12 +771,21 @@ columns: default_function: collation: comment: - - *6 - *9 pops: - *5 - *25 - *6 + - !ruby/object:ActiveRecord::ConnectionAdapters::SQLite3::Column + auto_increment: + name: reason + cast_type: *7 + sql_type_metadata: *8 + 'null': false + default: + default_function: + collation: + comment: - *9 - !ruby/object:ActiveRecord::ConnectionAdapters::SQLite3::Column auto_increment: @@ -787,16 +797,6 @@ columns: default_function: collation: comment: - - !ruby/object:ActiveRecord::ConnectionAdapters::SQLite3::Column - auto_increment: - name: reason - cast_type: *7 - sql_type_metadata: *8 - 'null': false - default: - default_function: - collation: - comment: reactions: - *5 - !ruby/object:ActiveRecord::ConnectionAdapters::SQLite3::Column @@ -1354,11 +1354,10 @@ indexes: bubbles: - !ruby/object:ActiveRecord::ConnectionAdapters::IndexDefinition table: bubbles - name: index_bubbles_on_last_active_at_and_status + name: index_bubbles_on_stage_id unique: false columns: - - last_active_at - - status + - stage_id lengths: {} orders: {} opclasses: {} @@ -1371,10 +1370,11 @@ indexes: valid: true - !ruby/object:ActiveRecord::ConnectionAdapters::IndexDefinition table: bubbles - name: index_bubbles_on_stage_id + name: index_bubbles_on_last_active_at_and_status unique: false columns: - - stage_id + - last_active_at + - status lengths: {} orders: {} opclasses: {} @@ -1856,6 +1856,23 @@ indexes: comment: valid: true pops: + - !ruby/object:ActiveRecord::ConnectionAdapters::IndexDefinition + table: pops + name: index_pops_on_bubble_id_and_created_at + unique: false + columns: + - bubble_id + - created_at + lengths: {} + orders: {} + opclasses: {} + where: + type: + using: + include: + nulls_not_distinct: + comment: + valid: true - !ruby/object:ActiveRecord::ConnectionAdapters::IndexDefinition table: pops name: index_pops_on_user_id @@ -2106,4 +2123,4 @@ indexes: nulls_not_distinct: comment: valid: true -version: 20250404083727 +version: 20250404133310