Skip to content

Commit

Permalink
Fixes the redirect fix migration.
Browse files Browse the repository at this point in the history
Tok me a while to figure out that the first migration didn't actually migrate the data because of a silent validation failure.
  • Loading branch information
Frédéric de Villamil committed Aug 25, 2013
1 parent 7dd91f1 commit 2e07b26
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions db/migrate/112_fixes_notes_redirects.rb
@@ -1,16 +1,17 @@
class FixesNotesRedirects < ActiveRecord::Migration
def self.up
say "Fixes notes redirects"
say "Fixes notes redirects, it may take some time"

notes = Note.find(:all)
notes.each do |note|
if note.redirects.size > 0
say "Fixes note #{note.id}"
from = note.redirects.first.to_path
note.redirects.first.to_path.gsub!(File.join(Blog.default.base_url, "st"), File.join(Blog.default.base_url, "note"))
note.redirects.first.save
redirect = Redirect.new(from_path: from, to_path: note.redirects.first.to_path)
redirect.save
old_from = note.redirects.first.to_path
from = note.redirects.first.from_path
to = note.redirects.first.to_path.gsub(File.join(Blog.default.base_url, "st"), File.join(Blog.default.base_url, "note"))
redirect = note.redirects.first
redirect.update_attribute('to_path', to)
redirect.save!
Redirect.create(from_path: old_from, to_path: to)
end
end
end
Expand All @@ -23,7 +24,7 @@ def self.down
if note.redirects.size > 0
redirect = Redirect.find.where(to_path: note.redirects.first.to_path)
redirect.destroy
note.redirects.first.to_path.gsub!(File.join(Blog.default.base_url, "note"), File.join(Blog.default.base_url, "st"))
note.redirects.first.to_path = note.redirects.first.to_path.gsub!(File.join(Blog.default.base_url, "note"), File.join(Blog.default.base_url, "st"))
note.redirects.first.save
end
end
Expand Down

0 comments on commit 2e07b26

Please sign in to comment.