Skip to content

Instantly share code, notes, and snippets.

View pbonnell's full-sized avatar

Peter B pbonnell

  • CIRCUIT LLC
View GitHub Profile
@pbonnell
pbonnell / mongiod_mysql_migrator.rb
Last active August 29, 2015 14:27 — forked from tomriley/mongiod_mysql_migrator.rb
Migrate a Mongoid / MongoDB database to an ActiveRecord based SQL one. One method to convert the schema, another to migrate data. Copes with most basic data types. Some hacks to infer TEXT columns. Converts embeds_one relationships to AR aggregations. I wrote this for a one-time migration. It could be a good starting point for someone doing the …
# Migrate schema and data from Mongoid to MySQL ActiveRecord
class MongoidMysqlMigrator
def randomize_auto_increment_values(source_models, from=5500, to=10500)
source_models.each do |model|
value = rand(from-to)+from
sql = %(ALTER TABLE #{model.name.tableize} AUTO_INCREMENT=#{value})
puts sql
ActiveRecord::Base.connection.execute(sql)
end
@pbonnell
pbonnell / production-data-sync.rb
Last active August 29, 2015 14:06
Sync data between heroku apps
#!/usr/bin/env ruby
require 'optparse'
require 'pty'
#TODO: PTY pushes messages but not the nice progress provided by `heroku pgbackups`
# Pushes messaging from a shell command to stdout.
# So that child processes can report to the script output
# @param cmd [string] the external process to run
#
@pbonnell
pbonnell / released-from.rb
Last active August 29, 2015 14:06
Ruby shell script to show what git branches contain the current release of a heroku app
#!/usr/bin/env ruby
if ARGV[0].nil?
puts "List the local branches of a git repository that contain the most recent release of a heroku app."
puts "usage: released-from heroku-app-name"
exit
else
h = `heroku releases -n1 -a#{ARGV[0]}`
puts h
parsed = h.scan(/Deploy (.{7}) /).flatten[0]
unless parsed.nil?
12:12:27 web.1 | started with pid 6842
12:12:29 web.1 | I, [2013-12-03T12:12:29.239032 #6842] INFO -- : Refreshing Gem list
12:12:33 web.1 | /moped-2.0.0.beta4/lib/moped/operation/read.rb:50:in `block in execute': The operation: #<Moped::Protocol::Query (Moped::Errors::QueryFailure)
12:12:33 web.1 | @length=51
12:12:33 web.1 | @request_id=5
12:12:33 web.1 | @response_to=0
12:12:33 web.1 | @op_code=2004
12:12:33 web.1 | @flags=[]
12:12:33 web.1 | @full_collection_name="app19591881.roles"
12:12:33 web.1 | @skip=0
alias stopsql='sudo launchctl unload -w /Library/LaunchDaemons/com.mysql.mysqld.plist'
alias startsql='sudo launchctl load -w /Library/LaunchDaemons/com.mysql.mysqld.plist'
alias l='ls -las'
alias 'tailr'='tail -f ./log/development.log'
alias 'ec2_describe'='$EC2_HOME/bin/ec2-describe-images | grep machine'
alias 'ec2_status'='$EC2_HOME/bin/ec2-describe-instances'
alias 'ec2_start'='$EC2_HOME/bin/ec2-run-instances $1 -k gsg-keypair'
alias 'ec2_stop'='ec2-terminate-instances $1'
alias 'ec2_connect'='ssh -i id_rsa-gsg-keypair root@{$1}'
alias 'updatedb'='sudo /usr/libexec/locate.updatedb'
@pbonnell
pbonnell / gist:2323124
Created April 6, 2012 21:36
expensive to put tracers on all class methods ?
if defined?(NewRelic)
Sircle.class_eval do
class << self
include NewRelic::Agent::MethodTracer
# all methods defined in sircle.rb
self.methods(false).each do |method|
add_method_tracer method.to_s, "Custom/Sircle/#{method}"
end
end
end
@pbonnell
pbonnell / gist:1362128
Created November 13, 2011 13:52
SIRCLE Reviews
# Add review (comment)
([{"request_path"=>"http://staging2.sircleadvertising.com/people/ito/4eb9dd393132182d9300/Actions?h=J8th3fK7",
"request_body"=>"<TrackingActionReviewUnsolicited i:type=\"TrackingActionReviewUnsolicited\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/SIRCLE.DataAccess.BusinessObjects.External\"><ActionID>46</ActionID><CategoryName>Administrative</CategoryName><ExternalID>4eb9dd393132182d9300000e</ExternalID><ProductID>38</ProductID><Review>Eye shadow is great</Review></TrackingActionReviewUnsolicited>",
"status_code"=>200,
"response_body"=>"false",
"request_method"=>"post", "_id"=>BSON::ObjectId('4ebfcdea313218f2d7000086'), "updated_at"=>2011-11-13 14:02:18 UTC, "created_at"=>2011-11-13 14:02:18 UTC}]
# Add Rating
([{"request_path"=>"http://staging2.sircleadvertising.com/people/ito/4eb9dd393132182d9300/Actions?h=J8th3fK7",
"request_body"=>"<TrackingActionRating xmlns=\"http://schemas.datacontract.org/2004/07/SIRCLE.DataAccess.
@pbonnell
pbonnell / gist:1361007
Created November 12, 2011 19:43
Stub for SIRCLE Suggest Product
def self.post_product_suggestion(user, data)
#TODO: STUBBED HERE UNTIl SIRCLE implements
action_id = nil
return_data = post('/Actions', user) do |xml|
xml.TrackingActionRecommendation(:xmlns => SIRCLE_XML_NS) {
xml.ActionID action_id # SIRCLE will tell us!
xml.CategoryName 'Products'
# brand_id would be INT of existing Brand , brand_name would be User input String of New brand
xml.Brand = data['brand_name'] || data['brand_id']
xml.ProductName = data['product_name'] # User input String of product name
@pbonnell
pbonnell / gist:1355016
Created November 10, 2011 14:48
SircleObjects on local VS. Heroku :
LOCAL DEV:
ruby-1.9.2-p290 :001 > p = Sircle.get_products(Sircle::UserZero)[0]
=> {"Brand"=>{"ExternalLoginUserName"=>nil, "ID"=>4, "Logo"=>"http://staging2.sircleadvertising.com/SampleImage/ITO/16?h=J8th3fK7", "Name"=>"Lift Lab"}, "ID"=>1, "ImageURL"=>"http://staging2.sircleadvertising.com/SampleImage/ITO/28?h=J8th3fK7", "Name"=>"Facial Cream", "SampleIDs"=>[], "Subcategory"=>{"Category"=>{"ID"=>1, "Name"=>"Bath & Body"}, "ID"=>8, "Name"=>"Deodorant"}}
ruby-1.9.2-p290 :002 >
HEROKU CONSOLE:
>> p = Sircle.get_products(Sircle::UserZero)[0]
=> #<SircleProduct:0x00000006bf4b80 @attributes=<#Hashie::Mash brand=<#Hashie::Mash external_login_user_name=nil id=4 logo="http://staging2.sircleadvertising.com/SampleImage/ITO/16?h=J8th3fK7" name="Lift Lab"> id=1 image_url="http://staging2.sircleadvertising.com/SampleImage/ITO/28?h=J8th3fK7" name="Facial Cream" sample_i_ds=[] subcategory=<#Hashie::Mash category=<#Hashie::Mash id=1 name="Bath & Body"> id=8 name="Deodorant">>>
>>
@pbonnell
pbonnell / gist:1355014
Created November 10, 2011 14:48
SircleObjects on local VS. Heroku :
LOCAL DEV:
ruby-1.9.2-p290 :001 > p = Sircle.get_products(Sircle::UserZero)[0]
=> {"Brand"=>{"ExternalLoginUserName"=>nil, "ID"=>4, "Logo"=>"http://staging2.sircleadvertising.com/SampleImage/ITO/16?h=J8th3fK7", "Name"=>"Lift Lab"}, "ID"=>1, "ImageURL"=>"http://staging2.sircleadvertising.com/SampleImage/ITO/28?h=J8th3fK7", "Name"=>"Facial Cream", "SampleIDs"=>[], "Subcategory"=>{"Category"=>{"ID"=>1, "Name"=>"Bath & Body"}, "ID"=>8, "Name"=>"Deodorant"}}
ruby-1.9.2-p290 :002 >
HEROKU CONSOLE:
>> p = Sircle.get_products(Sircle::UserZero)[0]
=> #<SircleProduct:0x00000006bf4b80 @attributes=<#Hashie::Mash brand=<#Hashie::Mash external_login_user_name=nil id=4 logo="http://staging2.sircleadvertising.com/SampleImage/ITO/16?h=J8th3fK7" name="Lift Lab"> id=1 image_url="http://staging2.sircleadvertising.com/SampleImage/ITO/28?h=J8th3fK7" name="Facial Cream" sample_i_ds=[] subcategory=<#Hashie::Mash category=<#Hashie::Mash id=1 name="Bath & Body"> id=8 name="Deodorant">>>
>>