diff --git a/db/migrate/20180608205049_add_index_to_supporter_id_on_tickets.rb b/db/migrate/20180608205049_add_index_to_supporter_id_on_tickets.rb new file mode 100644 index 00000000..d2dc5bf2 --- /dev/null +++ b/db/migrate/20180608205049_add_index_to_supporter_id_on_tickets.rb @@ -0,0 +1,5 @@ +class AddIndexToSupporterIdOnTickets < ActiveRecord::Migration + def change + add_index :tickets, :supporter_id + end +end diff --git a/db/migrate/20180608212658_add_index_to_event_id_on_donations_and_events.rb b/db/migrate/20180608212658_add_index_to_event_id_on_donations_and_events.rb new file mode 100644 index 00000000..c86222f9 --- /dev/null +++ b/db/migrate/20180608212658_add_index_to_event_id_on_donations_and_events.rb @@ -0,0 +1,6 @@ +class AddIndexToEventIdOnDonationsAndEvents < ActiveRecord::Migration + def change + add_index :tickets, :event_id + add_index :donations, :event_id + end +end diff --git a/db/structure.sql b/db/structure.sql index dd3574f3..c1525056 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -3141,6 +3141,13 @@ CREATE INDEX index_cards_on_id_and_holder_type_and_holder_id_and_inactive ON pub CREATE INDEX index_charges_on_payment_id ON public.charges USING btree (payment_id); +-- +-- Name: index_donations_on_event_id; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_donations_on_event_id ON public.donations USING btree (event_id); + + -- -- Name: index_exports_on_nonprofit_id; Type: INDEX; Schema: public; Owner: - -- @@ -3218,6 +3225,20 @@ CREATE INDEX index_supporters_on_import_id ON public.supporters USING btree (imp CREATE INDEX index_supporters_on_name ON public.supporters USING btree (name); +-- +-- Name: index_tickets_on_event_id; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_tickets_on_event_id ON public.tickets USING btree (event_id); + + +-- +-- Name: index_tickets_on_supporter_id; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_tickets_on_supporter_id ON public.tickets USING btree (supporter_id); + + -- -- Name: index_users_on_confirmation_token; Type: INDEX; Schema: public; Owner: - -- @@ -4275,3 +4296,7 @@ INSERT INTO schema_migrations (version) VALUES ('20180216124311'); INSERT INTO schema_migrations (version) VALUES ('20180217124311'); +INSERT INTO schema_migrations (version) VALUES ('20180608205049'); + +INSERT INTO schema_migrations (version) VALUES ('20180608212658'); + diff --git a/lib/query/query_supporters.rb b/lib/query/query_supporters.rb index 9b89bf6a..26e4a4e8 100644 --- a/lib/query/query_supporters.rb +++ b/lib/query/query_supporters.rb @@ -245,16 +245,35 @@ module QuerySupporters if query[:campaign_id].present? expr = expr.add_join("donations", "donations.supporter_id=supporters.id AND donations.campaign_id=#{query[:campaign_id].to_i}") end + if query[:event_id].present? + select_tickets_supporters = Qx.select("event_ticket_supporters.supporter_id") + .from( + "#{Qx.select("MAX(tickets.event_id) AS event_id", "tickets.supporter_id") + .from(:tickets) + .where("event_id = $event_id", event_id: query[:event_id]) + .group_by(:supporter_id).as('event_ticket_supporters').parse}" + ) + + select_donation_supporters = + Qx.select("event_donation_supporters.supporter_id") + .from( + "#{Qx.select("MAX(donations.event_id) AS event_id", "donations.supporter_id") + .from(:donations) + .where("event_id = $event_id", event_id: query[:event_id] ) + .group_by(:supporter_id).as('event_donation_supporters').parse}") + + union_expr = "( +#{select_tickets_supporters.parse} +UNION DISTINCT +#{select_donation_supporters.parse} +) AS event_supporters" + expr = expr - .add_join( - Qx.select("MAX(tickets.event_id) AS event_id", "tickets.supporter_id") - .from(:tickets) - .group_by(:supporter_id) - .as(:tickets), - "tickets.supporter_id=supporters.id" - ) - .and_where("tickets.event_id=$id", id: query[:event_id]) + .add_join( + union_expr, + "event_supporters.supporter_id=supporters.id" + ) end if ['asc', 'desc'].include? query[:sort_name] expr = expr.order_by(["supporters.name", query[:sort_name]])