Import task

This commit is contained in:
Rei
2014-02-28 22:25:23 +08:00
parent 375f2b2a83
commit d484c137dc
3 changed files with 59 additions and 0 deletions

View File

@@ -100,3 +100,6 @@ end
group :development, :test do
gem 'factory_girl_rails', '~> 4.3.0'
end
gem 'mongo'
gem 'bson_ext'

View File

@@ -93,6 +93,9 @@ GEM
bcrypt-ruby (3.1.2)
bootstrap-sass (3.1.0.1)
sass (~> 3.2)
bson (1.9.2)
bson_ext (1.9.2)
bson (~> 1.9.2)
builder (3.2.2)
carrierwave (0.9.0)
activemodel (>= 3.2.0)
@@ -186,6 +189,8 @@ GEM
subexec (~> 0.2.1)
mini_portile (0.5.2)
minitest (5.2.2)
mongo (1.9.2)
bson (~> 1.9.2)
mono_logger (1.1.0)
multi_json (1.8.4)
multipart-post (2.0.0)
@@ -271,6 +276,7 @@ DEPENDENCIES
arel!
bcrypt-ruby (~> 3.1.2)
bootstrap-sass (~> 3.1.0)
bson_ext
carrierwave (~> 0.9.0)
coffee-rails!
elasticsearch-model!
@@ -287,6 +293,7 @@ DEPENDENCIES
kaminari (~> 0.15.1)
mail_view (~> 2.0.4)
mini_magick (~> 3.7.0)
mongo
nokogiri (~> 1.6.1)
pg
rack-livereload

49
lib/tasks/import.rake Normal file
View File

@@ -0,0 +1,49 @@
desc "Import from code_campo"
task :import => [:environment, 'db:schema:load'] do
db = Mongo::MongoClient.new['code_campo']
puts 'Start import'
user_map = {}
db['users'].find.sort(created_at: :asc).each do |doc|
user = User.new(name: (doc['profile']['name'].present? ? doc['profile']['name'] : doc['name']),
username: doc['name'].gsub('_', '-'),
email: doc['email'],
password_digest: (doc['password_digest'] || User.new(password: SecureRandom.hex(16)).password_digest),
bio: doc['profile']['description'],
created_at: doc['created_at'],
updated_at: doc['created_at'])
if user.save
user_map[doc['_id']] = user.id
else
puts user.errors.inspect
exit
end
end
puts "User: #{User.count}"
topic_map = {}
db['topics'].find.sort(created_at: :asc).each do |doc|
topic = Topic.create!(id: doc['number_id'],
title: doc['title'],
body: doc['content'],
user_id: user_map[doc['user_id']],
created_at: doc['created_at'],
updated_at: doc['updated_at'])
topic_map[doc['_id']] = topic.id
end
puts "Topic: #{Topic.count}"
comment_map = {}
db['replies'].find.sort(created_at: :asc).each do |doc|
comment = Comment.create!(id: doc['number_id'],
body: doc['content'],
user_id: user_map[doc['user_id']],
commentable_type: 'Topic',
commentable_id: topic_map[doc['topic_id']],
created_at: doc['created_at'],
updated_at: doc['updated_at'])
comment_map[doc['_id']] = comment.id
end
puts "Comment: #{Comment.count}"
end