Skip to content

Commit

Permalink
Transform model calls to queries in migrations to not run into enum e…
Browse files Browse the repository at this point in the history
…rror
  • Loading branch information
Vakmeth committed Jan 28, 2025
1 parent 50fa4bf commit 6f1b52e
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions db/migrate/20150728084536_add_event_participation_states.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,32 @@ def up
unless column_exists?(:event_participations, :state)
add_column(:event_participations, :state, :string, limit: 60)

course_participations =
Event::Participation.joins(:event).
where(events: { type: Event::Course.sti_name })
course_participations.where(active: true).update_all(state: :assigned)
course_participations.where(active: false).update_all(state: :applied)
end
connection = ActiveRecord::Base.connection
set_assigned = <<-SQL
UPDATE events
SET state = 'assigned'
WHERE id IN
(
SELECT events.id FROM event_participations AS participations
JOIN events ON participations.event_id = events.id
WHERE events.type = 'Event::Course' AND participations.active = true
)
SQL

set_applied = <<-SQL
UPDATE events
SET state = 'applied'
WHERE id IN
(
SELECT events.id FROM event_participations AS participations
JOIN events ON participations.event_id = events.id
WHERE events.type = 'Event::Course' AND participations.active = false
)
SQL

Event::Participation.reset_column_information
Event.reset_column_information # to make new column appear
connection.execute(set_assigned)
connection.execute(set_applied)
end

# Recalculate the counts of all events
Event.find_each { |e| e.refresh_participant_counts! }
Expand Down

0 comments on commit 6f1b52e

Please sign in to comment.