Skip to content

Commit

Permalink
Fix migrations task for country petition journals
Browse files Browse the repository at this point in the history
A `TRUNCATE` followed by an `INSERT ... FROM` doesn't really work that
well on a busy site like the petitions website because new journals will
start to appear before the `INSERT` completes. Fix this by doing it in
Ruby and locking the journals before calculating the signatures.

To do this in a performant manner we need an index on the petition_id
and location_code for the signatures table. We need to do this
concurrently otherwise it will block writes to the table, preventing
anyone from signing any petitions.
  • Loading branch information
pixeltrix committed Feb 17, 2016
1 parent 8316246 commit 53a8921
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class AddLocationCodeIndexToCountryPetitionJournal < ActiveRecord::Migration
disable_ddl_transaction!

def up
unless index_exists?(:signatures, [:petition_id, :location_code])
add_index :signatures, [:petition_id, :location_code], algorithm: :concurrently
end
end

def down
if index_exists?(:signatures, [:petition_id, :location_code])
remove_index :signatures, [:petition_id, :location_code]
end
end
end
9 changes: 9 additions & 0 deletions db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,13 @@ CREATE UNIQUE INDEX index_signatures_on_email_and_petition_id_and_name ON signat
CREATE INDEX index_signatures_on_petition_id ON signatures USING btree (petition_id);


--
-- Name: index_signatures_on_petition_id_and_location_code; Type: INDEX; Schema: public; Owner: -; Tablespace:
--

CREATE INDEX index_signatures_on_petition_id_and_location_code ON signatures USING btree (petition_id, location_code);


--
-- Name: index_signatures_on_updated_at; Type: INDEX; Schema: public; Owner: -; Tablespace:
--
Expand Down Expand Up @@ -1497,3 +1504,5 @@ INSERT INTO schema_migrations (version) VALUES ('20160211003703');

INSERT INTO schema_migrations (version) VALUES ('20160211020341');

INSERT INTO schema_migrations (version) VALUES ('20160217192016');

15 changes: 14 additions & 1 deletion lib/tasks/migrations.rake
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,20 @@ namespace :epets do
signature.save(validate: false)
end

CountryPetitionJournal.reset!
Petition.find_each do |petition|
locations = petition.signatures.validated.distinct.pluck(:location_code).compact

locations.each do |location|
journal = CountryPetitionJournal.for(petition, location)

journal.with_lock do
signature_count = petition.signatures.validated.where(location_code: location).count
journal.update(signature_count: signature_count)
end
end

petition.touch
end
end
end
end

0 comments on commit 53a8921

Please sign in to comment.