Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Because I didn't found a simple solution for importing the database from Heroku (Postgresql) on my localhost (Mongoid). Import your database on localhost or online with this script (from Heroku)
## Requirements:
## - Ruby on Rails
## - Existing Heroku Application
## - HTTParty
# Get the json content of the
# backup and parse it
url = 'https://dataclips.heroku.com/xxx.json'
json = HTTParty.get(url)
# Cache the json result in the
# variables for them
fields = json['fields']
values = json['values']
# Initialize empty array for users
# and an empty hash for the puffer
users = []
puffer = {}
# Set the only arguments to be
# included in the User
allowed_arguments = ['email', 'encrypted_password', 'sign_in_count', 'name', 'username']
# For each value in the backup
# run the loop
values.each do |value|
# For each data and the index
# in the value var run the
# loop
value.each_with_index do |data, index|
# Only run if the current field is
# included in the allowed arguments
# variable
if allowed_arguments.include?(fields[index])
# Include the hash into the puffer
# variable
puffer.merge!(:"#{fields[index]}" => data)
end
end
# Merge the password wit the current
# puffer variable
puffer.merge!(:password => SecureRandom.hex(4))
# Push the current user into the
# users array
users.push(puffer)
# And empty the puffer variable
puffer = {}
end
# # Render the json for the browser
# # output
# render json: users.to_json
# Or Put each user into the local
# database
users.each do |user|
u = User.new user
unless u.save
raise "User #{u} could not be saved!"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.