Skip to content

MongoDB->Postgres Migration (Part 2 of 2): Convert Mongoid references to ActiveRecord #226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Nov 11, 2014
12 changes: 9 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ gem 'simple_form'
gem 'tweet-button'
gem 'local_time'

gem 'elasticsearch-model'
gem 'elasticsearch-rails'

# DROP BEFORE RAILS 4
# Mongo
gem 'mongoid'
Expand All @@ -141,6 +144,7 @@ gem 'tire'

group :development do
gem 'better_errors'
gem 'binding_of_caller'
gem 'flog'
gem 'fukuzatsu'
gem 'guard-rspec'
Expand All @@ -152,16 +156,17 @@ group :development do
end

group :development, :test do
gem 'annotate'
gem 'fabrication-rails'
gem 'ffaker'
gem 'jazz_hands', github: 'nixme/jazz_hands', branch: 'bring-your-own-debugger'
gem 'launchy'
gem 'pry-byebug'
gem 'pry-rescue'
#gem 'pry-rescue'
#gem 'pry-stack_explorer'
gem 'quiet_assets'
gem 'syntax'
gem 'annotate'
gem 'rspec-rails'
gem 'syntax'
end

group :test do
Expand All @@ -176,6 +181,7 @@ group :test do
gem 'timecop'
gem 'vcr'
gem 'webmock', '<1.16'
gem 'stripe-ruby-mock', git: 'https://github.com/rebelidealist/stripe-ruby-mock', branch: 'live-tests'
end

group :production do
Expand Down
61 changes: 45 additions & 16 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
GIT
remote: git://github.com/emberjs/ember-rails.git
revision: 5e5a398f3c67c3a3b84b7513b93b22bf81055cc9
specs:
ember-rails (0.15.0)
active_model_serializers
barber (>= 0.4.1)
ember-data-source (>= 1.0.0.beta.5)
ember-source (>= 1.1.0)
execjs (>= 1.2)
handlebars-source (> 1.0.0)
jquery-rails (>= 1.0.17)
railties (>= 3.1)

GIT
remote: git://github.com/nixme/jazz_hands.git
revision: 5e4b48f145883ecb14b55bf04eacc28ac9662676
Expand Down Expand Up @@ -25,18 +39,14 @@ GIT
rest-client (~> 1.4)

GIT
remote: git://github.com/emberjs/ember-rails.git
revision: 5e5a398f3c67c3a3b84b7513b93b22bf81055cc9
remote: https://github.com/rebelidealist/stripe-ruby-mock
revision: f4229cbe762bc2a5a302cdd83088c283881e9c5a
branch: live-tests
specs:
ember-rails (0.15.0)
active_model_serializers
barber (>= 0.4.1)
ember-data-source (>= 1.0.0.beta.5)
ember-source (>= 1.1.0)
execjs (>= 1.2)
handlebars-source (> 1.0.0)
jquery-rails (>= 1.0.17)
railties (>= 3.1)
stripe-ruby-mock (1.10.1.7)
dante (>= 0.2.0)
jimson-temp
stripe (>= 1.15.0)

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -106,6 +116,7 @@ GEM
rack (>= 0.9.0)
binding_of_caller (0.7.2)
debug_inspector (>= 0.0.1)
blankslate (3.1.3)
bson (1.10.2)
bson_ext (1.10.2)
bson (~> 1.10.2)
Expand Down Expand Up @@ -173,6 +184,7 @@ GEM
httparty (~> 0.10)
json
curb (0.8.6)
dante (0.2.0)
database_cleaner (1.3.0)
debug_inspector (0.0.2)
debugger-linecache (1.2.0)
Expand All @@ -192,6 +204,19 @@ GEM
execjs
eco-source (1.1.0.rc.1)
ejs (1.1.1)
elasticsearch (1.0.4)
elasticsearch-api (= 1.0.4)
elasticsearch-transport (= 1.0.4)
elasticsearch-api (1.0.4)
multi_json
elasticsearch-model (0.1.4)
activesupport (> 3)
elasticsearch (> 0.4)
hashie
elasticsearch-rails (0.1.4)
elasticsearch-transport (1.0.4)
faraday
multi_json
em-http-request (1.1.2)
addressable (>= 2.3.4)
cookiejar
Expand Down Expand Up @@ -316,11 +341,15 @@ GEM
httpauth (0.2.1)
i18n (0.6.11)
inflecto (0.0.2)
interception (0.5)
ipaddress (0.8.0)
jbuilder (2.1.3)
activesupport (>= 3.0.0, < 5)
multi_json (~> 1.2)
jimson-temp (0.9.5)
blankslate (>= 3.1.2)
multi_json (~> 1.0)
rack (~> 1.4)
rest-client (~> 1.0)
journey (1.0.4)
jquery-rails (2.0.3)
railties (>= 3.1.0, < 5.0)
Expand Down Expand Up @@ -459,9 +488,6 @@ GEM
pry-remote (0.1.8)
pry (~> 0.9)
slop (~> 3.0)
pry-rescue (1.4.1)
interception (>= 0.5)
pry
pry-stack_explorer (0.4.9.1)
binding_of_caller (>= 0.7)
pry (>= 0.9.11)
Expand Down Expand Up @@ -714,6 +740,7 @@ DEPENDENCIES
awesome_print
backbone-on-rails
better_errors
binding_of_caller
bson_ext
capybara
capybara-screenshot
Expand All @@ -729,6 +756,8 @@ DEPENDENCIES
createsend
database_cleaner
dotenv-rails
elasticsearch-model
elasticsearch-rails
ember-rails!
fabrication-rails
faraday (~> 0.8.1)
Expand Down Expand Up @@ -776,7 +805,6 @@ DEPENDENCIES
pg
postgres_ext
pry-byebug
pry-rescue
pubnub (= 0.1.9)
puma
querystring
Expand Down Expand Up @@ -809,6 +837,7 @@ DEPENDENCIES
spring-commands-rspec
squeel (= 1.0.1)
stripe!
stripe-ruby-mock!
strong_parameters
syntax
timecop
Expand Down
178 changes: 178 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,181 @@ require 'rake'
Coderwall::Application.load_tasks

