-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CV2-4458: improve-cluster-rake-task (#1853)
* CV2-4458: add uuid column to media * CV2-4458: fix rake task * CV2-4458: use downcase to get uuid
- Loading branch information
Showing
7 changed files
with
63 additions
and
47 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
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,5 @@ | ||
class AddUuidToMedias < ActiveRecord::Migration[6.1] | ||
def change | ||
add_column :medias, :uuid, :integer, null: false, default: 0 | ||
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
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,36 @@ | ||
namespace :check do | ||
namespace :migrate do | ||
task migrate_media_uuid: :environment do | ||
started = Time.now.to_i | ||
# Media of type Claim | ||
last_claim_id = Rails.cache.read('check:migrate:migrate_media_uuid:claim_id') || 0 | ||
Media.where(type: 'Claim').where('id > ?', last_claim_id).find_in_batches(batch_size: 2000) do |medias| | ||
m_items = [] | ||
medias.each do |m| | ||
print '.' | ||
uuid = Media.where(type: 'Claim') | ||
.where('lower(quote) = ?', m.quote.to_s.strip.downcase) | ||
.joins("INNER JOIN project_medias pm ON pm.media_id = medias.id").first&.id | ||
uuid ||= m.id | ||
m.uuid = uuid | ||
m_items << m.attributes | ||
end | ||
Media.upsert_all(m_items) | ||
last_id = medias.map(&:id).max | ||
Rails.cache.write('check:migrate:migrate_media_uuid:claim_id', last_id) | ||
end | ||
# Other medias (link, image, audio, etc) | ||
Media.where.not(type: 'Claim').find_in_batches(batch_size: 2000) do |medias| | ||
m_items = [] | ||
medias.each do |m| | ||
print '.' | ||
m.uuid = m.id | ||
m_items << m.attributes | ||
end | ||
Media.upsert_all(m_items) | ||
end | ||
minutes = ((Time.now.to_i - started) / 60).to_i | ||
puts "[#{Time.now}] Done in #{minutes} minutes." | ||
end | ||
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