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
cant_fix = []
to_fix = []
Page.find(:all, :conditions => "path LIKE '/%/'").each do |pt_page|
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 = []
Page::Version.find(:all, :conditions => "path LIKE '/%/'").each do |pt_page|
if Page.count(:conditions => ["path = ?", pt_page.path.sub(/(.+)\/+$/, '\1')]) > 0
version_cant_fix << pt_page
else
version_to_fix << pt_page
end
end
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|
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|
new_path = fix_page.path.to_s.sub(/(.+)\/+$/, '\1')
execute "UPDATE page_versions SET path = '#{new_path}' WHERE id = #{fix_page.id};"
end
end
|