## # Ideas for this CSV export comes from: # - http://dominikgrabiec.com/111-simple-dataset-exportimport-for-rails/ # namespace :teamly do namespace :export do desc 'CSV Export of the relevant user data' task :users => :environment do excludes = ['crypted_password','password_salt','persistence_token','single_access_token','perishable_token','ancestry','time_zone','hide_announcement_time','reminder_hour','reminder_min'] export_csv_file_for_table_with_exclusions('users', excludes) end desc 'CSV Export of the relevant account data' task :accounts => :environment do export_csv_file_for_table_with_exclusions('accounts') end end def export_csv_file_for_table_with_exclusions(table, excludes = []) require "fastercsv" require "fileutils" ActiveRecord::Base.establish_connection database = ActiveRecord::Base.connection FasterCSV.open(File.join(File.dirname(__FILE__), "/../../#{table}.csv"), "w") do |csv| csv << includes = database.columns(table).map(&:name) - excludes database.select_rows("select #{includes.join(',')} from #{table}").each { |row| csv << row } end end end