Add rescues to the aaj migrations for unsaved records.

If the Journal or the Journaled object don't save successfully,
put out the reasons why instead of aborting the migrations. Journaled
objects might failure from the :touch update, which are safe to ignore.
This commit is contained in:
Eric Davis 2011-07-01 15:16:07 -07:00
parent 32f92a5058
commit 6017e2712e
3 changed files with 19 additions and 5 deletions

View File

@ -14,7 +14,12 @@ class UpdateJournalsForActsAsJournalized < ActiveRecord::Migration
j.type = klass_name
j.version = idx + 2 # initial journal should be 1
j.activity_type = j.journalized_type.constantize.activity_provider_options.keys.first
j.save(false)
begin
j.save(false)
rescue ActiveRecord::RecordInvalid => ex
puts "Error saving: #{j.class.to_s}##{j.id} - #{ex.message}"
end
end
end
end

View File

@ -45,7 +45,11 @@ class BuildInitialJournalsForActsAsJournalized < ActiveRecord::Migration
elsif o.respond_to?(:user)
new_journal.user = o.user
end
if new_journal.save
# Using rescue and save! here because either the Journal or the
# touched record could fail. This will catch either error and continue
begin
new_journal.save!
new_journal.reload
# Backdate journal
@ -54,12 +58,12 @@ class BuildInitialJournalsForActsAsJournalized < ActiveRecord::Migration
elsif o.respond_to?(:created_on)
new_journal.update_attribute(:created_at, o.created_on)
end
else
rescue ActiveRecord::RecordInvalid => ex
if new_journal.errors.count == 1 && new_journal.errors.first[0] == "version"
# Skip, only error was from creating the initial journal for a record that already had one.
else
puts "ERROR: errors creating the initial journal for #{o.class.to_s}##{o.id.to_s}:"
puts " #{new_journal.errors.full_messages.inspect}"
puts " #{ex.message}"
end
end
end

View File

@ -16,7 +16,12 @@ class AddChangesFromJournalDetailsForActsAsJournalized < ActiveRecord::Migration
elsif detail.property == 'attachment' # Attachment
changes["attachments_" + detail.prop_key.to_s] = [detail.old_value, detail.value]
end
journal.update_attribute(:changes, changes.to_yaml)
begin
journal.update_attribute(:changes, changes.to_yaml)
rescue ActiveRecord::RecordInvalid => ex
puts "Error saving: #{journal.class.to_s}##{journal.id} - #{ex.message}"
end
end
end