Created
September 14, 2009 17:48
-
-
Save amiel/186799 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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