summaryrefslogtreecommitdiff
path: root/lib/tasks/pages.rake
blob: ea0776b7dec87290d66619de6b59e276f8ff7818 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
namespace :pages do
  desc "Backfill pages.slug and pages.parent_node_id from each page's " \
       "node. Historical accuracy is not attempted. Every revision gets " \
       "the node's current address, which is right for head and draft and " \
       "harmless for older revisions, and avoids nil checks everywhere. " \
       "Dry run unless WRITE=1."
  task :backfill_address => :environment do
    write = ENV["WRITE"] == "1"
    puts "DRY RUN -- nothing written. Re-run with WRITE=1." unless write

    touched = 0
    Node.find_each do |node|
      scope = node.pages.where("slug IS DISTINCT FROM :s OR parent_node_id IS DISTINCT FROM :p",
                               :s => node.slug, :p => node.parent_id)
      count = scope.count
      next if count.zero?

      scope.update_all(:slug => node.slug, :parent_node_id => node.parent_id) if write
      touched += count
    end

    # Autosaves carry no node_id -- has_many :pages does not cover them.
    Node.where.not(:autosave_id => nil).includes(:autosave).find_each do |node|
      a = node.autosave
      next if a.slug == node.slug && a.parent_node_id == node.parent_id

      a.update_columns(:slug => node.slug, :parent_node_id => node.parent_id) if write
      touched += 1
    end

    puts "#{write ? "updated" : "would update"} #{touched} pages"
  end

  desc "Backfill pages.external_url from each page's node."
  task :backfill_external_url => :environment do
    write = ENV["WRITE"] == "1"
    puts "DRY RUN -- nothing written. Re-run with WRITE=1." unless write

    touched = 0
    Node.where.not(:external_url => [nil, ""]).find_each do |node|
      scope = node.pages.where("external_url IS DISTINCT FROM :u", :u => node.external_url)
      count = scope.count
      next if count.zero?

      scope.update_all(:external_url => node.external_url) if write
      touched += count
    end

    # Autosaves carry no node_id, so has_many :pages does not cover them.
    Node.where.not(:autosave_id => nil).where.not(:external_url => [nil, ""])
        .includes(:autosave).find_each do |node|
      a = node.autosave
      next if a.nil? || a.external_url == node.external_url

      a.update_columns(:external_url => node.external_url) if write
      touched += 1
    end

    puts "#{write ? "updated" : "would update"} #{touched} pages"
  end
end