20110422152027_move_notices_to_separate_collection.rb
1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class MoveNoticesToSeparateCollection < Mongoid::Migration
def self.up
errs_coll = connection["errs"]
# copy embedded Notices into a separate collection
errs = errs_coll.find.select(notices: 1)
errs.each do |err|
next unless err['notices']
# This Err was created after the Problem->Err->Notice redesign
next if err['app_id'].nil? or err['problem_id']
e = Err.find(err['_id'])
# disable email notifications
old_notify = e.app.notify_on_errs?
e.app.update_attribute(:notify_on_errs, false)
puts "Copying notices for Err #{err['_id']}"
err['notices'].each do |notice|
e.notices.create!(notice)
end
e.app.update_attribute(:notify_on_errs, old_notify)
errs_coll.find({ "_id" => err['_id']}).update({ "$unset" => { "notices" => 1}})
end
(
Problem.where(:environment => '') |
Problem.where(:environment => nil) |
Problem.where(:environment => {})
).each {|pr|
pr.update_attributes(:environment => 'old')
}
Rake::Task["errbit:db:update_notices_count"].invoke
Rake::Task["errbit:db:update_problem_attrs"].invoke
end
def self.down
end
end