acread
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.
Usage
Installation
add to your Gemfile :
gem 'acread'
deprecate an attribute
class Person < ActiveRecord::Base
...
deprecate_attribute :long_name
...
end
find attribute usage
you can catch the DeprecatedAttributeError
exception and for example put a backtrace in a specific logger.
If you are using ruby > 1.9, Acread use the gem continuable (https://github.com/cmaruz/continuable). 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
zero downtime migration
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
Copyright (c) 2012 yann ARMAND & Nick Campbell under MIT See LICENSE.txt for further details.