task default: :spec


namespace :team do
task migrate: :environment do
TeamMigratorBatchJob.new.perform
end

#
# IMPORTANT: pending_join_requests is a STRING array in Postgres but an INTEGER array in MongoDB.
# IMPORTANT: pending_join_requests is an array of User#id values
#

def neq(attr, pg, mongo, fail_if_neq=true)
left = pg.send(attr)
right = mongo.send(attr)

if left != right
puts "#{attr} | pg:#{pg.id} | mongo:#{mongo.id}| #{left} != #{right}"
true
else
false
end
rescue => ex
puts '*'*80
puts
puts ex
puts
puts '-'*80
puts
ap ex.backtrace
puts
puts '*'*80

require 'pry'; binding.pry
end

task verify: :environment do
PgTeam.find_each(batch_size: 100) do |pg_team|
begin
mongo_id = pg_team.mongo_id
mongo_team = Team.find(mongo_id)

# Ignoring:
# - updated_at

puts '----------------------------------------------------------------------------------------------------'
puts 'TEAM'
puts '----------------------------------------------------------------------------------------------------'

%i(median score total slug mean pending_join_requests).each do |attr|
neq(attr, pg_team, mongo_team, false)
end

%i(about achievement_count analytics benefit_description_1 benefit_description_2 benefit_description_3 benefit_name_1 benefit_name_2 benefit_name_3 big_image big_quote blog_feed branding country_id created_at endorsement_count facebook featured_banner_image featured_links_title github github_organization_name headline hide_from_featured highlight_tags hiring_tagline interview_steps invited_emails link_to_careers_page location monthly_subscription name number_of_jobs_to_show office_photos organization_way organization_way_name organization_way_photo our_challenge paid_job_posts premium preview_code reason_description_1 reason_description_2 reason_description_3 reason_name_1 reason_name_2 reason_name_3 size stack_list twitter upcoming_events upgraded_at valid_jobs website why_work_image your_impact youtube_url).each do |attr|
neq(attr, pg_team, mongo_team)
end

# TODO: Account
if mongo_team.account.present? && pg_team.account.blank?
puts "account | pg:#{pg_team.id} | mongo:#{mongo_team.id}| The account was not migrated."
end

if mongo_team.account.present? && pg_team.account.present?
check_plans = %i(stripe_card_token stripe_customer_token admin_id).map do |attr|
neq(attr, pg_team.account, mongo_team.account)
end.any? { |x| !x }

# TODO: Plans
if check_plans
left = pg_team.account.plans.pluck(:id).sort
right = mongo_team.account.plan_ids.sort

if left != right
puts "account.plans | pg:#{pg_team.id} | mongo:#{mongo_team.id}| #{left} != #{right}"
end
end
end


puts '----------------------------------------------------------------------------------------------------'
puts 'LOCATIONS'
puts '----------------------------------------------------------------------------------------------------'

pg_team_locations = pg_team.locations
mongo_team_locations = mongo_team.team_locations

if mongo_team_locations.count != pg_team_locations.count
puts "locations | pg:#{pg_team.id} | mongo:#{mongo_team.id}| #{mongo_team_locations.count} != #{pg_team_locations.count}"
end

# Ignoring:
# - points_of_interest
pg_team.locations.each do |pg_team_location|
mongo_team_location = mongo_team.team_locations.select { |tl| tl.name == pg_team_location.name }.first

%i(address city country description name state_code).each do |attr|
neq(attr, pg_team_location, mongo_team_location, false)
end
end


puts '----------------------------------------------------------------------------------------------------'
puts 'LINKS'
puts '----------------------------------------------------------------------------------------------------'

pg_team_links = pg_team.links
mongo_team_links = mongo_team.featured_links

if mongo_team_links.count != pg_team_links.count
puts "links | pg:#{pg_team.id} | mongo:#{mongo_team.id}| #{mongo_team_links.count} != #{pg_team_links.count}"
end

pg_team_links.each do |pg_team_link|
mongo_team_link = mongo_team_links.select { |tl| tl.name == pg_team_link.name }.first

%i(url name).each do |attr|
neq(attr, pg_team_link, mongo_team_link, false)
end
end

puts '----------------------------------------------------------------------------------------------------'
puts 'MEMBERS'
puts '----------------------------------------------------------------------------------------------------'

if pg_team.members.count != mongo_team.team_members.count
puts "members | pg:#{pg_team.id} | mongo:#{mongo_team.id}| #{mongo_team.team_members.count} != #{pg_team.members.count}"
end


puts '----------------------------------------------------------------------------------------------------'
puts 'JOBS'
puts '----------------------------------------------------------------------------------------------------'

pg_team.jobs.each do |pg_team_job|
mongo_team_job = Team.where(id: pg_team_job.team_document_id.to_s).first

neq(:name, pg_team_job, mongo_team_job, false)
end

puts '----------------------------------------------------------------------------------------------------'
puts 'FOLLOWERS'
puts '----------------------------------------------------------------------------------------------------'

pg_team.followers.each do |pg_team_follower|
mongo_team_follower = Team.where(id: pg_team_follower.mongo_id.to_s).first
# admins
# editors
%i(about achievement_count analytics avatar benefit_description_1 benefit_description_2 benefit_description_3 benefit_name_1 benefit_name_2 benefit_name_3 big_image big_quote blog_feed branding country_id created_at endorsement_count facebook featured_banner_image featured_links_title github_organization_name headline hide_from_featured highlight_tags hiring_tagline interview_steps invited_emails link_to_careers_page location mean median monthly_subscription name number_of_jobs_to_show office_photos organization_way organization_way_name organization_way_photo our_challenge paid_job_posts pending_join_requests premium preview_code reason_description_1 reason_description_2 reason_description_3 reason_name_1 reason_name_2 reason_name_3 score size slug stack_list total twitter upcoming_events updated_at upgraded_at valid_jobs website why_work_image your_impact youtube_url).each do |attr|
neq(attr, pg_team_follower, mongo_team_follower, false)
end
end

# TODO: Pending Requests

end
end
end

task counts: :environment do
pg_team_count = PgTeam.count
puts "PgTeam.count=#{pg_team_count}"
team_count = Team.count
puts "Team.count=#{team_count}"
puts "Unmigrated teams count=#{(team_count - pg_team_count)}"
end


task unmigrated: :environment do
unmigrated_teams = []

Team.all.each do |team|
unmigrated_teams << team.id.to_s unless PgTeam.where(mongo_id: team.id.to_s).exists?
end

puts "Unmigrated teams count=#{unmigrated_teams.count}"
puts "Unmigrated Teams=%w(#{unmigrated_teams.join(' ')})"
end
end
Loading