Merge pull request #370 from ChrisBr/fix_migration
Assert event not deleted on add_events_per_week migration
This commit is contained in:
commit
0ca590a2e2
1 changed files with 44 additions and 40 deletions
|
|
@ -17,57 +17,61 @@ class AddEventsPerWeekToConference < ActiveRecord::Migration
|
||||||
add_column :conferences, :events_per_week, :text
|
add_column :conferences, :events_per_week, :text
|
||||||
|
|
||||||
TempVersion.where(item_type: 'Event').each do |event_version|
|
TempVersion.where(item_type: 'Event').each do |event_version|
|
||||||
event = TempEvent.find(event_version.item_id)
|
event = TempEvent.find_by_id(event_version.item_id)
|
||||||
conference = TempConference.find(event.conference_id)
|
if event
|
||||||
week = event_version.created_at.end_of_week
|
conference = TempConference.find_by_id(event.conference_id)
|
||||||
|
if conference
|
||||||
|
week = event_version.created_at.end_of_week
|
||||||
|
|
||||||
no_events = {
|
no_events = {
|
||||||
new: 0,
|
new: 0,
|
||||||
withdrawn: 0,
|
withdrawn: 0,
|
||||||
unconfirmed: 0,
|
unconfirmed: 0,
|
||||||
confirmed: 0,
|
confirmed: 0,
|
||||||
canceled: 0,
|
canceled: 0,
|
||||||
rejected: 0,
|
rejected: 0,
|
||||||
}
|
|
||||||
|
|
||||||
if !conference.events_per_week
|
|
||||||
conference.events_per_week = {
|
|
||||||
week => no_events
|
|
||||||
}
|
}
|
||||||
elsif !conference.events_per_week[week]
|
|
||||||
conference.events_per_week[week] = no_events
|
|
||||||
end
|
|
||||||
|
|
||||||
if event_version.object_changes &&
|
if !conference.events_per_week
|
||||||
event_version.event == 'create'
|
conference.events_per_week = {
|
||||||
|
week => no_events
|
||||||
# Increment the new state
|
}
|
||||||
conference.events_per_week[week][:new] += 1
|
elsif !conference.events_per_week[week]
|
||||||
elsif event_version.object_changes &&
|
conference.events_per_week[week] = no_events
|
||||||
event_version.object_changes[:state]
|
|
||||||
|
|
||||||
prev_state = event_version.object_changes[:state][0].to_sym
|
|
||||||
next_state = event_version.object_changes[:state][1].to_sym
|
|
||||||
|
|
||||||
# Backward compatibility: deprecated state :review now :new
|
|
||||||
if prev_state == :review
|
|
||||||
prev_state = :new
|
|
||||||
elsif next_state == :review
|
|
||||||
next_state = :new
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Increment the next state
|
if event_version.object_changes &&
|
||||||
conference.events_per_week[week][next_state] += 1
|
event_version.event == 'create'
|
||||||
|
|
||||||
# Decrement the previous state
|
# Increment the new state
|
||||||
conference.events_per_week[week][prev_state] -= 1
|
conference.events_per_week[week][:new] += 1
|
||||||
|
elsif event_version.object_changes &&
|
||||||
|
event_version.object_changes[:state]
|
||||||
|
|
||||||
|
prev_state = event_version.object_changes[:state][0].to_sym
|
||||||
|
next_state = event_version.object_changes[:state][1].to_sym
|
||||||
|
|
||||||
|
# Backward compatibility: deprecated state :review now :new
|
||||||
|
if prev_state == :review
|
||||||
|
prev_state = :new
|
||||||
|
elsif next_state == :review
|
||||||
|
next_state = :new
|
||||||
|
end
|
||||||
|
|
||||||
|
# Increment the next state
|
||||||
|
conference.events_per_week[week][next_state] += 1
|
||||||
|
|
||||||
|
# Decrement the previous state
|
||||||
|
conference.events_per_week[week][prev_state] -= 1
|
||||||
|
end
|
||||||
|
conference.save
|
||||||
|
end
|
||||||
end
|
end
|
||||||
conference.save
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Cumulate the previous weeks to get a snapshot
|
# Cumulate the previous weeks to get a snapshot
|
||||||
TempConference.all.each do |conference|
|
TempConference.all.each do |conference|
|
||||||
hash = conference.events_per_week.sort.to_hash
|
hash = conference.events_per_week.sort.to_h
|
||||||
previous = nil
|
previous = nil
|
||||||
|
|
||||||
hash.each do |week, values|
|
hash.each do |week, values|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue