From 8ada81e5c315bffa99c38ab55d97043cf6456ec7 Mon Sep 17 00:00:00 2001 From: yuenmichelle1 Date: Wed, 20 May 2026 14:34:04 -0500 Subject: [PATCH 1/5] create subjects_count counter_cache column on collections and index --- ...93213_add_subjects_count_to_collections.rb | 8 +++++++ lib/tasks/collections.rake | 22 +++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 db/migrate/20260520193213_add_subjects_count_to_collections.rb create mode 100644 lib/tasks/collections.rake diff --git a/db/migrate/20260520193213_add_subjects_count_to_collections.rb b/db/migrate/20260520193213_add_subjects_count_to_collections.rb new file mode 100644 index 000000000..e827bdf90 --- /dev/null +++ b/db/migrate/20260520193213_add_subjects_count_to_collections.rb @@ -0,0 +1,8 @@ +class AddSubjectsCountToCollections < ActiveRecord::Migration[7.2] + disable_ddl_transaction! + + def change + add_column :collections, :subjects_count, :integer, default: 0 + add_index :collections, :subjects_count, algorithm: :concurrently + end +end diff --git a/lib/tasks/collections.rake b/lib/tasks/collections.rake new file mode 100644 index 000000000..4bac703c8 --- /dev/null +++ b/lib/tasks/collections.rake @@ -0,0 +1,22 @@ +namespace :collections do + desc "Backfill subjects_count on collections" + task backfill_subjects_count: :environment do + Collection.in_batches(of: 1_000) do |batch| + collection_ids = batch.pluck(:id) + + counts = CollectionSubject + .where(collection_id: collection_ids) + .group(:collection_id) + .count + + collections_to_update = collection_ids.map do |id| + [id, counts[id] || 0] + end + + collections_to_update.each do |id, count| + Collection.where(id: id) + .update_all(subjects_count: count) + end + end + end +end \ No newline at end of file From 785ca57f18c38f58eb14bafa8890c8b043a673c1 Mon Sep 17 00:00:00 2001 From: yuenmichelle1 Date: Wed, 20 May 2026 14:39:02 -0500 Subject: [PATCH 2/5] Update structure.sql --- db/structure.sql | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/db/structure.sql b/db/structure.sql index ade9f7e0f..217a2bcf2 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -234,7 +234,8 @@ CREATE TABLE public.collections ( slug character varying DEFAULT ''::character varying, favorite boolean DEFAULT false NOT NULL, default_subject_id integer, - description text DEFAULT ''::text + description text DEFAULT ''::text, + subjects_count integer DEFAULT 0 ); @@ -2823,6 +2824,13 @@ CREATE INDEX index_collections_on_private ON public.collections USING btree (pri CREATE INDEX index_collections_on_slug ON public.collections USING btree (slug); +-- +-- Name: index_collections_on_subjects_count; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_collections_on_subjects_count ON public.collections USING btree (subjects_count); + + -- -- Name: index_collections_projects_on_collection_id; Type: INDEX; Schema: public; Owner: - -- @@ -4240,6 +4248,7 @@ ALTER TABLE ONLY public.users SET search_path TO "$user", public; INSERT INTO "schema_migrations" (version) VALUES +('20260520193213'), ('20260323120200'), ('20260323120100'), ('20260323120000'), From 4a14f943218ec5ee29e67a52b0ab187952445f5c Mon Sep 17 00:00:00 2001 From: yuenmichelle1 Date: Wed, 20 May 2026 14:58:07 -0500 Subject: [PATCH 3/5] hound sniffs use single quote and newline --- lib/tasks/collections.rake | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/tasks/collections.rake b/lib/tasks/collections.rake index 4bac703c8..2156e1975 100644 --- a/lib/tasks/collections.rake +++ b/lib/tasks/collections.rake @@ -1,5 +1,5 @@ namespace :collections do - desc "Backfill subjects_count on collections" + desc 'Backfill subjects_count on collections' task backfill_subjects_count: :environment do Collection.in_batches(of: 1_000) do |batch| collection_ids = batch.pluck(:id) @@ -19,4 +19,4 @@ namespace :collections do end end end -end \ No newline at end of file +end From c3fc4d378b7b5c478e2ab7431e35868dd443dc4d Mon Sep 17 00:00:00 2001 From: yuenmichelle1 Date: Tue, 2 Jun 2026 10:00:33 -0500 Subject: [PATCH 4/5] hound sniffs align methods --- lib/tasks/collections.rake | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/tasks/collections.rake b/lib/tasks/collections.rake index 2156e1975..53b5e9e1a 100644 --- a/lib/tasks/collections.rake +++ b/lib/tasks/collections.rake @@ -5,9 +5,9 @@ namespace :collections do collection_ids = batch.pluck(:id) counts = CollectionSubject - .where(collection_id: collection_ids) - .group(:collection_id) - .count + .where(collection_id: collection_ids) + .group(:collection_id) + .count collections_to_update = collection_ids.map do |id| [id, counts[id] || 0] From 7e13f0e19116a69cedf3ff17a0fda8755ae6234f Mon Sep 17 00:00:00 2001 From: yuenmichelle1 Date: Tue, 2 Jun 2026 10:02:29 -0500 Subject: [PATCH 5/5] Update collections.rake --- lib/tasks/collections.rake | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/tasks/collections.rake b/lib/tasks/collections.rake index 53b5e9e1a..dbc10a1b7 100644 --- a/lib/tasks/collections.rake +++ b/lib/tasks/collections.rake @@ -5,9 +5,9 @@ namespace :collections do collection_ids = batch.pluck(:id) counts = CollectionSubject - .where(collection_id: collection_ids) - .group(:collection_id) - .count + .where(collection_id: collection_ids) + .group(:collection_id) + .count collections_to_update = collection_ids.map do |id| [id, counts[id] || 0]