Skip to content

Instantly share code, notes, and snippets.

@amiel
Created September 14, 2009 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amiel/186799 to your computer and use it in GitHub Desktop.
Save amiel/186799 to your computer and use it in GitHub Desktop.
class MoveToParanoid < ActiveRecord::Migration
def self.up
classes_with_active = Dir['app/models/*'].collect do |f,o|
n = File.basename(f,'.rb')
m = n.classify.constantize
if m.respond_to?('column_names')
m.reset_column_information
m.column_names.include?('active') and m
end or nil
end.compact
classes_with_active.each do |c|
add_column c.table_name, :deleted_at, :timestamp
c.update_all [ 'deleted_at = ?', Time.current ], [ 'active = ?', false ]
remove_column c.table_name, :active
end
end
def self.down
classes_with_paranoid = Dir['app/models/*'].collect do |f,o|
n = File.basename(f,'.rb')
m = n.classify.constantize
if m.respond_to?('column_names')
m.reset_column_information
m.column_names.include?('deleted_at') and m
end or nil
end.compact
classes_with_paranoid.each do |c|
add_column c.table_name, :active, :boolean, :default => true
c.update_all_with_destroyed [ 'active = ?', true ], 'deleted_at IS NULL'
c.update_all_with_destroyed [ 'active = ?', false ], 'deleted_at IS NOT NULL'
remove_column c.table_name, :deleted_at
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment