Created
September 7, 2012 14:28
-
-
Save simplyb/3666658 to your computer and use it in GitHub Desktop.
rake task to scrub tables
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
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