Created
March 21, 2023 19:15
-
-
Save kinnalru/120a29144d0ec317f34bc1758f6f713a to your computer and use it in GitHub Desktop.
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
def csv_stream | |
sql = Request.where('created_at < ? AND client_id = ?', Time.now, 42).to_sql | |
Enumerator.new do |y| | |
conn = ActiveRecord::Base.connection.raw_connection | |
conn.copy_data("COPY (#{sql}) TO STDOUT WITH (FORMAT CSV, HEADER TRUE);") do | |
while (chunk = conn.get_copy_data) | |
y << chunk | |
end | |
end | |
end | |
end | |
stream = csv_stream |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment