acread is a gem that helps you deprecating ActiveRecord attributes.
When you deprecate an attribute, acread can helps you in 3 ways :
- helps you finding where you are using this attribute by creating glue to raise a
DeprecatedAttributeError
. - ignore this atribute when serializing the object through to_json, to_xml ...
- helps your zero downtime migration by ignoring the attribute for objects already in memory when saving to database.
Have a look at this London Ruby Group Lightning talk for a quick description of that 3 steps.
add to your Gemfile :
gem 'acread'
class Person < ActiveRecord::Base
...
deprecate_attribute :long_name
...
end
you can catch the DeprecatedAttributeError
exception and for example put a backtrace in a specific logger.
The DeprecatedAttributeError can then be continued, this mean you can catch it and continue normal ActiveRecord behavior. You're code hunting can then be done without any effect on your datas by simply catching the exception, logging and continuing.
class ApplicationController
rescue_from DeprecatedAttributeError, :with => :log_deprecate
private
def deprecated_logger
@@deprecated_logger ||= Logger.new("#{Rails.root}/log/deprecated_calls.log")
end
def log_deprecated e
deprecated_logger.error(e.backtrace.join("\n"))
e.continue
end
end
When you are done with cleaning your code from any usage of deprecated attributes, you can prepare a migration including some drop_columns.
example :
class RemoveLongNames < ActiveRecord::Migration
def self.up
remove_column :Person, :long_name
end
def self.down
raise ActiveRecord::IrreversibleMigration
end
end
Then you can safely follow the steps :
- Deploy your code with deprecation declaration in it
- Run the migration
- Remove deprecation declarations from your code
- Deploy your final clean version of code
Copyright (c) 2012 yann ARMAND & Nick Campbell under MIT See LICENSE.txt for further details.