Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class AddSubjectsCountToCollections < ActiveRecord::Migration[7.2]
Comment thread
yuenmichelle1 marked this conversation as resolved.
disable_ddl_transaction!

def change
add_column :collections, :subjects_count, :integer, default: 0
add_index :collections, :subjects_count, algorithm: :concurrently
end
end
11 changes: 10 additions & 1 deletion db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
);


Expand Down Expand Up @@ -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: -
--
Expand Down Expand Up @@ -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'),
Expand Down
22 changes: 22 additions & 0 deletions lib/tasks/collections.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace :collections do
Comment thread
yuenmichelle1 marked this conversation as resolved.
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
Loading