-
-
Save reborg/781702 to your computer and use it in GitHub Desktop.
Revisions
-
reborg revised this gist
Jan 16, 2011 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -18,8 +18,8 @@ namespace :teamly do 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| -
reborg revised this gist
Jan 16, 2011 . 1 changed file with 31 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,31 @@ ## # 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 -
reborg revised this gist
Jan 16, 2011 . 1 changed file with 4 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,7 @@ ## # More info at: http://adam.heroku.com/past/2009/3/30/export_to_csv/ # Use as: # DATABASE_URL=sqlite://db/development.sqlite3 rake export table=users > users.csv task :export do require 'sequel' require 'fastercsv' @@ -19,4 +23,3 @@ puts csv end -
adamwiggins revised this gist
Mar 14, 2009 . 1 changed file with 15 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,14 +1,22 @@ task :export do require 'sequel' require 'fastercsv' db = Sequel.connect(ENV['DATABASE_URL']) table_name = ENV['table'].to_sym table = db[table_name] fields = table.first.keys csv = FasterCSV.generate do |csv| csv << fields table.all.each do |record| csv << fields.map { |f| record[f] } end end puts csv end -
adamwiggins revised this gist
Mar 14, 2009 . 1 changed file with 6 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,11 +1,14 @@ task :export do require 'sequel' db = Sequel.connect(ENV['DATABASE_URL']) table_name = :users out = db[table_name].first.keys.join(",") + "\n" db[table_name].all.each do |row| out += row.values.join(",") + "\n" end puts out end -
adamwiggins created this gist
Mar 14, 2009 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,11 @@ task :export do db = Sequel.connect(ENV['DATABASE_URL']) table_name = :my_table out = db[table_name].keys.join(",") + "\n" db[table].all.each do |row| out += row.values.join(",") + "\n" end end