deprecating activerecord attributes without making zombies

Post on 06-May-2015

391 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

-Friday, May 10, 13

YANN ARMAND

-

@yarmand

Friday, May 10, 13

-

The Enterprise Social Network

Friday, May 10, 13

-Friday, May 10, 13

DELETE A DB COLUMNWITHOUT MAKING ZOMBIES

-Friday, May 10, 13

DELETE A DB COLUMNWITHOUT MAKING ZOMBIES

-Friday, May 10, 13

WHY DELETE A MODEL ATTRIBUTE ?

-Friday, May 10, 13

WHY DELETE A MODEL ATTRIBUTE ?

•Not used anymore

-Friday, May 10, 13

WHY DELETE A MODEL ATTRIBUTE ?

•Not used anymore

User Payment card• card_type• card_number

•Move to another model

-Friday, May 10, 13

KILLING AN ACTIVE RECORD FIELD

Cons

-Friday, May 10, 13

KILLING AN ACTIVE RECORD FIELD

•Code dependency

Cons

-Friday, May 10, 13

KILLING AN ACTIVE RECORD FIELD

•Code dependency

Cons

•Unknown impact on the app behavior

-Friday, May 10, 13

KILLING AN ACTIVE RECORD FIELD

•Code dependency

Cons

•Unknown impact on the app behavior

-Friday, May 10, 13

KILLING AN ACTIVE RECORD FIELD

•Code dependency

Cons

•Unknown impact on the app behavior

-Friday, May 10, 13

KILL AN ACTIVE RECORD FIELD

Pros

-Friday, May 10, 13

KILL AN ACTIVE RECORD FIELD

Pros

•Reduce Technical Debt

-Friday, May 10, 13

KILL AN ACTIVE RECORD FIELD

Pros

•Reduce Technical Debt

• Smaller code base

• Lower barrier of entry

-

• Eliminate black holes

• Prevent crashes

Friday, May 10, 13

BE PREPARED !!

-Friday, May 10, 13

BE PREPARED !!

-Friday, May 10, 13

BE PREPARED !!

-Friday, May 10, 13

class User deprecate_attribute :card_type deprecate_attribute :card_numberend

-Friday, May 10, 13

API OUTPUT

render :json => user

name: 'roger'age: 23card_type: 'visa'card_number: 123412341234

name: 'roger'age: 23

-Friday, May 10, 13

API OUTPUT

render :json => user

name: 'roger'age: 23card_type: 'visa'card_number: 123412341234

name: 'roger'age: 23

-

ActiveRecord#serializable_hash

Friday, May 10, 13

API OUTPUT

-

class User alias_method :super_serializable_hash, :serializable_hash

def serializable_hash(options = {}) options.merge! { :only => self.attributes.keys.map(&:to_sym) - (self.class.deprecated_attributes || []).map(&:to_sym) } super_serializable_hash(options) endend

Friday, May 10, 13

API OUTPUT

-

class User alias_method :super_serializable_hash, :serializable_hash

def serializable_hash(options = {}) options.merge! { :only => self.attributes.keys.map(&:to_sym) - (self.class.deprecated_attributes || []).map(&:to_sym) } super_serializable_hash(options) endend

Friday, May 10, 13

API OUTPUT

-

class User alias_method :super_serializable_hash, :serializable_hash

def serializable_hash(options = {}) options.merge! { :only => self.attributes.keys.map(&:to_sym) - (self.class.deprecated_attributes || []).map(&:to_sym) } super_serializable_hash(options) endend

Friday, May 10, 13

API OUTPUT

-

class User alias_method :super_serializable_hash, :serializable_hash

def serializable_hash(options = {}) options.merge! { :only => self.attributes.keys.map(&:to_sym) - (self.class.deprecated_attributes || []).map(&:to_sym) } super_serializable_hash(options) endend

Friday, May 10, 13

ACCESSORS =

[ '',

'=',

'_before_type_cast',

'?',

'_changed?',

'_change',

'_will_change!',

'_was']Zombie Radar

-

FIELD HUNTING

Friday, May 10, 13

-

def deprecate_attribute attr msg = "You can't access atribute #{attr}, it has been deprecated" ACCESSORS.each do |term|

