Class: Browsercms330

Inherits:
ActiveRecord::Migration
  • Object
show all
Defined in:
lib/generators/cms/upgrade_module/templates/20100705083859_browsercms_3_3_0.rb

Class Method Summary collapse

Class Method Details

.downObject



48
49
50
51
52
53
54
55
# File 'lib/generators/cms/upgrade_module/templates/20100705083859_browsercms_3_3_0.rb', line 48

def self.down

  # Cannot restore paths with trailing slash - raise error or show message
  #
  # raise IrreversibleMigration, "Cannot reverse migration which removes trailing slash from page paths"
  puts "Page paths which had trailing slashes removed cannot be restored to their original state."

end

.upObject



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
# File 'lib/generators/cms/upgrade_module/templates/20100705083859_browsercms_3_3_0.rb', line 2

def self.up

  # patch required for LH345
  cant_fix = []
  to_fix = []
  # find all pages whose path ends in slash and is not root
  Page.find(:all, :conditions => "path LIKE '/%/'").each do |pt_page|
    # make sure no extant page has this path
    if Page.count(:conditions => ["path = ?", pt_page.path.sub(/(.+)\/+$/, '\1')]) > 0
        cant_fix << pt_page
      else
        to_fix << pt_page
      end
  end
  version_cant_fix = []
  version_to_fix = []
  # find all page versions whose path ends in slash and is not root
  Page::Version.find(:all, :conditions => "path LIKE '/%/'").each do |pt_page|
    # make sure no extant page has this path
    if Page.count(:conditions => ["path = ?", pt_page.path.sub(/(.+)\/+$/, '\1')]) > 0
        version_cant_fix << pt_page
      else
        version_to_fix << pt_page
      end
  end

  # raise an error if there are pages (*not* page versions) that will duplicate an extant path if the ending slash is dropped
  if cant_fix.length > 0
    raise "Cannot remove trailing slashes from pages with ID(s) (#{cant_fix.map(&:id).join(', ')}). Other pages already exist with their correct path. The offending path may be in an unpublished page version, newer than the current public version. These needed to be corrected manually in your DBMS before running this migration"
  end

  to_fix.each do |fix_page|
    # change the path of all pages with a trailing slash to not have one
    # using sql updates to prevent unwanted callbacks
    new_path = fix_page.path.to_s.sub(/(.+)\/+$/, '\1')
    execute "UPDATE pages SET path = '#{new_path}' WHERE id = #{fix_page.id};"
  end
  version_to_fix.each do |fix_page|
    # change the path of all fixable page versions with a trailing slash to not have one
    # using sql updates to prevent unwanted callbacks
    new_path = fix_page.path.to_s.sub(/(.+)\/+$/, '\1')
    execute "UPDATE page_versions SET path = '#{new_path}' WHERE id = #{fix_page.id};"
  end
  # end patch for LH345
end