diff options
Diffstat (limited to 'lib/tasks')
| -rw-r--r-- | lib/tasks/pages.rake | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/lib/tasks/pages.rake b/lib/tasks/pages.rake index 0cfbc5dc..ea0776b7 100644 --- a/lib/tasks/pages.rake +++ b/lib/tasks/pages.rake | |||
| @@ -30,4 +30,32 @@ namespace :pages do | |||
| 30 | 30 | ||
| 31 | puts "#{write ? "updated" : "would update"} #{touched} pages" | 31 | puts "#{write ? "updated" : "would update"} #{touched} pages" |
| 32 | end | 32 | end |
| 33 | |||
| 34 | desc "Backfill pages.external_url from each page's node." | ||
| 35 | task :backfill_external_url => :environment do | ||
| 36 | write = ENV["WRITE"] == "1" | ||
| 37 | puts "DRY RUN -- nothing written. Re-run with WRITE=1." unless write | ||
| 38 | |||
| 39 | touched = 0 | ||
| 40 | Node.where.not(:external_url => [nil, ""]).find_each do |node| | ||
| 41 | scope = node.pages.where("external_url IS DISTINCT FROM :u", :u => node.external_url) | ||
| 42 | count = scope.count | ||
| 43 | next if count.zero? | ||
| 44 | |||
| 45 | scope.update_all(:external_url => node.external_url) if write | ||
| 46 | touched += count | ||
| 47 | end | ||
| 48 | |||
| 49 | # Autosaves carry no node_id, so has_many :pages does not cover them. | ||
| 50 | Node.where.not(:autosave_id => nil).where.not(:external_url => [nil, ""]) | ||
| 51 | .includes(:autosave).find_each do |node| | ||
| 52 | a = node.autosave | ||
| 53 | next if a.nil? || a.external_url == node.external_url | ||
| 54 | |||
| 55 | a.update_columns(:external_url => node.external_url) if write | ||
| 56 | touched += 1 | ||
| 57 | end | ||
| 58 | |||
| 59 | puts "#{write ? "updated" : "would update"} #{touched} pages" | ||
| 60 | end | ||
| 33 | end | 61 | end |
