Skip to content

Instantly share code, notes, and snippets.

@esebastian
Created March 27, 2016 19:02
Show Gist options
  • Save esebastian/efe5803383253db8de80 to your computer and use it in GitHub Desktop.
Save esebastian/efe5803383253db8de80 to your computer and use it in GitHub Desktop.
VLCTechHub: event collection `publish_id` generator
require 'dotenv'
require 'uri'
require 'mongo'
require 'securerandom'
Dotenv.load
def format_uri(uri)
index = (uri =~ /@/)
index = (uri =~ /\/{2}/) + 1 unless index
uri[index+1..-1]
end
db_uri = ENV['MONGODB_URI']
puts "Updating database at #{format_uri(db_uri)}"
print "Press 'Y' to continue, anything else to abort (y/N) "
abort "Aborting!" if gets.downcase != "y\n"
print "Processing... "
Mongo::Logger.logger.level = ::Logger::ERROR
db = Mongo::Client.new(db_uri)
db['events'].find(publish_id: nil).each do |event|
publish_id = SecureRandom.uuid
db['events'].find(_id: event['_id']).update_one({'$set' => {publish_id: publish_id}})
end
db['events'].indexes.create_one({ :publish_id => 1 }, :unique => true)
puts "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment