From 6017e2712e16febf3f0210c8cc8b6c6b25a0f999 Mon Sep 17 00:00:00 2001 From: Eric Davis Date: Fri, 1 Jul 2011 15:16:07 -0700 Subject: [PATCH] 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. --- ...14111652_update_journals_for_acts_as_journalized.rb | 7 ++++++- ...3_build_initial_journals_for_acts_as_journalized.rb | 10 +++++++--- ...ges_from_journal_details_for_acts_as_journalized.rb | 7 ++++++- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/db/migrate/20100714111652_update_journals_for_acts_as_journalized.rb b/db/migrate/20100714111652_update_journals_for_acts_as_journalized.rb index 8449412e..e279308d 100644 --- a/db/migrate/20100714111652_update_journals_for_acts_as_journalized.rb +++ b/db/migrate/20100714111652_update_journals_for_acts_as_journalized.rb @@ -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 diff --git a/db/migrate/20100714111653_build_initial_journals_for_acts_as_journalized.rb b/db/migrate/20100714111653_build_initial_journals_for_acts_as_journalized.rb index a5bf8e16..3574e642 100644 --- a/db/migrate/20100714111653_build_initial_journals_for_acts_as_journalized.rb +++ b/db/migrate/20100714111653_build_initial_journals_for_acts_as_journalized.rb @@ -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 diff --git a/db/migrate/20100714111654_add_changes_from_journal_details_for_acts_as_journalized.rb b/db/migrate/20100714111654_add_changes_from_journal_details_for_acts_as_journalized.rb index 99fe1295..43ef3256 100644 --- a/db/migrate/20100714111654_add_changes_from_journal_details_for_acts_as_journalized.rb +++ b/db/migrate/20100714111654_add_changes_from_journal_details_for_acts_as_journalized.rb @@ -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