Last Updated: February 25, 2016
·
579
· Alexandr K

Wrap your migrations by exceptions tracker.

Lets say you are writing the migration to change data in the database like the assignments the new value to the field.

First of all it would be great to use the save! or update_attributes! methods to raise exceptions in case of the validations. Add the rescue block for these statements and write something like this:

def self.up
  YourModel.all.each do |model|
    begin
      model.update_attributes!(field: 'new-value')
    rescue => boom
       Honeybadger.notify(
          :error_class   => 'MigrationName',
          :error_message => boom.message,
          :parameters    => {
            model: { id: model.to_param }
          }
        )
     end
  end
end

Probably it's not a good approach to make things happens like finished the migration in case of the invalid records but I can see in the honeybadger what kind of the records I need to change or have attention. I can create the trello card to fix it providing the url to the honeybadger tracker instead of describing the problem.