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 characters
module DatabaseQueryStreaming | |
def stream_query_rows(sql_query, options="WITH CSV HEADER") | |
conn = ActiveRecord::Base.connection.raw_connection | |
conn.copy_data "COPY (#{sql_query}) TO STDOUT #{options};" do | |
while row = conn.get_copy_data | |
yield row | |
end | |
end | |
end |
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 characters
class ApplicationRecord < ActiveRecord::Base | |
extend DatabaseQueryStreaming | |
self.abstract_class = true | |
end |
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 characters
class SalesReportsController < AdminController | |
include ActionController::Live | |
def export | |
respond_to do |format| | |
format.csv { stream_csv_report } | |
end | |
end | |
private |