Created
November 17, 2021 11:24
-
-
Save coorasse/f90c4badfdc36f42e4ebd23dfaac0fc4 to your computer and use it in GitHub Desktop.
Dump large table to JSON file in ruby
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
File.open(file_path, 'w') do |file| | |
file.write("[") | |
if records.any? | |
last_id = records.last.id | |
records.find_each do |model| | |
file.write(model.to_json(except: excluded_columns)) | |
file.write(",\n") if model.id != last_id | |
end | |
end | |
file.write("]\n") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
JSON dumping solutions expect you to do something simpler like:
this will inevitably crash if your table contains millions of rows.
You can use the above script when dumping ActiveRecord tables for example