define_method("#{attr}#{term}") do |*args| raise DeprecatedAttributeError, msg super end endend

Friday, May 10, 13

-

def deprecate_attribute attr msg = "You can't access atribute #{attr}, it has been deprecated" ACCESSORS.each do |term|

define_method("#{attr}#{term}") do |*args| raise DeprecatedAttributeError, msg super end endend

Friday, May 10, 13

-

def deprecate_attribute attr msg = "You can't access atribute #{attr}, it has been deprecated" ACCESSORS.each do |term|

define_method("#{attr}#{term}") do |*args| raise DeprecatedAttributeError, msg super end endend

Friday, May 10, 13

-

def deprecate_attribute attr msg = "You can't access atribute #{attr}, it has been deprecated" ACCESSORS.each do |term|

define_method("#{attr}#{term}") do |*args| raise DeprecatedAttributeError, msg super end endend

Friday, May 10, 13

-

def deprecate_attribute attr msg = "You can't access atribute #{attr}, it has been deprecated" ACCESSORS.each do |term|

define_method("#{attr}#{term}") do |*args| raise DeprecatedAttributeError, msg super end endend

Friday, May 10, 13

-

def deprecate_attribute attr msg = "You can't access atribute #{attr}, it has been deprecated" ACCESSORS.each do |term|

define_method("#{attr}#{term}") do |*args| raise DeprecatedAttributeError, msg super end endend

Friday, May 10, 13

-Friday, May 10, 13

-

class ApplicationController rescue_from DeprecatedAttributeError, :with => :log_deprecate private

def log_deprecated e deprecated_logger.error(e.backtrace.join("\n"))

e.continue end

end

Friday, May 10, 13

-

cmaruz/continuable

class ApplicationController rescue_from DeprecatedAttributeError, :with => :log_deprecate private

def log_deprecated e deprecated_logger.error(e.backtrace.join("\n"))

e.continue end

end

Friday, May 10, 13

REFACTOR

-Friday, May 10, 13

REFACTOR

-

DEPLOY

Friday, May 10, 13

REFACTOR

New Code

-

DEPLOY

Friday, May 10, 13

REFACTOR

MigrationNew Code

-

DEPLOY

Friday, May 10, 13

REFACTOR

MigrationNew Code

-

DEPLOY

Friday, May 10, 13

WHAT HAPPENS ?

name: 'roger'age: 23card_type: 'visa'card_number: 123412341234

-

TimeIn Cache

Friday, May 10, 13

WHAT HAPPENS ?

name: 'roger'age: 23card_type: 'visa'card_number: 123412341234

Deploy

-

TimeIn Cache

Friday, May 10, 13

WHAT HAPPENS ?

name: 'roger'age: 23card_type: 'visa'card_number: 123412341234

Deploy

name: 'roger'age: 24card_type: 'visa'card_number: 123412341234

-

TimeIn Cache Update

Friday, May 10, 13

WHAT HAPPENS ?

name: 'roger'age: 23card_type: 'visa'card_number: 123412341234

Deploy

name: 'roger'age: 24card_type: 'visa'card_number: 123412341234

Save

-

TimeIn Cache Update

Friday, May 10, 13

WHAT HAPPENS ?

name: 'roger'age: 23card_type: 'visa'card_number: 123412341234

Deploy

name: 'roger'age: 24card_type: 'visa'card_number: 123412341234

Save

Database Exceptionunknown columns

card_type, card_number

-

TimeIn Cache Update

Friday, May 10, 13

WHAT HAPPENS ?

name: 'roger'age: 23card_type: 'visa'card_number: 123412341234

Deploy

name: 'roger'age: 24card_type: 'visa'card_number: 123412341234

Save

Database Exceptionunknown columns

card_type, card_number

-

TimeIn Cache Update

Friday, May 10, 13

IGNORE COLUMNS

-

class User def columns self.class.columns.reject do |c| (self.class.deprecated_attributes || []).include? c.name.to_s end endend

Friday, May 10, 13

THANK YOU !!

https://github.com/yarmand/acread

Acread

-Friday, May 10, 13

top related