Module: Alchemy::Upgrader::EightOne

Defined in:
lib/alchemy/upgrader/eight_one.rb

Instance Method Summary collapse

Instance Method Details

#migrate_page_metadataObject



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
# File 'lib/alchemy/upgrader/eight_one.rb', line 6

def 
  desc "Migrating page metadata to page versions"

  connection = ActiveRecord::Base.connection
  pages_table = Alchemy::Page.table_name
  versions_table = Alchemy::PageVersion.table_name

  unmigrated_count = connection.select_value(<<-SQL.strip_heredoc).to_i
    SELECT COUNT(*) FROM #{versions_table}
    WHERE title IS NULL
      AND meta_description IS NULL
      AND meta_keywords IS NULL
  SQL

  if unmigrated_count == 0
    log "All page versions have meta data.", :skip
    return
  end

  sql = if connection.adapter_name.downcase.match?(/mysql|trilogy/)
    <<-SQL.strip_heredoc
      UPDATE #{versions_table} pv
      INNER JOIN #{pages_table} p ON pv.page_id = p.id
      SET pv.title = p.title,
          pv.meta_description = p.meta_description,
          pv.meta_keywords = p.meta_keywords
      WHERE pv.title IS NULL
        AND pv.meta_description IS NULL
        AND pv.meta_keywords IS NULL
    SQL
  else
    <<-SQL.strip_heredoc
      UPDATE #{versions_table}
      SET title = p.title,
          meta_description = p.meta_description,
          meta_keywords = p.meta_keywords
      FROM #{pages_table} p
      WHERE #{versions_table}.page_id = p.id
        AND #{versions_table}.title IS NULL
        AND #{versions_table}.meta_description IS NULL
        AND #{versions_table}.meta_keywords IS NULL
    SQL
  end

  migrated_count = connection.update(sql)

  log "Migrated metadata for #{migrated_count} page versions."
end