Skip to content

Instantly share code, notes, and snippets.

@sankalpk
Last active August 21, 2019 20:50
Show Gist options
  • Select an option

  • Save sankalpk/8a309f0778877eb14e3d289e75f93600 to your computer and use it in GitHub Desktop.

Select an option

Save sankalpk/8a309f0778877eb14e3d289e75f93600 to your computer and use it in GitHub Desktop.
Populate a Rails app w/ Elasticsearch

Clear and populate the database with fresh data:

rake db:populate

Clear the database:

rake db:clean
namespace :db do
desc "Fill the database with new data"
task fill: :environment do
# Populate records via FactoryBot
puts 'Populate Records Complete'
end
desc 'Clean the database and elasticsearch'
task clean: :environment do
next if ENV['ENVIRONMENT'] == 'production'
require 'database_cleaner'
DatabaseCleaner.strategy = :truncation
puts 'Cleaning database'
DatabaseCleaner.clean
puts 'Cleaning elasticsearch'
UserEvent.__elasticsearch__.client.indices.delete index: UserEvent.index_name rescue nil
UserContact.__elasticsearch__.client.indices.delete index: UserContact.index_name rescue nil
UserEvent.__elasticsearch__.client.indices.create \
index: UserEvent.index_name,
body: { settings: UserEvent.settings.to_hash, mappings: UserEvent.mappings.to_hash }
UserContact.__elasticsearch__.client.indices.create \
index: UserContact.index_name,
body: { settings: UserContact.settings.to_hash, mappings: UserContact.mappings.to_hash }
puts 'Database clean'
end
desc 'Populate the database with fresh data'
task populate: ['db:clean', 'db:fill']
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment