-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add provenance convenience column to pub table; rake task to backfill…
… data
- Loading branch information
Showing
4 changed files
with
35 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class AddProvenanceField < ActiveRecord::Migration[6.1] | ||
def change | ||
add_column :publications, :provenance, :string | ||
add_index :publications, :provenance | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
namespace :data do | ||
desc 'Backfile provenance into AR column for all records' | ||
# A new field was added to the publication table to allow for querying on publication provenance (already stored in pub_hash). | ||
# This task goes through all publications and adds the value to this field from the pub_hash | ||
# After this task completes, we can remove the `Publication#provenance` method | ||
# RAILS_ENV=production bundle exec rake cleanup:merge_profiles[123,456] # will merge all publications from cap_profile_id 456 into 123, without duplication | ||
# rubocop:disable Rails/SkipsModelValidations | ||
task add_provenance: :environment do |_t, _args| | ||
num_pubs = Publication.where(provenance: nil).count | ||
puts "Started at #{Time.zone.now}" | ||
puts "Found #{num_pubs} with missing provenance." | ||
Publication.where(provenance: nil).find_each.with_index do |pub, i| | ||
puts "#{i + 1} of #{num_pubs}" | ||
pub.update_column('provenance', pub.pub_hash[:provenance]) # skip callbacks and timestamp updates, just set the value | ||
end | ||
puts "Finished at #{Time.zone.now}" | ||
end | ||
# rubocop:enable Rails/SkipsModelValidations | ||
end |