Skip to content

Instantly share code, notes, and snippets.

@simplyb
Created September 7, 2012 14:28
Show Gist options
  • Save simplyb/3666658 to your computer and use it in GitHub Desktop.
Save simplyb/3666658 to your computer and use it in GitHub Desktop.
rake task to scrub tables
namespace :data do
desc "scrubs all strings in every table"
task :scrub => :environment do
unless Rails.env.development?
raise "This should only ever be run in development!"
end
tables = ActiveRecord::Base.connection.tables - ["schema_migrations"]
tables.each do |table|
string_attributes = table.classify.constantize.columns.map {|c| c.name if [:string, :text].include?(c.type) }.compact
setters = []
update_string = "UPDATE #{table} SET "
string_attributes.each do |attribute|
setters << "#{attribute}=SUBSTRING('#{MyApp::RakeData::HILARIOUS_STRING}',1,LENGTH(#{attribute}))"
end
update_string += setters.join(",")
begin
ActiveRecord::Base.connection.execute(sql)
puts "#{table} successfully scrubbed."
rescue Exception => e
raise "Failed: #{e}"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment