acread

Build Status

acread is a gem that helps you deprecating ActiveRecord attributes.

When you deprecate an attribute, acread can helps you in 3 ways :

  1. helps you finding where you are using this attribute by creating glue to raise a DeprecatedAttributeError.
  2. ignore this atribute when serializing the object through to_json, to_xml ...
  3. 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.stacktrace.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 :

  1. Deploy your code with deprecation declaration in it
  2. Run the migration
  3. Remove deprecation declarations from your code
  4. Deploy your final clean version of code

Copyright

Copyright (c) 2012 yann ARMAND & Nick Campbell under MIT See LICENSE.txt for further details.