deprecating activerecord attributes without making zombies

50
- Friday, May 10, 13

Upload: yann-armand

Post on 06-May-2015

391 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Deprecating ActiveRecord Attributes without making Zombies

-Friday, May 10, 13

Page 2: Deprecating ActiveRecord Attributes without making Zombies

YANN ARMAND

-

@yarmand

Friday, May 10, 13

Page 3: Deprecating ActiveRecord Attributes without making Zombies

-

The Enterprise Social Network

Friday, May 10, 13

Page 4: Deprecating ActiveRecord Attributes without making Zombies

-Friday, May 10, 13

Page 5: Deprecating ActiveRecord Attributes without making Zombies

DELETE A DB COLUMNWITHOUT MAKING ZOMBIES

-Friday, May 10, 13

Page 6: Deprecating ActiveRecord Attributes without making Zombies

DELETE A DB COLUMNWITHOUT MAKING ZOMBIES

-Friday, May 10, 13

Page 7: Deprecating ActiveRecord Attributes without making Zombies

WHY DELETE A MODEL ATTRIBUTE ?

-Friday, May 10, 13

Page 8: Deprecating ActiveRecord Attributes without making Zombies

WHY DELETE A MODEL ATTRIBUTE ?

•Not used anymore

-Friday, May 10, 13

Page 9: Deprecating ActiveRecord Attributes without making Zombies

WHY DELETE A MODEL ATTRIBUTE ?

•Not used anymore

User Payment card• card_type• card_number

•Move to another model

-Friday, May 10, 13

Page 10: Deprecating ActiveRecord Attributes without making Zombies

KILLING AN ACTIVE RECORD FIELD

Cons

-Friday, May 10, 13

Page 11: Deprecating ActiveRecord Attributes without making Zombies

KILLING AN ACTIVE RECORD FIELD

•Code dependency

Cons

-Friday, May 10, 13

Page 12: Deprecating ActiveRecord Attributes without making Zombies

KILLING AN ACTIVE RECORD FIELD

•Code dependency

Cons

•Unknown impact on the app behavior

-Friday, May 10, 13

Page 13: Deprecating ActiveRecord Attributes without making Zombies

KILLING AN ACTIVE RECORD FIELD

•Code dependency

Cons

•Unknown impact on the app behavior

-Friday, May 10, 13

Page 14: Deprecating ActiveRecord Attributes without making Zombies

KILLING AN ACTIVE RECORD FIELD

•Code dependency

Cons

•Unknown impact on the app behavior

-Friday, May 10, 13

Page 15: Deprecating ActiveRecord Attributes without making Zombies

KILL AN ACTIVE RECORD FIELD

Pros

-Friday, May 10, 13

Page 16: Deprecating ActiveRecord Attributes without making Zombies

KILL AN ACTIVE RECORD FIELD

Pros

•Reduce Technical Debt

-Friday, May 10, 13

Page 17: Deprecating ActiveRecord Attributes without making Zombies

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

Page 18: Deprecating ActiveRecord Attributes without making Zombies

BE PREPARED !!

-Friday, May 10, 13

Page 19: Deprecating ActiveRecord Attributes without making Zombies

BE PREPARED !!

-Friday, May 10, 13

Page 20: Deprecating ActiveRecord Attributes without making Zombies

BE PREPARED !!

-Friday, May 10, 13

Page 21: Deprecating ActiveRecord Attributes without making Zombies

class User deprecate_attribute :card_type deprecate_attribute :card_numberend

-Friday, May 10, 13

Page 22: Deprecating ActiveRecord Attributes without making Zombies

API OUTPUT

render :json => user

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

name: 'roger'age: 23

-Friday, May 10, 13

Page 23: Deprecating ActiveRecord Attributes without making Zombies

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

Page 24: Deprecating ActiveRecord Attributes without making Zombies

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

Page 25: Deprecating ActiveRecord Attributes without making Zombies

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

Page 26: Deprecating ActiveRecord Attributes without making Zombies

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

Page 27: Deprecating ActiveRecord Attributes without making Zombies

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

Page 28: Deprecating ActiveRecord Attributes without making Zombies

ACCESSORS =

[ '',

'=',

'_before_type_cast',

'?',

'_changed?',

'_change',

'_will_change!',

'_was']Zombie Radar

-

FIELD HUNTING

Friday, May 10, 13

Page 29: Deprecating ActiveRecord Attributes without making Zombies

-

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

Page 30: Deprecating ActiveRecord Attributes without making Zombies

-

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

Page 31: Deprecating ActiveRecord Attributes without making Zombies

-

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

Page 32: Deprecating ActiveRecord Attributes without making Zombies

-

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

Page 33: Deprecating ActiveRecord Attributes without making Zombies

-

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

Page 34: Deprecating ActiveRecord Attributes without making Zombies

-

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

Page 35: Deprecating ActiveRecord Attributes without making Zombies

-Friday, May 10, 13

Page 36: Deprecating ActiveRecord Attributes without making Zombies

-

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

Page 37: Deprecating ActiveRecord Attributes without making Zombies

-

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

Page 38: Deprecating ActiveRecord Attributes without making Zombies

REFACTOR

-Friday, May 10, 13

Page 39: Deprecating ActiveRecord Attributes without making Zombies

REFACTOR

-

DEPLOY

Friday, May 10, 13

Page 40: Deprecating ActiveRecord Attributes without making Zombies

REFACTOR

New Code

-

DEPLOY

Friday, May 10, 13

Page 41: Deprecating ActiveRecord Attributes without making Zombies

REFACTOR

MigrationNew Code

-

DEPLOY

Friday, May 10, 13

Page 42: Deprecating ActiveRecord Attributes without making Zombies

REFACTOR

MigrationNew Code

-

DEPLOY

Friday, May 10, 13

Page 43: Deprecating ActiveRecord Attributes without making Zombies

WHAT HAPPENS ?

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

-

TimeIn Cache

Friday, May 10, 13

Page 44: Deprecating ActiveRecord Attributes without making Zombies

WHAT HAPPENS ?

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

Deploy

-

TimeIn Cache

Friday, May 10, 13

Page 45: Deprecating ActiveRecord Attributes without making Zombies

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

Page 46: Deprecating ActiveRecord Attributes without making Zombies

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

Page 47: Deprecating ActiveRecord Attributes without making Zombies

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

Page 48: Deprecating ActiveRecord Attributes without making Zombies

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

Page 49: Deprecating ActiveRecord Attributes without making Zombies

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

Page 50: Deprecating ActiveRecord Attributes without making Zombies

THANK YOU !!

https://github.com/yarmand/acread

Acread

-Friday, May 10, 13