Skip to content

Instantly share code, notes, and snippets.

@kinnalru
Created March 21, 2023 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kinnalru/120a29144d0ec317f34bc1758f6f713a to your computer and use it in GitHub Desktop.
Save kinnalru/120a29144d0ec317f34bc1758f6f713a to your computer and use it in GitHub Desktop.
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