Created
December 8, 2017 08:58
-
-
Save eldoy/deb42f2dd593f7732996e7bacac8c4bc to your computer and use it in GitHub Desktop.
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
# Add rethinkdb to Gemfile | |
# Add to boot.rb: autoload :RethinkDB, 'rethinkdb' | |
# Set up RabbitMQ queue as well | |
# Load database settings | |
config = (YAML.load_file('./config/database.yml') || {})[App.env] | |
# Connect database | |
if config | |
# Read settings or load defaults | |
name = "#{config['name'] || App.name}_#{App.env}" | |
# Connect rethinkdb | |
include RethinkDB::Shortcuts | |
r.connect(:host => 'localhost', :port => 28015, :db => name).repl | |
# Create database unless it exists | |
r.db_create(name).run unless r.db_list.run.include?(name) | |
# Create tables | |
table_list = r.db(name).table_list.run | |
%w[projects].each do |table| | |
r.db(name).table_create(table).run unless table_list.include?(table) | |
end | |
# Listen for changes | |
cursor = r.table('projects').changes | |
Thread.new do | |
cursor.run.each do |document| | |
token = 'BNquxwicvgrVJxdBidmiIA' | |
App.channel.publish(token, document.to_json) | |
end | |
end | |
else | |
puts "Database settings not found for #{App.env} in config/database.yml" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment